mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Clean up the code
This commit is contained in:
parent
577e92e699
commit
7395fd46db
@ -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
|
||||
}
|
||||
|
||||
28
js-jest/src/item_updater.js
Normal file
28
js-jest/src/item_updater.js
Normal file
@ -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
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
const { ItemUpdater } = require("../gilded_rose");
|
||||
const { ItemUpdater } = require("../item_updater");
|
||||
|
||||
class AgedBrieUpdater extends ItemUpdater {
|
||||
updateQuality() {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const { ItemUpdater } = require("../gilded_rose");
|
||||
const { ItemUpdater } = require("../item_updater");
|
||||
|
||||
class BackStagePassesUpdater extends ItemUpdater {
|
||||
updateQuality() {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const { ItemUpdater } = require("../gilded_rose");
|
||||
const { ItemUpdater } = require("../item_updater");
|
||||
|
||||
class ConjuredUpdater extends ItemUpdater {
|
||||
updateQuality() {
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user