mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-20 00:41:07 +00:00
completed the code
This commit is contained in:
parent
2454d1d0e8
commit
47a41e4a55
@ -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 {
|
class Rules {
|
||||||
constructor(dailyQualityChangeValue, maxQuality, minQuality, tenDayChange, fiveDayChange,
|
constructor(dailyQualityChangeValue, maxQuality, minQuality, tenDayChange, fiveDayChange,
|
||||||
isZeroDayQualityDrop, zeroDayQualityValue) {
|
isZeroDayQualityDrop, zeroDayQualityValue, isSellInValueChangeAllowed, isConcert) {
|
||||||
this.dailyQualityChangeValue = dailyQualityChangeValue;
|
this.dailyQualityChangeValue = dailyQualityChangeValue;
|
||||||
this.maxQuality = maxQuality;
|
this.maxQuality = maxQuality;
|
||||||
this.minQuality = minQuality;
|
this.minQuality = minQuality;
|
||||||
@ -16,6 +17,8 @@ class Rules {
|
|||||||
this.fiveDayChange = fiveDayChange;
|
this.fiveDayChange = fiveDayChange;
|
||||||
this.isZeroDayQualityDrop = isZeroDayQualityDrop;
|
this.isZeroDayQualityDrop = isZeroDayQualityDrop;
|
||||||
this.zeroDayQualityValue = zeroDayQualityValue;
|
this.zeroDayQualityValue = zeroDayQualityValue;
|
||||||
|
this.isSellInValueChangeAllowed = isSellInValueChangeAllowed;
|
||||||
|
this.isConcert = isConcert;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,11 +28,90 @@ class Shop {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
updateQuality() {
|
updateQualityNew() {
|
||||||
const rules = {
|
const rules = {
|
||||||
'Aged Brie': new Rules(1, 50, 0, 1, 1),
|
'Aged Brie': new Rules(1, 50, 0, 1, 1, false, 0, true, false),
|
||||||
'Backstage passes to a TAFKAL80ETC concert': new Rules(1, 50, 0)
|
'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++) {
|
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].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
|
||||||
if (this.items[i].quality > 0) {
|
if (this.items[i].quality > 0) {
|
||||||
@ -78,6 +160,8 @@ class Shop {
|
|||||||
|
|
||||||
return this.items;
|
return this.items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@ -23,5 +23,5 @@ for (let day = 0; day < days; day++) {
|
|||||||
console.log(`\n-------- day ${day} --------`);
|
console.log(`\n-------- day ${day} --------`);
|
||||||
console.log("name, sellIn, quality");
|
console.log("name, sellIn, quality");
|
||||||
items.forEach(item => console.log(`${item.name}, ${item.sellIn}, ${item.quality}`));
|
items.forEach(item => console.log(`${item.name}, ${item.sellIn}, ${item.quality}`));
|
||||||
gildedRose.updateQuality();
|
gildedRose.updateQualityNew();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user