mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-05 09:41:37 +00:00
Add conjured item functionality
This commit is contained in:
parent
6d7bec3f73
commit
1c99c4d55e
@ -0,0 +1,9 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class AgedBrieItemUpdater extends ItemUpdater {
|
||||
|
||||
@Override
|
||||
int getIncrementValue() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class BackstagePassesItemUpdater extends ItemUpdater {
|
||||
|
||||
@Override
|
||||
public void update(Item item) {
|
||||
updateQuality(item, getIncrementValue());
|
||||
if (item.sellIn <= 10) {
|
||||
updateQuality(item, getIncrementValue());
|
||||
}
|
||||
if (item.sellIn <= 5) {
|
||||
updateQuality(item, getIncrementValue());
|
||||
}
|
||||
updateSellIn(item);
|
||||
if (isExpired(item)) {
|
||||
item.quality = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
int getIncrementValue() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class ConjuredItemUpdater extends ItemUpdater {
|
||||
|
||||
@Override
|
||||
int getIncrementValue() {
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
@ -8,55 +8,9 @@ class GildedRose {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Item item : items) {
|
||||
ItemUpdater itemUpdater = ItemUpdaterFactory.getUpdater(item);
|
||||
itemUpdater.update(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
31
Java/src/main/java/com/gildedrose/ItemUpdater.java
Normal file
31
Java/src/main/java/com/gildedrose/ItemUpdater.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
import static java.lang.Math.min;
|
||||
|
||||
public abstract class ItemUpdater {
|
||||
public static final int QUALITY_MIN = 0;
|
||||
public static final int QUALITY_MAX = 50;
|
||||
|
||||
protected void update(Item item){
|
||||
updateQuality(item, getIncrementValue());
|
||||
updateSellIn(item);
|
||||
if (isExpired(item)) {
|
||||
updateQuality(item, getIncrementValue());
|
||||
}
|
||||
}
|
||||
|
||||
abstract int getIncrementValue();
|
||||
|
||||
boolean isExpired(Item item) {
|
||||
return item.sellIn < 0;
|
||||
}
|
||||
|
||||
void updateQuality(Item item, int increment){
|
||||
item.quality = max(QUALITY_MIN, min(QUALITY_MAX, item.quality + increment));
|
||||
}
|
||||
|
||||
final void updateSellIn(Item item) {
|
||||
item.sellIn = (int) max(Integer.MIN_VALUE, min(Integer.MAX_VALUE, ((long) item.sellIn) - 1));
|
||||
}
|
||||
}
|
||||
23
Java/src/main/java/com/gildedrose/ItemUpdaterFactory.java
Normal file
23
Java/src/main/java/com/gildedrose/ItemUpdaterFactory.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public final class ItemUpdaterFactory {
|
||||
|
||||
private ItemUpdaterFactory() {
|
||||
throw new UnsupportedOperationException("Class cannot be instantiated");
|
||||
}
|
||||
|
||||
public static ItemUpdater getUpdater(Item item) {
|
||||
switch (item.name) {
|
||||
case "Aged Brie":
|
||||
return new AgedBrieItemUpdater();
|
||||
case "Backstage passes to a TAFKAL80ETC concert":
|
||||
return new BackstagePassesItemUpdater();
|
||||
case "Sulfuras, Hand of Ragnaros":
|
||||
return new SulfurasItemUpdater();
|
||||
case "Conjured Mana Cake":
|
||||
return new ConjuredItemUpdater();
|
||||
default:
|
||||
return new RegularItemUpdater();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class RegularItemUpdater extends ItemUpdater {
|
||||
|
||||
@Override
|
||||
int getIncrementValue() {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
14
Java/src/main/java/com/gildedrose/SulfurasItemUpdater.java
Normal file
14
Java/src/main/java/com/gildedrose/SulfurasItemUpdater.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class SulfurasItemUpdater extends ItemUpdater {
|
||||
|
||||
@Override
|
||||
public void update(Item item) {
|
||||
// Don't change legendary item
|
||||
}
|
||||
|
||||
@Override
|
||||
int getIncrementValue() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user