mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-10 12:11:20 +00:00
Moved Aged Brie logic to AgedBrieBehavior
This commit is contained in:
parent
1563d86e90
commit
372111147f
@ -1,9 +1,12 @@
|
|||||||
import { Item } from "@app/item";
|
import { Item } from "@app/item";
|
||||||
import { IUpdateBehavior } from "./update-behavior.interface";
|
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 {
|
export function getUpdateBehaviorFor(item: Item): IUpdateBehavior {
|
||||||
switch (item.name) {
|
switch (item.name) {
|
||||||
|
case "Aged Brie":
|
||||||
|
return new AgedBrieBehavior(item);
|
||||||
default:
|
default:
|
||||||
return new LegacyBehavior(item);
|
return new LegacyBehavior(item);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,14 +1,11 @@
|
|||||||
import { Item } from "@app/item";
|
import { Item } from "@app/item";
|
||||||
import { IUpdateBehavior } from "./update-behavior.interface";
|
import { IUpdateBehavior } from "../update-behavior.interface";
|
||||||
|
|
||||||
export class LegacyBehavior implements IUpdateBehavior {
|
export class LegacyBehavior implements IUpdateBehavior {
|
||||||
constructor(private item: Item) {}
|
constructor(private item: Item) {}
|
||||||
|
|
||||||
update(): Item {
|
update(): Item {
|
||||||
if (
|
if (this.item.name !== "Backstage passes to a TAFKAL80ETC concert") {
|
||||||
this.item.name !== "Aged Brie" &&
|
|
||||||
this.item.name !== "Backstage passes to a TAFKAL80ETC concert"
|
|
||||||
) {
|
|
||||||
if (this.item.quality > 0) {
|
if (this.item.quality > 0) {
|
||||||
if (this.item.name !== "Sulfuras, Hand of Ragnaros") {
|
if (this.item.name !== "Sulfuras, Hand of Ragnaros") {
|
||||||
this.item.quality = this.item.quality - 1;
|
this.item.quality = this.item.quality - 1;
|
||||||
@ -35,20 +32,14 @@ export class LegacyBehavior implements IUpdateBehavior {
|
|||||||
this.item.sellIn = this.item.sellIn - 1;
|
this.item.sellIn = this.item.sellIn - 1;
|
||||||
}
|
}
|
||||||
if (this.item.sellIn < 0) {
|
if (this.item.sellIn < 0) {
|
||||||
if (this.item.name !== "Aged Brie") {
|
if (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.quality > 0) {
|
if (this.item.name !== "Sulfuras, Hand of Ragnaros") {
|
||||||
if (this.item.name !== "Sulfuras, Hand of Ragnaros") {
|
this.item.quality = this.item.quality - 1;
|
||||||
this.item.quality = this.item.quality - 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.item.quality = this.item.quality - this.item.quality;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.item.quality < 50) {
|
this.item.quality = this.item.quality - this.item.quality;
|
||||||
this.item.quality = this.item.quality + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.item;
|
return this.item;
|
||||||
Loading…
Reference in New Issue
Block a user