mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 06:21:29 +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;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import com.gildedrose.item.CustomisedItem;
|
||||||
import com.gildedrose.item.Item;
|
import com.gildedrose.item.Item;
|
||||||
import com.gildedrose.item.CustomisedItemFactory;
|
import com.gildedrose.item.CustomisedItemFactory;
|
||||||
import com.gildedrose.item.QualityValues;
|
import com.gildedrose.item.QualityValues;
|
||||||
@ -7,17 +8,15 @@ import com.gildedrose.item.QualityValues;
|
|||||||
class GildedRose {
|
class GildedRose {
|
||||||
|
|
||||||
private static final int LOWEST_QUALITY_VALUE_POSSIBLE = 0;
|
private static final int LOWEST_QUALITY_VALUE_POSSIBLE = 0;
|
||||||
private final CustomisedItemFactory customisedItemFactory;
|
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
public GildedRose(Item[] items) {
|
public GildedRose(Item[] items) {
|
||||||
this.customisedItemFactory = new CustomisedItemFactory();
|
|
||||||
this.items = items;
|
this.items = items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
for (Item item : items) {
|
for (Item item : items) {
|
||||||
customisedItemFactory.customiseItem(item).updateState();
|
customisedItem(item).updateState();
|
||||||
if (hasReachedLowestQualityValue(item)) {
|
if (hasReachedLowestQualityValue(item)) {
|
||||||
item.quality = LOWEST_QUALITY_VALUE_POSSIBLE;
|
item.quality = LOWEST_QUALITY_VALUE_POSSIBLE;
|
||||||
} else if (hasReachedHighestQualityValue(item)) {
|
} 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) {
|
private boolean hasReachedLowestQualityValue(Item item) {
|
||||||
return item.quality < LOWEST_QUALITY_VALUE_POSSIBLE;
|
return item.quality < LOWEST_QUALITY_VALUE_POSSIBLE;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,23 +1,31 @@
|
|||||||
package com.gildedrose.item;
|
package com.gildedrose.item;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class CustomisedItemFactory {
|
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 SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||||
public final static String BRIE = "Aged Brie";
|
public final static String BRIE = "Aged Brie";
|
||||||
public final static String BACKSTAGE_PASSES_ITEM = "Backstage passes to a TAFKAL80ETC concert";
|
public final static String BACKSTAGE_PASSES_ITEM = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
public final static String CONJURED_ITEM = "Conjured";
|
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) {
|
public CustomisedItem customiseItem(Item item) {
|
||||||
if (item.name.equals(SULFURAS)) {
|
if (isStandardItem(item)) {
|
||||||
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 {
|
|
||||||
return new StandardItem(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