diff --git a/src/stock.ts b/src/stock.ts index 86098dc..92a6141 100644 --- a/src/stock.ts +++ b/src/stock.ts @@ -17,6 +17,10 @@ export class Stock { this.time = new Date(); } + static printfHeader() { + console.log(`Stock\tPrice\t\tVolume\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`); + } + printf () { const volume = this.f(this.volume); console.log(`${this.code}\t${this.f(this.currentPrice)}\t\t${volume}${volume.length >= 9 ? '\t' : '\t\t' }${this.f(this.openPrice)}\t\t${this.f(this.highestPrice)}\t\t${this.f(this.lowestPrice)}\t\t${this.time.toLocaleTimeString()}`); diff --git a/src/stock_api.ts b/src/stock_api.ts index d4d9ab8..76e35eb 100644 --- a/src/stock_api.ts +++ b/src/stock_api.ts @@ -1,7 +1,8 @@ import { Helper } from './helper'; +import { Stock } from './stock'; const gets = (stockCodes: string[]) => { - console.log(`Stock\tPrice\t\tVolume\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`); + Stock.printfHeader(); try { return Promise.all(stockCodes.map((code) => { return Helper.fetchStock(code).then(stock => stock && stock.printf()); @@ -12,7 +13,7 @@ const gets = (stockCodes: string[]) => { } const stream = (stockCode: string) => { - console.log(`Stock\tPrice\t\tVolume\t\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`); + Stock.printfHeader(); setInterval(() => { Helper.fetchStock(stockCode).then(stock => stock && stock.printf()); }, 2000);