diff --git a/TypeScript/app/update-behaviors/behavior-resolver.ts b/TypeScript/app/update-behaviors/behavior-resolver.ts index 5784f007..93a99297 100644 --- a/TypeScript/app/update-behaviors/behavior-resolver.ts +++ b/TypeScript/app/update-behaviors/behavior-resolver.ts @@ -1,9 +1,12 @@ import { Item } from "@app/item"; import { IUpdateBehavior } from "./update-behavior.interface"; -import { LegacyBehavior } from "./legacy-behavior"; +import { LegacyBehavior } from "./implementations/legacy-behavior"; +import { AgedBrieBehavior } from "./implementations/aged-brie-behavior"; export function getUpdateBehaviorFor(item: Item): IUpdateBehavior { switch (item.name) { + case "Aged Brie": + return new AgedBrieBehavior(item); default: return new LegacyBehavior(item); } diff --git a/TypeScript/app/update-behaviors/implementations/aged-brie-behavior.ts b/TypeScript/app/update-behaviors/implementations/aged-brie-behavior.ts new file mode 100644 index 00000000..a21e1a50 --- /dev/null +++ b/TypeScript/app/update-behaviors/implementations/aged-brie-behavior.ts @@ -0,0 +1,23 @@ +import { Item } from "@app/item"; +import { IUpdateBehavior } from "../update-behavior.interface"; + +export class AgedBrieBehavior implements IUpdateBehavior { + readonly #MAX_AMOUNT = 50; + + constructor(private item: Item) {} + + update(): Item { + this.item.sellIn -= 1; + + const isPastSellInDay = this.item.sellIn < 0; + + const amountToAdd = isPastSellInDay ? 2 : 1; + + this.item.quality = Math.min( + this.#MAX_AMOUNT, + this.item.quality + amountToAdd + ); + + return this.item; + } +} diff --git a/TypeScript/app/update-behaviors/legacy-behavior.ts b/TypeScript/app/update-behaviors/implementations/legacy-behavior.ts similarity index 61% rename from TypeScript/app/update-behaviors/legacy-behavior.ts rename to TypeScript/app/update-behaviors/implementations/legacy-behavior.ts index e9fa304a..5d140fee 100644 --- a/TypeScript/app/update-behaviors/legacy-behavior.ts +++ b/TypeScript/app/update-behaviors/implementations/legacy-behavior.ts @@ -1,14 +1,11 @@ import { Item } from "@app/item"; -import { IUpdateBehavior } from "./update-behavior.interface"; +import { IUpdateBehavior } from "../update-behavior.interface"; export class LegacyBehavior implements IUpdateBehavior { constructor(private item: Item) {} update(): Item { - if ( - this.item.name !== "Aged Brie" && - this.item.name !== "Backstage passes to a TAFKAL80ETC concert" - ) { + if (this.item.name !== "Backstage passes to a TAFKAL80ETC concert") { if (this.item.quality > 0) { if (this.item.name !== "Sulfuras, Hand of Ragnaros") { this.item.quality = this.item.quality - 1; @@ -35,20 +32,14 @@ export class LegacyBehavior implements IUpdateBehavior { this.item.sellIn = this.item.sellIn - 1; } if (this.item.sellIn < 0) { - if (this.item.name !== "Aged Brie") { - if (this.item.name !== "Backstage passes to a TAFKAL80ETC concert") { - if (this.item.quality > 0) { - if (this.item.name !== "Sulfuras, Hand of Ragnaros") { - this.item.quality = this.item.quality - 1; - } + if (this.item.name !== "Backstage passes to a TAFKAL80ETC concert") { + if (this.item.quality > 0) { + if (this.item.name !== "Sulfuras, Hand of Ragnaros") { + this.item.quality = this.item.quality - 1; } - } else { - this.item.quality = this.item.quality - this.item.quality; } } else { - if (this.item.quality < 50) { - this.item.quality = this.item.quality + 1; - } + this.item.quality = this.item.quality - this.item.quality; } } return this.item;