mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-20 00:41:07 +00:00
🔨 move item name to implementations
This commit is contained in:
parent
39a0e60141
commit
fa28c30215
@ -7,7 +7,9 @@ import com.gildedrose.items.LegendaryItem;
|
|||||||
import com.gildedrose.items.NormalItem;
|
import com.gildedrose.items.NormalItem;
|
||||||
import com.gildedrose.main.Item;
|
import com.gildedrose.main.Item;
|
||||||
|
|
||||||
import static com.gildedrose.item_helpers.ItemName.getItemName;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class ItemFactory {
|
public class ItemFactory {
|
||||||
|
|
||||||
@ -16,19 +18,17 @@ public class ItemFactory {
|
|||||||
|
|
||||||
public static ItemType getItemType(Item item) {
|
public static ItemType getItemType(Item item) {
|
||||||
QualityValidator.validateQuality(item);
|
QualityValidator.validateQuality(item);
|
||||||
ItemName itemName = getItemName(item.name);
|
ItemType itemType = getItems(item).get(item.name);
|
||||||
switch (itemName) {
|
if (itemType == null) {
|
||||||
case AGED_BRIE:
|
itemType = new NormalItem(item);
|
||||||
return new AgedBrieItem(item);
|
|
||||||
case LEGENDARY:
|
|
||||||
return new LegendaryItem(item);
|
|
||||||
case BACKSTAGE_PASS:
|
|
||||||
return new BackstagePassItem(item);
|
|
||||||
case CONJURED:
|
|
||||||
return new ConjuredItem(item);
|
|
||||||
default:
|
|
||||||
return new NormalItem(item);
|
|
||||||
}
|
}
|
||||||
|
return itemType;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, ItemType> getItems(Item item) {
|
||||||
|
return Stream.of(new NormalItem(item), new AgedBrieItem(item), new LegendaryItem(item),
|
||||||
|
new BackstagePassItem(item), new ConjuredItem(item))
|
||||||
|
.collect(Collectors.toMap(ItemType::getName, itemType -> itemType));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,30 +0,0 @@
|
|||||||
package com.gildedrose.item_helpers;
|
|
||||||
|
|
||||||
public enum ItemName {
|
|
||||||
|
|
||||||
LEGENDARY("Sulfuras, Hand of Ragnaros"),
|
|
||||||
NORMAL("Normal"),
|
|
||||||
AGED_BRIE("Aged Brie"),
|
|
||||||
BACKSTAGE_PASS("Backstage passes to a TAFKAL80ETC concert"),
|
|
||||||
CONJURED("Conjured Mana Cake");
|
|
||||||
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
ItemName(String input) {
|
|
||||||
this.name = input;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ItemName getItemName(String input) {
|
|
||||||
for (ItemName itemName : ItemName.values()) {
|
|
||||||
if (itemName.name.equalsIgnoreCase(input)) {
|
|
||||||
return itemName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NORMAL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,4 +2,6 @@ package com.gildedrose.item_helpers;
|
|||||||
|
|
||||||
public interface ItemType {
|
public interface ItemType {
|
||||||
void updateQuality();
|
void updateQuality();
|
||||||
|
|
||||||
|
String getName();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,8 @@ import com.gildedrose.main.Item;
|
|||||||
|
|
||||||
public class AgedBrieItem implements ItemType {
|
public class AgedBrieItem implements ItemType {
|
||||||
|
|
||||||
|
public static final String AGED_BRIE = "Aged Brie";
|
||||||
|
|
||||||
private final ItemHandler item;
|
private final ItemHandler item;
|
||||||
|
|
||||||
public AgedBrieItem(Item item) {
|
public AgedBrieItem(Item item) {
|
||||||
@ -22,4 +24,9 @@ public class AgedBrieItem implements ItemType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return AGED_BRIE;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import com.gildedrose.main.Item;
|
|||||||
|
|
||||||
public class BackstagePassItem implements ItemType {
|
public class BackstagePassItem implements ItemType {
|
||||||
|
|
||||||
|
public static final String BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
private final ItemHandler item;
|
private final ItemHandler item;
|
||||||
|
|
||||||
public BackstagePassItem(Item item) {
|
public BackstagePassItem(Item item) {
|
||||||
@ -26,4 +27,9 @@ public class BackstagePassItem implements ItemType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return BACKSTAGE_PASS;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,8 @@ import com.gildedrose.main.Item;
|
|||||||
|
|
||||||
public class ConjuredItem implements ItemType {
|
public class ConjuredItem implements ItemType {
|
||||||
|
|
||||||
|
public static final String CONJURED = "Conjured Mana Cake";
|
||||||
|
|
||||||
private final ItemHandler item;
|
private final ItemHandler item;
|
||||||
|
|
||||||
public ConjuredItem(Item item) {
|
public ConjuredItem(Item item) {
|
||||||
@ -22,4 +24,9 @@ public class ConjuredItem implements ItemType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return CONJURED;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,11 +4,10 @@ import com.gildedrose.item_helpers.ItemHandler;
|
|||||||
import com.gildedrose.item_helpers.ItemType;
|
import com.gildedrose.item_helpers.ItemType;
|
||||||
import com.gildedrose.main.Item;
|
import com.gildedrose.main.Item;
|
||||||
|
|
||||||
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
|
||||||
|
|
||||||
public class LegendaryItem implements ItemType {
|
public class LegendaryItem implements ItemType {
|
||||||
|
|
||||||
public static final int LEGENDARY_ITEM_QUALITY = 80;
|
public static final int LEGENDARY_ITEM_QUALITY = 80;
|
||||||
|
public static final String LEGENDARY = "Sulfuras, Hand of Ragnaros";
|
||||||
|
|
||||||
private final ItemHandler item;
|
private final ItemHandler item;
|
||||||
|
|
||||||
@ -21,12 +20,17 @@ public class LegendaryItem implements ItemType {
|
|||||||
item.decrementSellInDate();
|
item.decrementSellInDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return LEGENDARY;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isLegendary(Item item) {
|
public static boolean isLegendary(Item item) {
|
||||||
return item.name.equals(LEGENDARY.toString());
|
return item.name.equals(LEGENDARY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNotLegendary(Item item) {
|
public static boolean isNotLegendary(Item item) {
|
||||||
return !item.name.equals(LEGENDARY.toString());
|
return !item.name.equals(LEGENDARY);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import com.gildedrose.main.Item;
|
|||||||
|
|
||||||
public class NormalItem implements ItemType {
|
public class NormalItem implements ItemType {
|
||||||
|
|
||||||
|
public static final String NORMAL = "Normal";
|
||||||
private final ItemHandler item;
|
private final ItemHandler item;
|
||||||
|
|
||||||
public NormalItem(Item item) {
|
public NormalItem(Item item) {
|
||||||
@ -22,4 +23,9 @@ public class NormalItem implements ItemType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return NORMAL;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,13 +8,13 @@ import org.junit.jupiter.api.TestMethodOrder;
|
|||||||
|
|
||||||
import static com.gildedrose.helper.TestHelper.testItem;
|
import static com.gildedrose.helper.TestHelper.testItem;
|
||||||
import static com.gildedrose.helper.TestHelper.testItemException;
|
import static com.gildedrose.helper.TestHelper.testItemException;
|
||||||
import static com.gildedrose.item_helpers.ItemName.AGED_BRIE;
|
import static com.gildedrose.items.AgedBrieItem.AGED_BRIE;
|
||||||
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
class AgedBrieItemTest {
|
class AgedBrieItemTest {
|
||||||
|
|
||||||
private final Item item = new Item(AGED_BRIE.toString(), 5, 20);
|
private final Item item = new Item(AGED_BRIE, 5, 20);
|
||||||
private final Item itemError = new Item(AGED_BRIE.toString(), 10, -5);
|
private final Item itemError = new Item(AGED_BRIE, 10, -5);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
|
|||||||
@ -8,13 +8,13 @@ import org.junit.jupiter.api.TestMethodOrder;
|
|||||||
|
|
||||||
import static com.gildedrose.helper.TestHelper.testItem;
|
import static com.gildedrose.helper.TestHelper.testItem;
|
||||||
import static com.gildedrose.helper.TestHelper.testItemException;
|
import static com.gildedrose.helper.TestHelper.testItemException;
|
||||||
import static com.gildedrose.item_helpers.ItemName.BACKSTAGE_PASS;
|
import static com.gildedrose.items.BackstagePassItem.BACKSTAGE_PASS;
|
||||||
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
class BackstagePassItemTest {
|
class BackstagePassItemTest {
|
||||||
|
|
||||||
private final Item item = new Item(BACKSTAGE_PASS.toString(), 15, 20);
|
private final Item item = new Item(BACKSTAGE_PASS, 15, 20);
|
||||||
private final Item itemError = new Item(BACKSTAGE_PASS.toString(), 10, -5);
|
private final Item itemError = new Item(BACKSTAGE_PASS, 10, -5);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
|
|||||||
@ -8,13 +8,13 @@ import org.junit.jupiter.api.TestMethodOrder;
|
|||||||
|
|
||||||
import static com.gildedrose.helper.TestHelper.testItem;
|
import static com.gildedrose.helper.TestHelper.testItem;
|
||||||
import static com.gildedrose.helper.TestHelper.testItemException;
|
import static com.gildedrose.helper.TestHelper.testItemException;
|
||||||
import static com.gildedrose.item_helpers.ItemName.CONJURED;
|
import static com.gildedrose.items.ConjuredItem.CONJURED;
|
||||||
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
class ConjuredItemTest {
|
class ConjuredItemTest {
|
||||||
|
|
||||||
private final Item item = new Item(CONJURED.toString(), 5, 20);
|
private final Item item = new Item(CONJURED, 5, 20);
|
||||||
private final Item itemError = new Item(CONJURED.toString(), 10, -5);
|
private final Item itemError = new Item(CONJURED, 10, -5);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import org.junit.jupiter.api.TestMethodOrder;
|
|||||||
|
|
||||||
import static com.gildedrose.helper.TestHelper.testItem;
|
import static com.gildedrose.helper.TestHelper.testItem;
|
||||||
import static com.gildedrose.item_helpers.QualityValidator.NOT_LEGENDARY_ITEM_ERROR_MESSAGE;
|
import static com.gildedrose.item_helpers.QualityValidator.NOT_LEGENDARY_ITEM_ERROR_MESSAGE;
|
||||||
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
import static com.gildedrose.items.LegendaryItem.LEGENDARY;
|
||||||
import static com.gildedrose.items.LegendaryItem.LEGENDARY_ITEM_QUALITY;
|
import static com.gildedrose.items.LegendaryItem.LEGENDARY_ITEM_QUALITY;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
@ -17,9 +17,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
@TestMethodOrder(OrderAnnotation.class)
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
class LegendaryItemTest {
|
class LegendaryItemTest {
|
||||||
|
|
||||||
private final Item item = new Item(LEGENDARY.toString(), 5, 80);
|
private final Item item = new Item(LEGENDARY, 5, 80);
|
||||||
private final Item fakeLegendaryItem = new Item(LEGENDARY.toString(), 5, 75);
|
private final Item fakeLegendaryItem = new Item(LEGENDARY, 5, 75);
|
||||||
private final Item itemError = new Item(LEGENDARY.toString(), 10, -5);
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
|
|||||||
@ -7,14 +7,14 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.junit.jupiter.api.TestMethodOrder;
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
|
||||||
import static com.gildedrose.helper.TestHelper.*;
|
import static com.gildedrose.helper.TestHelper.*;
|
||||||
import static com.gildedrose.item_helpers.ItemName.NORMAL;
|
import static com.gildedrose.items.NormalItem.NORMAL;
|
||||||
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
class NormalItemTest {
|
class NormalItemTest {
|
||||||
|
|
||||||
private final Item item = new Item(NORMAL.toString(), 5, 20);
|
private final Item item = new Item(NORMAL, 5, 20);
|
||||||
private final Item itemNegativeQuality = new Item(NORMAL.toString(), 10, -5);
|
private final Item itemNegativeQuality = new Item(NORMAL, 10, -5);
|
||||||
private final Item itemAboveLimitQuality = new Item(NORMAL.toString(), 10, 60);
|
private final Item itemAboveLimitQuality = new Item(NORMAL, 10, 60);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
|
|||||||
@ -5,18 +5,18 @@ import org.junit.jupiter.api.Order;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestMethodOrder;
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
|
||||||
import static com.gildedrose.item_helpers.ItemName.AGED_BRIE;
|
import static com.gildedrose.items.AgedBrieItem.AGED_BRIE;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
class ItemClassTest {
|
class ItemClassTest {
|
||||||
|
|
||||||
private final Item item = new Item(AGED_BRIE.toString(), 5, 20);
|
private final Item item = new Item(AGED_BRIE, 5, 20);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
void testItemSuccess() {
|
void testItemSuccess() {
|
||||||
assertEquals(AGED_BRIE.toString(), item.name);
|
assertEquals(AGED_BRIE, item.name);
|
||||||
assertEquals(5, item.sellIn);
|
assertEquals(5, item.sellIn);
|
||||||
assertEquals(20, item.quality);
|
assertEquals(20, item.quality);
|
||||||
assertEquals("Aged Brie, 5, 20", item.toString());
|
assertEquals("Aged Brie, 5, 20", item.toString());
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user