mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
62 lines
1.3 KiB
JavaScript
62 lines
1.3 KiB
JavaScript
class Item {
|
|
constructor(name, sellIn, quality) {
|
|
this.name = name;
|
|
this.sellIn = sellIn;
|
|
this.quality = quality;
|
|
}
|
|
}
|
|
|
|
class Shop {
|
|
constructor(items = []) {
|
|
this.items = items;
|
|
}
|
|
increaseQuality(item) {
|
|
if (item.quality < 50) {
|
|
item.quality++;
|
|
}
|
|
return item;
|
|
}
|
|
decreaseQuality(item) {
|
|
if (item.quality > 0) {
|
|
if (item.sellIn < 0 || item.name === 'Conjured Mana Cake') {
|
|
item.quality = item.quality - 2 > 0 ? item.quality - 2 : 0;
|
|
} else {
|
|
item.quality--;
|
|
}
|
|
}
|
|
return item;
|
|
}
|
|
updateQuality() {
|
|
this.items.map(item => {
|
|
switch (item.name) {
|
|
case 'Aged Brie':
|
|
this.increaseQuality(item);
|
|
item.sellIn--;
|
|
break;
|
|
case 'Backstage passes to a TAFKAL80ETC concert':
|
|
if (item.sellIn > 0) {
|
|
this.increaseQuality(item);
|
|
if (item.sellIn < 11) {
|
|
this.increaseQuality(item);
|
|
}
|
|
if (item.sellIn < 6) {
|
|
this.increaseQuality(item);
|
|
}
|
|
} else {
|
|
item.quality = 0;
|
|
}
|
|
item.sellIn--;
|
|
break;
|
|
case 'Sulfuras, Hand of Ragnaros':
|
|
item.sellIn--;
|
|
break;
|
|
|
|
default:
|
|
this.decreaseQuality(item);
|
|
item.sellIn--;
|
|
}
|
|
});
|
|
return this.items;
|
|
}
|
|
}
|