diff --git a/TypeScript/app/components/Item.ts b/TypeScript/app/components/Item.ts index 0497e01e..5cfea7bf 100644 --- a/TypeScript/app/components/Item.ts +++ b/TypeScript/app/components/Item.ts @@ -1,11 +1,15 @@ export class Item { - name: string; - sellIn: number; - quality: number; - - constructor(name, sellIn, quality) { - this.name = name; - this.sellIn = sellIn; - this.quality = quality; + constructor(public name: string, public sellIn: number, public quality: number) {} + + /** + * update quality is a common across all functions, so moving it to Item and extend them + */ + updateQuality() { + this.sellIn--; + this.quality = Math.max(this.quality - 1, 0); + + if (this.sellIn < 0) { + this.quality = Math.max(this.quality - 1, 0); + } } - } \ No newline at end of file +} \ No newline at end of file diff --git a/TypeScript/app/components/aged-brie.ts b/TypeScript/app/components/aged-brie.ts new file mode 100644 index 00000000..5096a87a --- /dev/null +++ b/TypeScript/app/components/aged-brie.ts @@ -0,0 +1,8 @@ +import { Item } from "./Item"; + +export class AgedBrie extends Item { + updateQuality() { + super.updateQuality(); + this.quality = Math.min(this.quality + 1, 50); + } + } \ No newline at end of file diff --git a/TypeScript/app/components/backstagepass.ts b/TypeScript/app/components/backstagepass.ts new file mode 100644 index 00000000..49c29f5a --- /dev/null +++ b/TypeScript/app/components/backstagepass.ts @@ -0,0 +1,16 @@ +import { Item } from "./Item"; + +export class BackstagePass extends Item { + updateQuality() { + super.updateQuality(); + if (this.sellIn < 0) { + this.quality = 0; + } else if (this.sellIn <= 5) { + this.quality = Math.min(this.quality + 3, 50); + } else if (this.sellIn <= 10) { + this.quality = Math.min(this.quality + 2, 50); + } else { + this.quality = Math.min(this.quality + 1, 50); + } + } +} \ No newline at end of file diff --git a/TypeScript/app/components/conjured.ts b/TypeScript/app/components/conjured.ts new file mode 100644 index 00000000..f320c889 --- /dev/null +++ b/TypeScript/app/components/conjured.ts @@ -0,0 +1,8 @@ +import { Item } from "./Item"; + +export class Conjured extends Item { + updateQuality() { + super.updateQuality(); + this.quality = Math.max(this.quality - 2, 0); + } +} \ No newline at end of file diff --git a/TypeScript/app/components/sulfuras.ts b/TypeScript/app/components/sulfuras.ts new file mode 100644 index 00000000..08f5b1ef --- /dev/null +++ b/TypeScript/app/components/sulfuras.ts @@ -0,0 +1,12 @@ +import { ItemConstants } from "@/constants/constant"; +import { Item } from "./Item"; + +export class Sulfuras extends Item { + constructor() { + super(ItemConstants.SULFURAS, 0, 80); + } + + updateQuality() { + // Sulfuras quality never changes + } +} \ No newline at end of file diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index f32c96c8..9810e333 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -1,6 +1,11 @@ import { Item } from "./components/Item"; +import { AgedBrie } from "./components/aged-brie"; +import { BackstagePass } from "./components/backstagepass"; +import { Conjured } from "./components/conjured"; +import { Sulfuras } from "./components/sulfuras"; import { ItemConstants } from "./constants/constant"; + export class GildedRose { items: Array; @@ -8,110 +13,36 @@ export class GildedRose { this.items = items; } + /** + * Function to create item + * @param name + * @param sellIn + * @param quality + * @returns + */ + static createItem(name: string, sellIn: number, quality: number): Item { + switch (name) { + case ItemConstants.AGED_BRIE: + return new AgedBrie(name, sellIn, quality); + case ItemConstants.BACKSTAGE: + return new BackstagePass(name, sellIn, quality); + case ItemConstants.SULFURAS: + return new Sulfuras(); + case ItemConstants.CONJURED: + return new Conjured(name, sellIn, quality); + default: + return new Item(name, sellIn, quality); + } + } + /** * Function to update quality */ - updateQuality(){ - for (let i = 0; i < this.items.length; i++) { - const item = this.items[i]; - - // Check for specific item types and update quality accordingly - switch (item.name) { - case ItemConstants.AGED_BRIE: - item.sellIn--; - - //"Aged Brie" actually increases in Quality the older it gets - item.quality = Math.min(item.quality + 1, ItemConstants.MAX_QUALITY); - break; - - case ItemConstants.BACKSTAGE: - item.sellIn--; - - // The Quality of an item is never negative - if (item.sellIn < 0) { - item.quality = 0; - } else if (item.sellIn <= 5) { - // by 3 when there are 5 days or less but - item.quality = Math.min(item.quality + 3, ItemConstants.MAX_QUALITY); - } else if (item.sellIn <= 10) { - // Quality increases by 2 when there are 10 days or less - item.quality = Math.min(item.quality + 2, ItemConstants.MAX_QUALITY); - } else { - item.quality = Math.min(item.quality + 1, ItemConstants.MAX_QUALITY); - } - break; - case ItemConstants.SULFURAS: - // "Sulfuras", being a legendary item, never has to be sold or decreases in Quality - break; - case ItemConstants.CONJURED: - item.sellIn--; - - // "Conjured" items degrade in Quality twice as fast as normal items - item.quality = Math.max(item.quality - 2, 0); - break; - default: - // Normal item - item.sellIn--; - item.quality = Math.max(item.quality - 1, 0); - - // The Quality of an item is never negative - if (item.sellIn < 0) { - item.quality = Math.max(item.quality - 1, 0); - } - break; - } - } + updateQuality() { + for (const item of this.items) { + item.updateQuality(); + } return this.items; } - - // updateQualityOld() { - // for (let i = 0; i < this.items.length; i++) { - // if (this.items[i].name != ItemConstants.AGED_BRIE && this.items[i].name != ItemConstants.BACKSTAGE) { - // if (this.items[i].quality > 0) { - // if (this.items[i].name != ItemConstants.SULFURAS) { - // 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 == ItemConstants.BACKSTAGE) { - // 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 - // } - // } - // } - // } - // } - // if (this.items[i].name != ItemConstants.SULFURAS) { - // this.items[i].sellIn = this.items[i].sellIn - 1; - // } - // if (this.items[i].sellIn < 0) { - // if (this.items[i].name != ItemConstants.AGED_BRIE) { - // if (this.items[i].name != ItemConstants.BACKSTAGE) { - // if (this.items[i].quality > 0) { - // if (this.items[i].name != ItemConstants.SULFURAS) { - // 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 - // } - // } - // } - // } - - // return this.items; - // } }