From 47a41e4a5572f73d59c140396121de9f682a106c Mon Sep 17 00:00:00 2001 From: Arpita Saha Date: Thu, 23 Dec 2021 19:35:15 -0500 Subject: [PATCH] completed the code --- js-jest/src/gilded_rose.js | 92 ++++++++++++++++++++++++++++++-- js-jest/test/texttest_fixture.js | 2 +- 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index 0fac2b1e..caf43447 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -6,9 +6,10 @@ class Item { } } +//This class will hold the rules for the items. making it easier to accomodate future additions to the shop class Rules { constructor(dailyQualityChangeValue, maxQuality, minQuality, tenDayChange, fiveDayChange, - isZeroDayQualityDrop, zeroDayQualityValue) { + isZeroDayQualityDrop, zeroDayQualityValue, isSellInValueChangeAllowed, isConcert) { this.dailyQualityChangeValue = dailyQualityChangeValue; this.maxQuality = maxQuality; this.minQuality = minQuality; @@ -16,6 +17,8 @@ class Rules { this.fiveDayChange = fiveDayChange; this.isZeroDayQualityDrop = isZeroDayQualityDrop; this.zeroDayQualityValue = zeroDayQualityValue; + this.isSellInValueChangeAllowed = isSellInValueChangeAllowed; + this.isConcert = isConcert; } } @@ -25,11 +28,90 @@ class Shop { } - updateQuality() { + updateQualityNew() { const rules = { - 'Aged Brie': new Rules(1, 50, 0, 1, 1), - 'Backstage passes to a TAFKAL80ETC concert': new Rules(1, 50, 0) + 'Aged Brie': new Rules(1, 50, 0, 1, 1, false, 0, true, false), + 'Backstage passes to a TAFKAL80ETC concert': new Rules(1, 50, 0, 2, 3, true, 0, true, true), + 'Sulfuras, Hand of Ragnaros': new Rules(0, Infinity, 0, 0, 0, false, 0, false, false), + 'Conjured Mana Cake': new Rules(-2, 50, 0, -2, -2, false, 0, true, false), + 'DEFAULT': new Rules(-1, 50, 0, -1, -1, false, 0, true, false) } + //1. Iterate over the item list + for (let i = 0; i < this.items.length; i++) { + let itemName = this.items[i].name; + //2. Check rules object and fetch corresponding rules. If the item name is not found in rules use 'DEFAULT' rule + let itemRule; + if (rules[itemName]) { + itemRule = rules[itemName]; + } else { + itemRule = rules['DEFAULT']; + } + // console.log("Rules for item " + itemName + " is " + JSON.stringify(itemRule)); + this.applyRule(this.items[i], itemRule); + } + + return this.items; + } + + applyRule(itemObj, ruleObj) { + //Decrease the sellin value + if (itemObj.sellIn <= 0 && !ruleObj.isZeroDayQualityDrop) { + //if sell in day is less than 0 then add the quality by 2 times dailyChangeValue + //Not applicable for concert tickets + if (!itemObj.isConcert) { + itemObj.quality = itemObj.quality + 2 * ruleObj.dailyQualityChangeValue; + } + } else if (itemObj.sellIn <= 0 && ruleObj.isZeroDayQualityDrop) { + /* + if sell in day is 0 and quality drop is required on 0 day + then assign zero day value to quality + */ + itemObj.quality = ruleObj.zeroDayQualityValue; + + //Decrement sell in and return + if (ruleObj.isSellInValueChangeAllowed) { + itemObj.sellIn = itemObj.sellIn - 1; + } + return; + + } else if (itemObj.sellIn <= 5) { + /* + if sell in day is 5 or less then add fiveDayChange value to quality + */ + itemObj.quality = itemObj.quality + ruleObj.fiveDayChange; + } else if (itemObj.sellIn <= 10) { + /* + if sell in day is 10 or less then add tenDayChange value to quality + */ + itemObj.quality = itemObj.quality + ruleObj.tenDayChange; + } else { + /* + for any other sellIn day add dailyQualityChangeValue to quality + */ + itemObj.quality = itemObj.quality + ruleObj.dailyQualityChangeValue; + } + + //Check Quality value min and max limits and adjust + this.applyQualityMaxMinCheck(itemObj, ruleObj.minQuality, ruleObj.maxQuality) + + //decrease sell in value by 1 + if (ruleObj.isSellInValueChangeAllowed) { + itemObj.sellIn = itemObj.sellIn - 1; + } + + + } + + applyQualityMaxMinCheck(itemObj, minVal, maxVal) { + //Check and update items quality based on max and min values allowed + if (itemObj.quality < minVal) { + itemObj.quality = minVal; + } else if (itemObj.quality > maxVal) { + itemObj.quality = maxVal; + } + } + + updateQuality() { for (let 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) { @@ -78,6 +160,8 @@ class Shop { return this.items; } + + } module.exports = { diff --git a/js-jest/test/texttest_fixture.js b/js-jest/test/texttest_fixture.js index a62ede3f..13a4f139 100644 --- a/js-jest/test/texttest_fixture.js +++ b/js-jest/test/texttest_fixture.js @@ -23,5 +23,5 @@ for (let day = 0; day < days; day++) { console.log(`\n-------- day ${day} --------`); console.log("name, sellIn, quality"); items.forEach(item => console.log(`${item.name}, ${item.sellIn}, ${item.quality}`)); - gildedRose.updateQuality(); + gildedRose.updateQualityNew(); }