diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index e6feb751..4291d496 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,62 +1,22 @@ package com.gildedrose; -class GildedRose { - Item[] items; +import java.util.List; - public GildedRose(Item[] items) { +import static com.gildedrose.item_helpers.ItemFactory.getItem; +import static java.util.Collections.singletonList; + +class GildedRose { + List items; + + public GildedRose(List items) { this.items = items; } - 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; - } - } - } - } + public GildedRose(Item items) { + this.items = singletonList(items); } -} \ No newline at end of file + + public void updateQuality() { + items.forEach(item -> getItem(item).updateQuality()); + } +} diff --git a/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java b/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java index 87702dde..5567d06d 100644 --- a/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java +++ b/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java @@ -4,20 +4,23 @@ import com.gildedrose.Item; import com.gildedrose.items.AgedBrie; import com.gildedrose.items.BackstagePass; import com.gildedrose.items.Conjured; +import com.gildedrose.items.Legendary; import com.gildedrose.items.Normal; -import com.gildedrose.items.Sulfura; import static com.gildedrose.item_helpers.ItemName.getItemName; public class ItemFactory { - public ItemType getItemType(Item item) { + private ItemFactory() { + } + + public static ItemType getItem(Item item) { ItemName itemName = getItemName(item.name); switch (itemName) { case AGED_BRIE: return new AgedBrie(item); - case SULFURA: - return new Sulfura(item); + case LEGENDARY: + return new Legendary(item); case BACKSTAGE_PASS: return new BackstagePass(item); case CONJURED: diff --git a/Java/src/main/java/com/gildedrose/item_helpers/ItemHandler.java b/Java/src/main/java/com/gildedrose/item_helpers/ItemHandler.java index 9bc1436f..82979c8f 100644 --- a/Java/src/main/java/com/gildedrose/item_helpers/ItemHandler.java +++ b/Java/src/main/java/com/gildedrose/item_helpers/ItemHandler.java @@ -6,7 +6,7 @@ import static java.lang.Math.max; public class ItemHandler { - private static final int QUALITY = 80; + private static final int LEGENDARY_ITEM_QUALITY = 80; private final Item item; @@ -15,61 +15,61 @@ public class ItemHandler { } public void decrementSellInDate() { - this.item.sellIn--; + item.sellIn--; + } + + public boolean beforeSellInDate() { + return item.sellIn >= 0; + } + + public void incrementQualityByTwo() { + item.quality = max(item.quality + 2, 0); + } + + public void incrementQuality() { + item.quality++; + } + + public void decrementQuality() { + item.quality--; + } + + public void decrementQualityBy4() { + item.quality = max(item.quality - 4, 0); + } + + public void decrementQualityBy2() { + item.quality = max(item.quality - 2, 0); + } + + public void setLegendaryQuality() { + if (item.quality != LEGENDARY_ITEM_QUALITY) { + item.quality = LEGENDARY_ITEM_QUALITY; + } + } + + public boolean sellInLessThan5Days() { + return item.sellIn >= 0 && item.sellIn <= 5; + } + + public boolean sellInLessThan10Days() { + return item.sellIn >= 5 && item.sellIn <= 10; + } + + public boolean sellInDaysMoreThan10Days() { + return item.sellIn >= 10; } public boolean qualityIsHigherThanZero() { return item.quality > 0; } - public boolean sellInDatePasses() { - return this.item.sellIn < 0; - } - - public void incrementQualityByTwo() { - this.item.quality = max(this.item.quality + 2, 0); - } - - public void incrementQuality() { - this.item.quality++; - } - - public void decrementQuality() { - this.item.quality--; - } - - public void decrementQualityBy4() { - this.item.quality = max(this.item.quality - 4, 0); - } - - public void decrementQualityBy2() { - this.item.quality = this.item.quality - 2; - } - - public void setQualityTo80() { - if (this.item.quality != QUALITY) { - this.item.quality = QUALITY; - } - } - - public boolean sellInLessThan5Days() { - return this.item.sellIn >= 0 && this.item.sellIn <= 5; - } - - public boolean sellInLessThan10Days() { - return this.item.sellIn >= 5 && this.item.sellIn <= 10; - } - - public boolean sellInDaysMoreThan10Days() { - return this.item.sellIn >= 10; - } - public void makeQualityZero() { - this.item.quality = 0; + item.quality = 0; } public void incrementQualityBy3() { - this.item.quality = this.item.quality + 3; + item.quality = max(item.quality + 3, 0); } } diff --git a/Java/src/main/java/com/gildedrose/item_helpers/ItemName.java b/Java/src/main/java/com/gildedrose/item_helpers/ItemName.java index 25a6af9d..c97790f4 100644 --- a/Java/src/main/java/com/gildedrose/item_helpers/ItemName.java +++ b/Java/src/main/java/com/gildedrose/item_helpers/ItemName.java @@ -2,11 +2,11 @@ package com.gildedrose.item_helpers; public enum ItemName { - SULFURA("Sulfura"), + LEGENDARY("Sulfuras, Hand of Ragnaros"), NORMAL("Normal"), AGED_BRIE("Aged Brie"), - BACKSTAGE_PASS("Backstage pass"), - CONJURED("Conjured"); + BACKSTAGE_PASS("Backstage passes to a TAFKAL80ETC concert"), + CONJURED("Conjured Mana Cake"); private final String name; @@ -21,9 +21,9 @@ public enum ItemName { public static ItemName getItemName(String input) { for (ItemName itemName : ItemName.values()) { - String itemNameStr = itemName.toString().toUpperCase(); - if (itemNameStr.contains(input.toUpperCase())) + if (itemName.name.equalsIgnoreCase(input)) { return itemName; + } } return NORMAL; } diff --git a/Java/src/main/java/com/gildedrose/items/AgedBrie.java b/Java/src/main/java/com/gildedrose/items/AgedBrie.java index f0cd8b86..28dc9cb5 100644 --- a/Java/src/main/java/com/gildedrose/items/AgedBrie.java +++ b/Java/src/main/java/com/gildedrose/items/AgedBrie.java @@ -15,12 +15,10 @@ public class AgedBrie implements ItemType { @Override public void updateQuality() { item.decrementSellInDate(); - if (item.qualityIsHigherThanZero()) { - if (item.sellInDatePasses()) { - item.incrementQualityByTwo(); - } else { - item.incrementQuality(); - } + if (item.beforeSellInDate()) { + item.incrementQuality(); + } else { + item.incrementQualityByTwo(); } } diff --git a/Java/src/main/java/com/gildedrose/items/Conjured.java b/Java/src/main/java/com/gildedrose/items/Conjured.java index fc521b10..3407ce51 100644 --- a/Java/src/main/java/com/gildedrose/items/Conjured.java +++ b/Java/src/main/java/com/gildedrose/items/Conjured.java @@ -16,12 +16,11 @@ public class Conjured implements ItemType { public void updateQuality() { item.decrementSellInDate(); if (item.qualityIsHigherThanZero()) { - if (item.sellInDatePasses()) { - item.decrementQualityBy4(); - } else { + if (item.beforeSellInDate()) { item.decrementQuality(); + } else { + item.decrementQualityBy4(); } } } - } diff --git a/Java/src/main/java/com/gildedrose/items/Sulfura.java b/Java/src/main/java/com/gildedrose/items/Legendary.java similarity index 74% rename from Java/src/main/java/com/gildedrose/items/Sulfura.java rename to Java/src/main/java/com/gildedrose/items/Legendary.java index 4a0ef582..413a9aa2 100644 --- a/Java/src/main/java/com/gildedrose/items/Sulfura.java +++ b/Java/src/main/java/com/gildedrose/items/Legendary.java @@ -4,18 +4,18 @@ import com.gildedrose.Item; import com.gildedrose.item_helpers.ItemHandler; import com.gildedrose.item_helpers.ItemType; -public class Sulfura implements ItemType { +public class Legendary implements ItemType { private final ItemHandler item; - public Sulfura(Item item) { + public Legendary(Item item) { this.item = new ItemHandler(item); } @Override public void updateQuality() { item.decrementSellInDate(); - item.setQualityTo80(); + item.setLegendaryQuality(); } } diff --git a/Java/src/main/java/com/gildedrose/items/Normal.java b/Java/src/main/java/com/gildedrose/items/Normal.java index 5a355132..da7afd0b 100644 --- a/Java/src/main/java/com/gildedrose/items/Normal.java +++ b/Java/src/main/java/com/gildedrose/items/Normal.java @@ -16,10 +16,10 @@ public class Normal implements ItemType { public void updateQuality() { item.decrementSellInDate(); if (item.qualityIsHigherThanZero()) { - if (item.sellInDatePasses()) { - item.decrementQualityBy2(); - } else { + if (item.beforeSellInDate()) { item.decrementQuality(); + } else { + item.decrementQualityBy2(); } } } diff --git a/Java/src/test/java/com/gildedrose/GildedRoseTest.java b/Java/src/test/java/com/gildedrose/GildedRoseTest.java index b818451a..2d431081 100644 --- a/Java/src/test/java/com/gildedrose/GildedRoseTest.java +++ b/Java/src/test/java/com/gildedrose/GildedRoseTest.java @@ -1,30 +1,32 @@ package com.gildedrose; -import com.gildedrose.item_helpers.ItemFactory; -import com.gildedrose.item_helpers.ItemType; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.List; + import static com.gildedrose.item_helpers.ItemName.*; +import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertEquals; class GildedRoseTest { @Test void foo() { - Item[] items = new Item[]{new Item("foo", 0, 0)}; + List items = singletonList(new Item("foo", 0, 0)); GildedRose app = new GildedRose(items); app.updateQuality(); - assertEquals("foo", app.items[0].name); + assertEquals("foo", app.items.get(0).name); } @Test void testNormalItem() { - ItemFactory itemFactory = new ItemFactory(); int days = 20; Item normalItem = new Item(NORMAL.toString(), 10, 20); + GildedRose app = new GildedRose(normalItem); + app.updateQuality(); for (int i = 0; i < days; i++) { - ItemType itemType = itemFactory.getItemType(normalItem); - itemType.updateQuality(); + app.updateQuality(); System.out.println("name, sell-in, quality"); System.out.println(normalItem); } @@ -32,12 +34,11 @@ class GildedRoseTest { @Test void testConjuredItem() { - ItemFactory itemFactory = new ItemFactory(); int days = 20; Item normalItem = new Item(CONJURED.toString(), 10, 40); + GildedRose app = new GildedRose(normalItem); for (int i = 0; i < days; i++) { - ItemType itemType = itemFactory.getItemType(normalItem); - itemType.updateQuality(); + app.updateQuality(); System.out.println("name, sell-in, quality"); System.out.println(normalItem); } @@ -46,40 +47,39 @@ class GildedRoseTest { @Test void testSulfuraItem() { - ItemFactory itemFactory = new ItemFactory(); int days = 20; - Item normalItem = new Item(SULFURA.toString(), 10, 40); + Item legendaryItem = new Item(LEGENDARY.toString(), 10, 40); + GildedRose app = new GildedRose(legendaryItem); for (int i = 0; i < days; i++) { - ItemType itemType = itemFactory.getItemType(normalItem); - itemType.updateQuality(); + app.updateQuality(); System.out.println("name, sell-in, quality"); - System.out.println(normalItem); + System.out.println(legendaryItem); } } @Test void testAgedBrieItem() { - ItemFactory itemFactory = new ItemFactory(); int days = 20; - Item normalItem = new Item(AGED_BRIE.toString(), 10, 40); + Item agedBrie = new Item(AGED_BRIE.toString(), 10, 40); + GildedRose app = new GildedRose(agedBrie); for (int i = 0; i < days; i++) { - ItemType itemType = itemFactory.getItemType(normalItem); - itemType.updateQuality(); + app.updateQuality(); System.out.println("name, sell-in, quality"); - System.out.println(normalItem); + System.out.println(agedBrie); } } @Test void testBackstagePassItem() { - ItemFactory itemFactory = new ItemFactory(); int days = 20; - Item normalItem = new Item(BACKSTAGE_PASS.toString(), 15, 40); + List backStagePass = Arrays.asList(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)); + GildedRose app = new GildedRose(backStagePass); for (int i = 0; i < days; i++) { - ItemType itemType = itemFactory.getItemType(normalItem); - itemType.updateQuality(); + app.updateQuality(); System.out.println("name, sell-in, quality"); - System.out.println(normalItem); + System.out.println(backStagePass); } } diff --git a/Java/src/test/java/com/gildedrose/TexttestFixture.java b/Java/src/test/java/com/gildedrose/TexttestFixture.java index d059c88f..2ccfa77b 100644 --- a/Java/src/test/java/com/gildedrose/TexttestFixture.java +++ b/Java/src/test/java/com/gildedrose/TexttestFixture.java @@ -1,22 +1,24 @@ package com.gildedrose; +import static java.util.Arrays.asList; + public class TexttestFixture { 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) }; + Item[] items = new Item[]{ + new Item("Aged Brie", 2, 0), // + new Item("+5 Dexterity Vest", 10, 20), // + 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)}; - GildedRose app = new GildedRose(items); + GildedRose app = new GildedRose(asList(items)); int days = 2; if (args.length > 0) { diff --git a/Java/src/test/java/com/gildedrose/TexttestFixtureTemp.java b/Java/src/test/java/com/gildedrose/TexttestFixtureTemp.java index c1aa14a6..14fa055d 100644 --- a/Java/src/test/java/com/gildedrose/TexttestFixtureTemp.java +++ b/Java/src/test/java/com/gildedrose/TexttestFixtureTemp.java @@ -1,22 +1,24 @@ package com.gildedrose; +import static java.util.Arrays.asList; + public class TexttestFixtureTemp { 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) }; + 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)}; - GildedRose app = new GildedRose(items); + GildedRose app = new GildedRose(asList(items)); int days = 20; if (args.length > 0) {