mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-05-19 16:21:55 +00:00
Lazy implementation solution
This commit is contained in:
parent
3e0085bfd0
commit
3cb923056f
@ -1,62 +1,89 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
class GildedRose {
|
import java.util.Date;
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class GildedRose {
|
||||||
|
|
||||||
|
public static final String AGED_BRIE = "Aged Brie";
|
||||||
|
public static final String CONCERT = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
|
public static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||||
|
public static final String CONJURED = "Conjured";
|
||||||
|
public static final int MIN_QUALITY = 0;
|
||||||
|
public static final int MAX_QUALITY = 50;
|
||||||
|
|
||||||
|
public static final int CONCERT_HOTTEST_DAYS = 6;
|
||||||
|
public static final int CONCERT_HOT_DAYS = 11;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
public GildedRose(Item[] items) {
|
public GildedRose(Item[] items) {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
//"Aged Brie" and "Backstage passes":
|
||||||
for (int i = 0; i < items.length; i++) {
|
protected boolean isImproving(Item item) {
|
||||||
if (!items[i].name.equals("Aged Brie")
|
return item.name.equals(AGED_BRIE) || item.name.equals(CONCERT);
|
||||||
&& !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
|
||||||
if (items[i].quality > 0) {
|
|
||||||
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
|
||||||
items[i].quality = items[i].quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1;
|
|
||||||
|
|
||||||
if (items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
|
||||||
if (items[i].sellIn < 11) {
|
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items[i].sellIn < 6) {
|
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
|
||||||
items[i].sellIn = items[i].sellIn - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items[i].sellIn < 0) {
|
|
||||||
if (!items[i].name.equals("Aged Brie")) {
|
|
||||||
if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
|
||||||
if (items[i].quality > 0) {
|
|
||||||
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
|
||||||
items[i].quality = items[i].quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
items[i].quality = items[i].quality - items[i].quality;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//"Normal" and "Conjured" items:
|
||||||
|
protected boolean isDegrading(Item item) {
|
||||||
|
return !item.name.equals(SULFURAS) && !isImproving(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// public void updateQuality() {
|
||||||
|
// for (int i = 0; i < items.length; i++) {
|
||||||
|
// if (!items[i].name.equals("Aged Brie")
|
||||||
|
// && !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||||
|
// if (items[i].quality > 0) {
|
||||||
|
// if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||||
|
// items[i].quality = items[i].quality - 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// if (items[i].quality < 50) {
|
||||||
|
// items[i].quality = items[i].quality + 1;
|
||||||
|
//
|
||||||
|
// if (items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||||
|
// if (items[i].sellIn < 11) {
|
||||||
|
// if (items[i].quality < 50) {
|
||||||
|
// items[i].quality = items[i].quality + 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (items[i].sellIn < 6) {
|
||||||
|
// if (items[i].quality < 50) {
|
||||||
|
// items[i].quality = items[i].quality + 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||||
|
// items[i].sellIn = items[i].sellIn - 1;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (items[i].sellIn < 0) {
|
||||||
|
// if (!items[i].name.equals("Aged Brie")) {
|
||||||
|
// if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||||
|
// if (items[i].quality > 0) {
|
||||||
|
// if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||||
|
// items[i].quality = items[i].quality - 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// items[i].quality = items[i].quality - items[i].quality;
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// if (items[i].quality < 50) {
|
||||||
|
// items[i].quality = items[i].quality + 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
70
Java/src/main/java/com/gildedrose/LazyGildedRose.java
Normal file
70
Java/src/main/java/com/gildedrose/LazyGildedRose.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package java.com.gildedrose;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
/* If the amout of items is substantially bigger than day-to-day item removing/adding,
|
||||||
|
we can calculate item quality on the fly,
|
||||||
|
instead of constantly decrementing every item quality, by using timestamps:
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class LazyGildedRose extends com.gildedrose.GildedRose {
|
||||||
|
|
||||||
|
Long startUnixTime;
|
||||||
|
|
||||||
|
public LazyGildedRose(Item[] items) {
|
||||||
|
super(items);
|
||||||
|
this.startUnixTime = System.currentTimeMillis()/1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Adjust item quality as if item was placed in the store from day one:
|
||||||
|
private int getDaysSinceStart() {
|
||||||
|
var timePassed = System.currentTimeMillis() / 1000 - startUnixTime;
|
||||||
|
return Math.toIntExact(timePassed) / 24 / 3600;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void adjustItemQuality(Item item, int daysPassed) {
|
||||||
|
switch(item.name) {
|
||||||
|
case AGED_BRIE, CONCERT: item.quality += daysPassed;
|
||||||
|
break;
|
||||||
|
case SULFURAS:;
|
||||||
|
break;
|
||||||
|
case CONJURED: item.quality -= daysPassed * 2;
|
||||||
|
break;
|
||||||
|
default: item.quality -= daysPassed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(Item item) {
|
||||||
|
var daysPassedSinceStart = getDaysSinceStart();
|
||||||
|
adjustItemQuality(item, -daysPassedSinceStart);
|
||||||
|
item.sellIn += daysPassedSinceStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sellItem(Item item) {
|
||||||
|
var daysPassedSinceStart = getDaysSinceStart();
|
||||||
|
adjustItemQuality(item, daysPassedSinceStart);
|
||||||
|
|
||||||
|
var daysLeft = daysPassedSinceStart - item.sellIn;
|
||||||
|
if(item.name.equals(CONCERT)) {
|
||||||
|
if(daysLeft < 0) {
|
||||||
|
item.quality = 0;
|
||||||
|
} else {
|
||||||
|
adjustItemQuality(item, Math.max(CONCERT_HOT_DAYS - daysLeft, 0) + Math.max(CONCERT_HOTTEST_DAYS - daysLeft, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//For degrading and outdated items, adjust quality one more time:
|
||||||
|
else if(isDegrading(item) && daysPassedSinceStart > item.sellIn) {
|
||||||
|
adjustItemQuality(item, daysLeft);
|
||||||
|
}
|
||||||
|
normalizeQuality(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void normalizeQuality(Item item) {
|
||||||
|
if (item.quality < MIN_QUALITY) {
|
||||||
|
item.quality = MIN_QUALITY;
|
||||||
|
}
|
||||||
|
if (item.quality > MAX_QUALITY) {
|
||||||
|
item.quality = MAX_QUALITY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user