From bc2126c1c528d210d774911dc355dbfb33e6c061 Mon Sep 17 00:00:00 2001 From: Audrius Baltusis Date: Tue, 8 Sep 2020 21:26:45 +0300 Subject: [PATCH] Fixed Conjoured items --- js-jasmine/src/gilded_rose.js | 169 ++++++++++++++++++++++------------ 1 file changed, 112 insertions(+), 57 deletions(-) diff --git a/js-jasmine/src/gilded_rose.js b/js-jasmine/src/gilded_rose.js index acb33985..40de0fe7 100644 --- a/js-jasmine/src/gilded_rose.js +++ b/js-jasmine/src/gilded_rose.js @@ -1,66 +1,121 @@ class Item { - constructor(name, sellIn, quality){ - this.name = name; - this.sellIn = sellIn; - this.quality = quality; - } + constructor(name, sellIn, quality) { + this.name = name; + this.sellIn = sellIn; + this.quality = quality; + } +} + +class StandardItem extends Item { + itemTomorrow() { + this.quality = this.qualityTomorrow(); + this.sellIn--; + return this; + } + + qualityTomorrow() { + let qualityTomorrow = this.quality - this.calculateDepreciation(); + if (qualityTomorrow < 0) { + return 0; + } + if (qualityTomorrow >= 50) { + return 50; + } + return qualityTomorrow; + } + + calculateDepreciation() { + return this.sellIn <= 0 ? 2 : 1; + } +} + +class Sulfuras extends Item { + itemTomorrow() { + return this; + } +} + +class ConjuredItem extends StandardItem { + calculateDepreciation() { + return this.sellIn <= 0 ? 4 : 2; + } +} + +class BackStagePass extends StandardItem { + calculateDepreciation() { + switch (true) { + case this.sellIn <= 0: + return this.quality; + case this.sellIn <= 5: + return -3; + case this.sellIn <= 10: + return -2; + default: + return -1; + } + } +} + +class AgedBrie extends StandardItem { + calculateDepreciation() { + return this.sellIn <= 0 ? -2 : -1; + } +} + +class ItemTypeFactory { + constructor() { + this.setupItemTypes(); + this.setupClasses(); + } + + setupItemTypes() { + this.itemTypes = new Map(); + this.itemTypes.set('Aged Brie', 'AgedBrie'); + this.itemTypes.set('Backstage passes', 'BackStagePass'); + this.itemTypes.set('Conjured', 'ConjuredItem'); + this.itemTypes.set('Sulfuras', 'Sulfuras'); + this.itemTypes.set('Standard', 'StandardItem'); + } + + setupClasses() { + this.availableClasses = { + StandardItem, + Sulfuras, + ConjuredItem, + BackStagePass, + AgedBrie + }; + } + + getClassName(name) { + const itemType = [...this.itemTypes] + .filter(([key, value]) => name.includes(key)) + .map(([key, value]) => key)[0]; + return this.itemTypes.get(itemType || this.defaultClassName); + } + + createItemUsingType(item) { + const {name, sellIn, quality} = item; + return new this.availableClasses[this.getClassName(name)](name, sellIn, quality); + } + + get defaultClassName() { + return 'Standard'; + } } class Shop { - constructor(items=[]){ - this.items = items; - } - updateQuality() { - for (var 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; - } - } - } 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; - } - } - } - } - } - 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; - } - } - } + constructor(items = []) { + const itemTypeFactory = new ItemTypeFactory(); + this.items = items.map((item) => itemTypeFactory.createItemUsingType(item)); } - return this.items; - } + updateQuality() { + return this.items.map((item) => item.itemTomorrow()); + } } + module.exports = { - Item, - Shop + Item, + Shop }