mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-14 22:21:20 +00:00
replaces if-statements with Map in Factory Class
This commit is contained in:
parent
5df478fa29
commit
7b4a1b6c3b
@ -1,5 +1,6 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.gildedrose.item.CustomisedItem;
|
||||
import com.gildedrose.item.Item;
|
||||
import com.gildedrose.item.CustomisedItemFactory;
|
||||
import com.gildedrose.item.QualityValues;
|
||||
@ -7,17 +8,15 @@ import com.gildedrose.item.QualityValues;
|
||||
class GildedRose {
|
||||
|
||||
private static final int LOWEST_QUALITY_VALUE_POSSIBLE = 0;
|
||||
private final CustomisedItemFactory customisedItemFactory;
|
||||
Item[] items;
|
||||
|
||||
public GildedRose(Item[] items) {
|
||||
this.customisedItemFactory = new CustomisedItemFactory();
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public void updateQuality() {
|
||||
for (Item item : items) {
|
||||
customisedItemFactory.customiseItem(item).updateState();
|
||||
customisedItem(item).updateState();
|
||||
if (hasReachedLowestQualityValue(item)) {
|
||||
item.quality = LOWEST_QUALITY_VALUE_POSSIBLE;
|
||||
} else if (hasReachedHighestQualityValue(item)) {
|
||||
@ -26,6 +25,10 @@ class GildedRose {
|
||||
}
|
||||
}
|
||||
|
||||
private CustomisedItem customisedItem(Item item) {
|
||||
return new CustomisedItemFactory(item).customiseItem(item);
|
||||
}
|
||||
|
||||
private boolean hasReachedLowestQualityValue(Item item) {
|
||||
return item.quality < LOWEST_QUALITY_VALUE_POSSIBLE;
|
||||
}
|
||||
|
||||
@ -1,23 +1,31 @@
|
||||
package com.gildedrose.item;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CustomisedItemFactory {
|
||||
|
||||
private final static Map<String, CustomisedItem> ITEM_TYPES_LIST = new HashMap<>();
|
||||
public final static String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||
public final static String BRIE = "Aged Brie";
|
||||
public final static String BACKSTAGE_PASSES_ITEM = "Backstage passes to a TAFKAL80ETC concert";
|
||||
public final static String CONJURED_ITEM = "Conjured";
|
||||
|
||||
public CustomisedItemFactory(Item item) {
|
||||
ITEM_TYPES_LIST.put(SULFURAS, new Sulfuras());
|
||||
ITEM_TYPES_LIST.put(BRIE, new AgedBrie(item));
|
||||
ITEM_TYPES_LIST.put(BACKSTAGE_PASSES_ITEM, new BackstagePassesItem(item));
|
||||
ITEM_TYPES_LIST.put(CONJURED_ITEM, new ConjuredItem(item));
|
||||
}
|
||||
|
||||
public CustomisedItem customiseItem(Item item) {
|
||||
if (item.name.equals(SULFURAS)) {
|
||||
return new Sulfuras();
|
||||
} else if (item.name.equals(BRIE)) {
|
||||
return new AgedBrie(item);
|
||||
} else if (item.name.equals(BACKSTAGE_PASSES_ITEM)) {
|
||||
return new BackstagePassesItem(item);
|
||||
} else if (item.name.equals(CONJURED_ITEM)) {
|
||||
return new ConjuredItem(item);
|
||||
} else {
|
||||
if (isStandardItem(item)) {
|
||||
return new StandardItem(item);
|
||||
}
|
||||
return ITEM_TYPES_LIST.get(item.name);
|
||||
}
|
||||
|
||||
private boolean isStandardItem(Item item) {
|
||||
return !ITEM_TYPES_LIST.keySet().contains(item.name);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user