From e19e99800c2ed8a0dbc7f1500c37bda565c3df9b Mon Sep 17 00:00:00 2001 From: kiwioyster Date: Thu, 22 Jun 2023 12:24:31 +1200 Subject: [PATCH] refactor business logic, add test to verify backstage item quality limit --- TypeScript/app/gilded-rose.ts | 115 ++++++++++++++--------- TypeScript/test/jest/gilded-rose.spec.ts | 6 ++ 2 files changed, 75 insertions(+), 46 deletions(-) diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index db58d678..b08d6c73 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -3,7 +3,7 @@ export class Item { sellIn: number; quality: number; - constructor(name, sellIn, quality) { + constructor(name: string, sellIn: number, quality: number) { this.name = name; this.sellIn = sellIn; this.quality = quality; @@ -11,6 +11,7 @@ export class Item { } export class GildedRose { + private QUALITY_LIM = 50; items: Array; constructor(items = [] as Array) { @@ -18,52 +19,74 @@ export class GildedRose { } 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 - } - } + return this.items.map((item) => { + if (this._isAgeable(item.name)) { + return this._decayAgeableItem(item); + } else if (this._isLegendary(item.name)) { + return this._decayLegendaryItem(item); + } else if (this._isBackstagePass(item.name)) { + return this._decayBackstagePassItem(item); } 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 - } - } - } - } + return this._decayGeneralItem(item); } - 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 - } - } - } - } - - return this.items; + }); } + + private _isAgeable = (name: string): boolean => name === "Aged Brie"; + + private _isLegendary = (name: string): boolean => + name === "Sulfuras, Hand of Ragnaros"; + + private _isBackstagePass = (name: string): boolean => + name === "Backstage passes to a TAFKAL80ETC concert"; + + private _decayGeneralItem = (item: Item): Item => { + const qualityDecay = item.sellIn > 0 ? 1 : 2; + return { + name: item.name, + quality: item.quality > 0 ? item.quality - qualityDecay : 0, + sellIn: item.sellIn - 1, + }; + }; + + private _decayLegendaryItem = (item: Item): Item => { + return item; + }; + + private _decayAgeableItem = (item: Item): Item => { + const newItem = { + name: item.name, + quality: item.quality + 1, + sellIn: item.sellIn - 1, + }; + return this._applyQualityLimitation(newItem); + }; + + private _decayBackstagePassItem = (item: Item): Item => { + let newQuality; + if (item.sellIn > 10) { + newQuality = item.quality + 1; + } else if (item.sellIn > 5) { + newQuality = item.quality + 2; + } else if (item.sellIn > 0) { + newQuality = item.quality + 3; + } else { + newQuality = 0; + } + const newItem = { + name: item.name, + quality: newQuality, + sellIn: item.sellIn - 1, + }; + return this._applyQualityLimitation(newItem); + }; + + private _applyQualityLimitation = (item: Item): Item => { + return { + name: item.name, + quality: + item.quality > this.QUALITY_LIM ? this.QUALITY_LIM : item.quality, + sellIn: item.sellIn, + }; + }; } diff --git a/TypeScript/test/jest/gilded-rose.spec.ts b/TypeScript/test/jest/gilded-rose.spec.ts index 47d3ca5b..fcd72510 100644 --- a/TypeScript/test/jest/gilded-rose.spec.ts +++ b/TypeScript/test/jest/gilded-rose.spec.ts @@ -95,5 +95,11 @@ describe("Gilded Rose", () => { expect(items[0].sellIn).toBe(-1); expect(items[0].quality).toBe(0); }); + it("should not exceed 50 quality", () => { + const gildedRose = new GildedRose([backstagePassItem(5, 50)]); + const items = gildedRose.updateQuality(); + expect(items[0].sellIn).toBe(4); + expect(items[0].quality).toBe(50); + }); }); });