Moved Aged Brie logic to AgedBrieBehavior

This commit is contained in:
Arno Chauveau 2025-07-24 11:50:17 +02:00
parent 1563d86e90
commit 372111147f
3 changed files with 34 additions and 17 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;