diff --git a/Java/src/main/java/com/gildedrose/Constraints.java b/Java/src/main/java/com/gildedrose/Constraints.java new file mode 100644 index 0000000..7002744 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/Constraints.java @@ -0,0 +1,20 @@ +package com.gildedrose; + +public enum Constraints { + QUALITY_MAXIMUM(50), + QUALITY_MINIMUM(0), + QUALITY_OF_SULFURAS(80), + QUALITY_DECREASE_MULTIPLE(2), + QUALITY_INCREASE_MULTIPLE(2), + MULTIPLE_START_DAY(0); + + private final int value; + + Constraints(int value) { + this.value = value; + } + + public int value() { + return value; + } +} diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 87a3b92..801daf3 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,62 +1,21 @@ package com.gildedrose; +import com.gildedrose.updateLogic.UpdateLogic; +import com.gildedrose.updateLogic.UpdateLogicFactory; + class GildedRose { Item[] items; + UpdateLogicFactory updateLogics; public GildedRose(Item[] items) { this.items = items; + this.updateLogics = new UpdateLogicFactory(); } 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) { + UpdateLogic updateLogic = updateLogics.findUpdateLogic(item.name); + updateLogic.update(item); } } } diff --git a/Java/src/main/java/com/gildedrose/updateLogic/BrieUpdateLogic.java b/Java/src/main/java/com/gildedrose/updateLogic/BrieUpdateLogic.java new file mode 100644 index 0000000..af6fc36 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/updateLogic/BrieUpdateLogic.java @@ -0,0 +1,19 @@ +package com.gildedrose.updateLogic; + +import com.gildedrose.Constraints; +import com.gildedrose.Item; + +public class BrieUpdateLogic extends UpdateLogic { + private static final int QUALITY_INCREASE_BRIE = 1; + + @Override + public void update(Item item) { + decreaseSellIn(item); + + if (item.sellIn < Constraints.MULTIPLE_START_DAY.value()) { + increaseQuality(item, QUALITY_INCREASE_BRIE * Constraints.QUALITY_INCREASE_MULTIPLE.value()); + } else { + increaseQuality(item, QUALITY_INCREASE_BRIE); + } + } +} diff --git a/Java/src/main/java/com/gildedrose/updateLogic/ConjuredUpdateLogic.java b/Java/src/main/java/com/gildedrose/updateLogic/ConjuredUpdateLogic.java new file mode 100644 index 0000000..86a1e2a --- /dev/null +++ b/Java/src/main/java/com/gildedrose/updateLogic/ConjuredUpdateLogic.java @@ -0,0 +1,20 @@ +package com.gildedrose.updateLogic; + +import com.gildedrose.Constraints; +import com.gildedrose.Item; + +public class ConjuredUpdateLogic extends UpdateLogic { + private static final int QUALITY_DECREASE_CONJURED = 2; + + @Override + public void update(Item item) { + decreaseSellIn(item); + + if (item.sellIn < Constraints.MULTIPLE_START_DAY.value()) { + decreaseQuality(item, QUALITY_DECREASE_CONJURED * Constraints.QUALITY_DECREASE_MULTIPLE.value()); + } else { + decreaseQuality(item, QUALITY_DECREASE_CONJURED); + } + + } +} diff --git a/Java/src/main/java/com/gildedrose/updateLogic/ItemCategory.java b/Java/src/main/java/com/gildedrose/updateLogic/ItemCategory.java new file mode 100644 index 0000000..e03a08b --- /dev/null +++ b/Java/src/main/java/com/gildedrose/updateLogic/ItemCategory.java @@ -0,0 +1,19 @@ +package com.gildedrose.updateLogic; + +public enum ItemCategory { + BRIE("Aged Brie"), + PASS("Backstage passes to a "), + CONJURED("Conjured "), + SULFURAS("Sulfuras"), + NORMAL("THIS_IS_NORMAL!!"); + + private final String value; + + ItemCategory(String value) { + this.value = value; + } + + public String value() { + return value; + } +} diff --git a/Java/src/main/java/com/gildedrose/updateLogic/NormalUpdateLogic.java b/Java/src/main/java/com/gildedrose/updateLogic/NormalUpdateLogic.java new file mode 100644 index 0000000..bbf850b --- /dev/null +++ b/Java/src/main/java/com/gildedrose/updateLogic/NormalUpdateLogic.java @@ -0,0 +1,19 @@ +package com.gildedrose.updateLogic; + +import com.gildedrose.Constraints; +import com.gildedrose.Item; + +public class NormalUpdateLogic extends UpdateLogic { + private static final int QUALITY_DECREASE_NORMAL = 1; + + @Override + public void update(Item item) { + decreaseSellIn(item); + + if (item.sellIn < 0) { + decreaseQuality(item, QUALITY_DECREASE_NORMAL * Constraints.QUALITY_DECREASE_MULTIPLE.value()); + } else { + decreaseQuality(item, QUALITY_DECREASE_NORMAL); + } + } +} diff --git a/Java/src/main/java/com/gildedrose/updateLogic/PassesUpdateLogic.java b/Java/src/main/java/com/gildedrose/updateLogic/PassesUpdateLogic.java new file mode 100644 index 0000000..043aa71 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/updateLogic/PassesUpdateLogic.java @@ -0,0 +1,25 @@ +package com.gildedrose.updateLogic; + +import com.gildedrose.Item; + +public class PassesUpdateLogic extends UpdateLogic { + private static final int QUALITY_INCREASE_PASSES_V1 = 1; + private static final int QUALITY_INCREASE_PASSES_V2 = 2; + private static final int QUALITY_INCREASE_PASSES_V3 = 3; + + @Override + public void update(Item item) { + decreaseSellIn(item); + + if (item.sellIn < 0) { + resetQuality(item); + } else if (item.sellIn <= 5) { + increaseQuality(item, QUALITY_INCREASE_PASSES_V3); + } else if (item.sellIn <= 10) { + increaseQuality(item, QUALITY_INCREASE_PASSES_V2); + } else { + increaseQuality(item, QUALITY_INCREASE_PASSES_V1); + } + + } +} diff --git a/Java/src/main/java/com/gildedrose/updateLogic/SulfurasUpdateLogic.java b/Java/src/main/java/com/gildedrose/updateLogic/SulfurasUpdateLogic.java new file mode 100644 index 0000000..e17e393 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/updateLogic/SulfurasUpdateLogic.java @@ -0,0 +1,10 @@ +package com.gildedrose.updateLogic; + +import com.gildedrose.Item; + +public class SulfurasUpdateLogic extends UpdateLogic { + @Override + public void update(Item item) { + + } +} diff --git a/Java/src/main/java/com/gildedrose/updateLogic/UpdateLogic.java b/Java/src/main/java/com/gildedrose/updateLogic/UpdateLogic.java new file mode 100644 index 0000000..baad208 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/updateLogic/UpdateLogic.java @@ -0,0 +1,26 @@ +package com.gildedrose.updateLogic; + +import static com.gildedrose.Constraints.*; + +import com.gildedrose.Item; + +public abstract class UpdateLogic { + + protected void decreaseSellIn(Item item) { + item.sellIn -= 1; + } + + protected void decreaseQuality(Item item, int decreaseAmount) { + item.quality = Math.max(item.quality - decreaseAmount, QUALITY_MINIMUM.value()); + } + + protected void increaseQuality(Item item, int increaseAmount) { + item.quality = Math.min(item.quality + increaseAmount, QUALITY_MAXIMUM.value()); + } + + protected void resetQuality(Item item) { + item.quality = QUALITY_MINIMUM.value(); + } + + public abstract void update(Item item); +} diff --git a/Java/src/main/java/com/gildedrose/updateLogic/UpdateLogicFactory.java b/Java/src/main/java/com/gildedrose/updateLogic/UpdateLogicFactory.java new file mode 100644 index 0000000..05ccd15 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/updateLogic/UpdateLogicFactory.java @@ -0,0 +1,29 @@ +package com.gildedrose.updateLogic; + +import static com.gildedrose.updateLogic.ItemCategory.*; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class UpdateLogicFactory { + private final Map updateLogicMap; + + public UpdateLogicFactory() { + updateLogicMap = new HashMap<>(); + + updateLogicMap.put(BRIE, new BrieUpdateLogic()); + updateLogicMap.put(PASS, new PassesUpdateLogic()); + updateLogicMap.put(CONJURED, new ConjuredUpdateLogic()); + updateLogicMap.put(SULFURAS, new SulfurasUpdateLogic()); + updateLogicMap.put(NORMAL, new NormalUpdateLogic()); + } + + public UpdateLogic findUpdateLogic(String name) { + ItemCategory itemCategory = Arrays.stream(ItemCategory.values()) + .filter(category -> name.contains(category.value())) + .findFirst() + .orElse(NORMAL); + return updateLogicMap.get(itemCategory); + } +} diff --git a/Java/src/test/java/com/gildedrose/GildedRoseTest.java b/Java/src/test/java/com/gildedrose/GildedRoseTest.java deleted file mode 100644 index 8ae29ee..0000000 --- a/Java/src/test/java/com/gildedrose/GildedRoseTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.gildedrose; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class GildedRoseTest { - - @Test - void foo() { - Item[] items = new Item[] { new Item("foo", 0, 0) }; - GildedRose app = new GildedRose(items); - app.updateQuality(); - assertEquals("fixme", app.items[0].name); - } - -} diff --git a/Java/src/test/java/com/gildedrose/TexttestFixture.java b/Java/src/test/java/com/gildedrose/TexttestFixture.java index d059c88..d4a66c8 100644 --- a/Java/src/test/java/com/gildedrose/TexttestFixture.java +++ b/Java/src/test/java/com/gildedrose/TexttestFixture.java @@ -5,16 +5,17 @@ public static void main(String[] args) { System.out.println("OMGHAI!"); Item[] items = new Item[] { - new Item("+5 Dexterity Vest", 10, 20), // - new Item("Aged Brie", 2, 0), // - new Item("Elixir of the Mongoose", 5, 7), // - new Item("Sulfuras, Hand of Ragnaros", 0, 80), // - new Item("Sulfuras, Hand of Ragnaros", -1, 80), - new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), - new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), - new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), - // this conjured item does not work properly yet - new Item("Conjured Mana Cake", 3, 6) }; + new Item("+5 Dexterity Vest", 10, 20), // + new Item("Aged Brie", 2, 0), // + new Item("Elixir of the Mongoose", 5, 7), // + new Item("Sulfuras, Hand of Ragnaros", 0, 80), // + new Item("Sulfuras, Hand of Ragnaros", -1, 80), + new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), + new Item("Backstage passes to a TAFKAL80ETC concert", 11, 40), + new Item("Backstage passes to a TAFKAL80ETC concert", 6, 40), + new Item("Backstage passes to a TAFKAL80ETC concert", 0, 40), + // this conjured item does not work properly yet + new Item("Conjured Mana Cake", 3, 6)}; GildedRose app = new GildedRose(items);