From 7395fd46dbafecad7c1cad9193c2b93e93f9a026 Mon Sep 17 00:00:00 2001 From: Pabasara Jayawardhana Date: Sun, 19 Sep 2021 10:20:52 +0530 Subject: [PATCH] Clean up the code --- js-jest/src/gilded_rose.js | 29 +------------------ js-jest/src/item_updater.js | 28 ++++++++++++++++++ .../src/item_updaters/aged_brie_updater.js | 2 +- .../item_updaters/backstage_passes_updater.js | 2 +- js-jest/src/item_updaters/conjured_updater.js | 2 +- js-jest/test/gilded_rose.test.js | 14 +++++++-- 6 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 js-jest/src/item_updater.js diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index 2f271971..cf8708dd 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -5,32 +5,6 @@ class Item { this.quality = quality; } } - -class ItemUpdater { - constructor(item) { - this.item = item; - this.qualityChangeFactor = 1; - } - - updateQuality() { - - //Once the sell by date has passed, Quality degrades twice as fast - if(this.item.sellIn < 0) { - this.qualityChangeFactor = 2; - } - - //The Quality of an item is never more than 50 - if (this.item.quality > 50) { - this.item.quality = 50; - } - - if (this.item.quality > 0 && this.item.quality < 50) { - this.item.quality = this.item.quality - this.qualityChangeFactor; - } - return this.item; - } -} - class Shop { constructor(itemUpdaters=[]){ this.itemUpdaters = itemUpdaters; @@ -50,6 +24,5 @@ class Shop { module.exports = { Item, - Shop, - ItemUpdater + Shop } diff --git a/js-jest/src/item_updater.js b/js-jest/src/item_updater.js new file mode 100644 index 00000000..a8313682 --- /dev/null +++ b/js-jest/src/item_updater.js @@ -0,0 +1,28 @@ +class ItemUpdater { + constructor(item) { + this.item = item; + this.qualityChangeFactor = 1; + } + + updateQuality() { + + //Once the sell by date has passed, Quality degrades twice as fast + if(this.item.sellIn < 0) { + this.qualityChangeFactor = 2; + } + + //The Quality of an item is never more than 50 + if (this.item.quality > 50) { + this.item.quality = 50; + } + + if (this.item.quality > 0 && this.item.quality < 50) { + this.item.quality = this.item.quality - this.qualityChangeFactor; + } + return this.item; + } + } + + module.exports = { + ItemUpdater + } \ No newline at end of file diff --git a/js-jest/src/item_updaters/aged_brie_updater.js b/js-jest/src/item_updaters/aged_brie_updater.js index ec343d35..059549fe 100644 --- a/js-jest/src/item_updaters/aged_brie_updater.js +++ b/js-jest/src/item_updaters/aged_brie_updater.js @@ -1,4 +1,4 @@ -const { ItemUpdater } = require("../gilded_rose"); +const { ItemUpdater } = require("../item_updater"); class AgedBrieUpdater extends ItemUpdater { updateQuality() { diff --git a/js-jest/src/item_updaters/backstage_passes_updater.js b/js-jest/src/item_updaters/backstage_passes_updater.js index 5aebe241..376085f6 100644 --- a/js-jest/src/item_updaters/backstage_passes_updater.js +++ b/js-jest/src/item_updaters/backstage_passes_updater.js @@ -1,4 +1,4 @@ -const { ItemUpdater } = require("../gilded_rose"); +const { ItemUpdater } = require("../item_updater"); class BackStagePassesUpdater extends ItemUpdater { updateQuality() { diff --git a/js-jest/src/item_updaters/conjured_updater.js b/js-jest/src/item_updaters/conjured_updater.js index 913f5605..25a594cc 100644 --- a/js-jest/src/item_updaters/conjured_updater.js +++ b/js-jest/src/item_updaters/conjured_updater.js @@ -1,4 +1,4 @@ -const { ItemUpdater } = require("../gilded_rose"); +const { ItemUpdater } = require("../item_updater"); class ConjuredUpdater extends ItemUpdater { updateQuality() { diff --git a/js-jest/test/gilded_rose.test.js b/js-jest/test/gilded_rose.test.js index 1b654898..27c4d9b6 100644 --- a/js-jest/test/gilded_rose.test.js +++ b/js-jest/test/gilded_rose.test.js @@ -1,8 +1,9 @@ -const {Shop, Item, ItemUpdater} = require("../src/gilded_rose"); +const {Shop, Item} = require("../src/gilded_rose"); const {AgedBrieUpdater} = require("../src/item_updaters/aged_brie_updater"); const {SulfurasUpdater } = require("../src/item_updaters/sulfuras_updater"); const {BackStagePassesUpdater } = require("../src/item_updaters/backstage_passes_updater"); const {ConjuredUpdater } = require("../src/item_updaters/conjured_updater"); +const {ItemUpdater } = require("../src/item_updater"); describe("Gilded Rose check common rules", function () { it("Item quality should decrease by one", function () { @@ -86,12 +87,19 @@ describe("Gilded Rose check Conjured rules", function () { }); describe("Gilded Rose system test", function() { - it("should check list of itemUpater", function() { + it("should check list of itemUpaters", function () { const itemUpdaters = [ new ItemUpdater(new Item("+5 Dexterity Vest", 10, 20)), new AgedBrieUpdater(new Item("Aged Brie", 2, 0)), + new ItemUpdater(new Item("Elixir of the Mongoose", 5, 7)), + new SulfurasUpdater(new Item("Sulfuras, Hand of Ragnaros", 0, 80)), + new SulfurasUpdater(new Item("Sulfuras, Hand of Ragnaros", -1, 80)), + new BackStagePassesUpdater(new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20)), + new BackStagePassesUpdater(new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49)), + new BackStagePassesUpdater(new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49)), + new ConjuredUpdater(new Item("Conjured Mana Cake", 3, 6)), ]; - + const days = Number(process.argv[2]) || 2; const gildedRose = new Shop(itemUpdaters);