diff --git a/TypeScript/.gitignore b/TypeScript/.gitignore index 7872ff9b..778a45b5 100644 --- a/TypeScript/.gitignore +++ b/TypeScript/.gitignore @@ -9,4 +9,3 @@ test/**/*.js.map coverage .nyc_output .yarn -.DS_Store \ No newline at end of file diff --git a/TypeScript/app/constants.ts b/TypeScript/app/constants.ts new file mode 100644 index 00000000..7cde7b89 --- /dev/null +++ b/TypeScript/app/constants.ts @@ -0,0 +1,5 @@ +export const ITEMS = { + BRIE: "Aged Brie", + SURFRAS: "Sulfuras, Hand of Ragnaros", + PASSES: "Backstage passes to a TAFKAL80ETC concert", +}; diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index db58d678..15d316bf 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -1,3 +1,5 @@ +import { ITEMS } from "./constants"; + export class Item { name: string; sellIn: number; @@ -17,51 +19,52 @@ export class GildedRose { this.items = items; } + handlePassesQuality(item) { + if (item.name !== ITEMS.PASSES) return; + if (6 <= item.sellIn && item.sellIn < 11) { + item.quality += 1; + } + + if (item.sellIn < 6) { + item.quality += 2; + } + } + + handleIfSellInIs0(item) { + if (item.sellIn >= 0) return; + + switch (item.name) { + case ITEMS.BRIE: + if (item.quality >= 50) return; + item.quality = item.quality + 1; + break; + case ITEMS.SURFRAS: + item.quality = 0; + break; + default: + if (!item.quality) return; + item.quality -= 1; + break; + } + } + updateQuality() { - for (let i = 0; i < this.items.length; i++) { - if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].quality > 0) { - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].quality = this.items[i].quality - 1 - } - } - } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].sellIn < 11) { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } - } - if (this.items[i].sellIn < 6) { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } - } - } - } + for (const item of this.items) { + if (!item.quality) break; + if (item.name != ITEMS.SURFRAS) { + item.sellIn -= 1; } - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].sellIn = this.items[i].sellIn - 1; - } - if (this.items[i].sellIn < 0) { - if (this.items[i].name != 'Aged Brie') { - if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') { - if (this.items[i].quality > 0) { - if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].quality = this.items[i].quality - 1 - } - } - } else { - this.items[i].quality = this.items[i].quality - this.items[i].quality - } - } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } - } + this.handleIfSellInIs0(item); + + if (item.name != ITEMS.BRIE && item.name != ITEMS.PASSES) { + if (item.name === ITEMS.SURFRAS) break; + item.quality -= 1; } + + if (item.quality >= 50) break; + item.quality += 1; + + this.handlePassesQuality(item); } return this.items; diff --git a/TypeScript/test/golden-master-text-test.ts b/TypeScript/test/golden-master-text-test.ts index 2259b975..6fa5537a 100644 --- a/TypeScript/test/golden-master-text-test.ts +++ b/TypeScript/test/golden-master-text-test.ts @@ -1,4 +1,4 @@ -import { Item, GildedRose } from '../app/gilded-rose'; +import { Item, GildedRose } from "../app/gilded-rose"; const items = [ new Item("+5 Dexterity Vest", 10, 20), // @@ -10,22 +10,21 @@ const items = [ new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), // this conjured item does not work properly yet - new Item("Conjured Mana Cake", 3, 6)]; - + // new Item("Conjured Mana Cake", 3, 6) +]; const gildedRose = new GildedRose(items); let days: number = 2; if (process.argv.length > 2) { - days = +process.argv[2]; - } + days = +process.argv[2]; +} for (let i = 0; i < days; i++) { console.log("-------- day " + i + " --------"); console.log("name, sellIn, quality"); - items.forEach(element => { - console.log(element.name + ' ' + element.sellIn + ' ' + element.quality); - + items.forEach((element) => { + console.log(element.name + " " + element.sellIn + " " + element.quality); }); console.log(); gildedRose.updateQuality(); diff --git a/TypeScript/test/jest/gilded-rose.spec.ts b/TypeScript/test/jest/gilded-rose.spec.ts index 48da86d3..e89a60f6 100644 --- a/TypeScript/test/jest/gilded-rose.spec.ts +++ b/TypeScript/test/jest/gilded-rose.spec.ts @@ -77,6 +77,4 @@ describe("Backstage passes 테스트", () => { expect(items[0].sellIn).toBe(4); expect(items[0].quality).toBe(6); }); - - // TODO: 콘서트 종료 후 quality 0으로 변경 테스트 });