diff --git a/Java/readme.md b/Java/readme.md index 8a0e345f..f1b789ea 100644 --- a/Java/readme.md +++ b/Java/readme.md @@ -1,9 +1,9 @@ # Types of items -| Item name | Quality | Description | +| Item name | Quality changes | Description | |:------------|:-----------------|:------------- | -| Sulfura | Always 80, constant value, cannot change.| Legendary item| -| Aged Brie | **increases** in quality twice as fast, when sell-in date approaches | - | -| Backstage Pass | **increases** in quality as sell-in date approaches; Quality increases by 2 when there are 10 days or less and by 3 when there are days or less but Quality drops to 0 after the concert. |- | -| Normal | **decreases** in quality twice as fast, when sell-in date approaches |- | -| Conjured | **decreases twice as fast** in quality as normal items, when sell-in date approaches |- | +| Normal | **Quality decreases by 1;**
**Quality decreases by 2**, when sell-in date approaches | - | +| Aged Brie | **Quality increases by 1;**
**Quality increases by 2**, when sell-in date approaches | - | +| Conjured | Quality decreases by 1;
Quality decreases by 4 when sell-in date approaches | - | +| Backstage Pass | Quality increases by 1;
Quality increases by 2 when there are 10 days or less;
Quality increases by 3 when there are 5 days or less;
Quality drops to 0 after the concert. | - | +| Sulfura | **Always 80**, constant value, cannot change.| Legendary item | diff --git a/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java b/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java new file mode 100644 index 00000000..87702dde --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java @@ -0,0 +1,29 @@ +package com.gildedrose.item_helpers; + +import com.gildedrose.Item; +import com.gildedrose.items.AgedBrie; +import com.gildedrose.items.BackstagePass; +import com.gildedrose.items.Conjured; +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) { + ItemName itemName = getItemName(item.name); + switch (itemName) { + case AGED_BRIE: + return new AgedBrie(item); + case SULFURA: + return new Sulfura(item); + case BACKSTAGE_PASS: + return new BackstagePass(item); + case CONJURED: + return new Conjured(item); + default: + return new Normal(item); + } + } +} diff --git a/Java/src/main/java/com/gildedrose/item_helpers/ItemName.java b/Java/src/main/java/com/gildedrose/item_helpers/ItemName.java new file mode 100644 index 00000000..25a6af9d --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item_helpers/ItemName.java @@ -0,0 +1,30 @@ +package com.gildedrose.item_helpers; + +public enum ItemName { + + SULFURA("Sulfura"), + NORMAL("Normal"), + AGED_BRIE("Aged Brie"), + BACKSTAGE_PASS("Backstage pass"), + CONJURED("Conjured"); + + 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()) { + String itemNameStr = itemName.toString().toUpperCase(); + if (itemNameStr.contains(input.toUpperCase())) + return itemName; + } + return NORMAL; + } +} diff --git a/Java/src/main/java/com/gildedrose/item_helpers/ItemType.java b/Java/src/main/java/com/gildedrose/item_helpers/ItemType.java new file mode 100644 index 00000000..1d98d95a --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item_helpers/ItemType.java @@ -0,0 +1,7 @@ +package com.gildedrose.item_helpers; + +public interface ItemType { + + void updateQuality(); + +} diff --git a/Java/src/main/java/com/gildedrose/items/AgedBrie.java b/Java/src/main/java/com/gildedrose/items/AgedBrie.java new file mode 100644 index 00000000..30d05927 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/items/AgedBrie.java @@ -0,0 +1,48 @@ +package com.gildedrose.items; + +import com.gildedrose.Item; +import com.gildedrose.item_helpers.ItemType; + +import static java.lang.Math.max; + +public class AgedBrie implements ItemType { + + private final Item item; + + public AgedBrie(Item item) { + this.item = item; + } + + @Override + public void updateQuality() { + decrementSellInDate(); + if (qualityIsPositive()) { + if (sellInDatePasses()) { + incrementQualityByTwo(); + } else { + incrementQuality(); + } + } + } + + private boolean qualityIsPositive() { + return item.quality > 0; + } + + private void decrementSellInDate() { + this.item.sellIn--; + } + + private boolean sellInDatePasses() { + return this.item.sellIn < 0; + } + + private void incrementQualityByTwo() { + this.item.quality = max(this.item.quality + 2, 0); + } + + private void incrementQuality() { + this.item.quality++; + } + +} diff --git a/Java/src/main/java/com/gildedrose/items/BackstagePass.java b/Java/src/main/java/com/gildedrose/items/BackstagePass.java new file mode 100644 index 00000000..fd20813e --- /dev/null +++ b/Java/src/main/java/com/gildedrose/items/BackstagePass.java @@ -0,0 +1,52 @@ +package com.gildedrose.items; + +import com.gildedrose.Item; +import com.gildedrose.item_helpers.ItemType; + +public class BackstagePass implements ItemType { + + private final Item item; + + public BackstagePass(Item item) { + this.item = item; + } + + @Override + public void updateQuality() { + decrementSellInDate(); + determineQuality(); + } + + private void determineQuality() { + if (this.item.sellIn >= 10) { + incrementQuality(); + } else if (this.item.sellIn >= 5) { + incrementQualityByTwo(); + } else if (this.item.sellIn >= 0) { + incrementQualityByThree(); + } else { + makeQualityZero(); + } + } + + private void decrementSellInDate() { + this.item.sellIn--; + } + + private void incrementQuality() { + this.item.quality++; + } + + private void makeQualityZero() { + this.item.quality = 0; + } + + private void incrementQualityByTwo() { + this.item.quality = this.item.quality + 2; + } + + private void incrementQualityByThree() { + this.item.quality = this.item.quality + 3; + } + +} diff --git a/Java/src/main/java/com/gildedrose/items/Conjured.java b/Java/src/main/java/com/gildedrose/items/Conjured.java new file mode 100644 index 00000000..45481722 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/items/Conjured.java @@ -0,0 +1,47 @@ +package com.gildedrose.items; + +import com.gildedrose.Item; +import com.gildedrose.item_helpers.ItemType; + +import static java.lang.Math.max; + +public class Conjured implements ItemType { + + private final Item item; + + public Conjured(Item item) { + this.item = item; + } + + @Override + public void updateQuality() { + decrementSellInDate(); + if (qualityIsGreaterThanZero()) { + if (sellInDatePasses()) { + decrementQualityByFour(); + } else { + decrementQuality(); + } + } + } + + private boolean qualityIsGreaterThanZero() { + return item.quality > 0; + } + + private void decrementSellInDate() { + this.item.sellIn--; + } + + private void decrementQualityByFour() { + this.item.quality = max(this.item.quality - 4, 0); + } + + private boolean sellInDatePasses() { + return this.item.sellIn < 0; + } + + private void decrementQuality() { + this.item.quality--; + } +} diff --git a/Java/src/main/java/com/gildedrose/items/Normal.java b/Java/src/main/java/com/gildedrose/items/Normal.java new file mode 100644 index 00000000..62b686d6 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/items/Normal.java @@ -0,0 +1,47 @@ +package com.gildedrose.items; + +import com.gildedrose.Item; +import com.gildedrose.item_helpers.ItemType; + +import static java.lang.Math.max; + +public class Normal implements ItemType { + + private final Item item; + + public Normal(Item item) { + this.item = item; + } + + @Override + public void updateQuality() { + decrementSellInDate(); + if (qualityIsPositive()) { + if (sellInDatePasses()) { + decrementQualityByTwo(); + } else { + decrementQuality(); + } + } + } + + private boolean qualityIsPositive() { + return item.quality > 0; + } + + private void decrementSellInDate() { + this.item.sellIn--; + } + + private void decrementQualityByTwo() { + this.item.quality = max(this.item.quality - 2, 0); + } + + private boolean sellInDatePasses() { + return this.item.sellIn < 0; + } + + private void decrementQuality() { + this.item.quality--; + } +} diff --git a/Java/src/main/java/com/gildedrose/items/Sulfura.java b/Java/src/main/java/com/gildedrose/items/Sulfura.java new file mode 100644 index 00000000..07e7e112 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/items/Sulfura.java @@ -0,0 +1,30 @@ +package com.gildedrose.items; + +import com.gildedrose.Item; +import com.gildedrose.item_helpers.ItemType; + +public class Sulfura implements ItemType { + + private static final int QUALITY = 80; + private final Item item; + + public Sulfura(Item item) { + this.item = item; + } + + @Override + public void updateQuality() { + decrementSellInDate(); + setQualityTo80(); + } + + private void setQualityTo80() { + if (this.item.quality != QUALITY) { + this.item.quality = QUALITY; + } + } + + private void decrementSellInDate() { + this.item.sellIn--; + } +} diff --git a/Java/src/test/java/com/gildedrose/GildedRoseTest.java b/Java/src/test/java/com/gildedrose/GildedRoseTest.java index de3838bb..b818451a 100644 --- a/Java/src/test/java/com/gildedrose/GildedRoseTest.java +++ b/Java/src/test/java/com/gildedrose/GildedRoseTest.java @@ -1,7 +1,10 @@ package com.gildedrose; +import com.gildedrose.item_helpers.ItemFactory; +import com.gildedrose.item_helpers.ItemType; import org.junit.jupiter.api.Test; +import static com.gildedrose.item_helpers.ItemName.*; import static org.junit.jupiter.api.Assertions.assertEquals; class GildedRoseTest { @@ -14,4 +17,70 @@ class GildedRoseTest { assertEquals("foo", app.items[0].name); } + @Test + void testNormalItem() { + ItemFactory itemFactory = new ItemFactory(); + int days = 20; + Item normalItem = new Item(NORMAL.toString(), 10, 20); + for (int i = 0; i < days; i++) { + ItemType itemType = itemFactory.getItemType(normalItem); + itemType.updateQuality(); + System.out.println("name, sell-in, quality"); + System.out.println(normalItem); + } + } + + @Test + void testConjuredItem() { + ItemFactory itemFactory = new ItemFactory(); + int days = 20; + Item normalItem = new Item(CONJURED.toString(), 10, 40); + for (int i = 0; i < days; i++) { + ItemType itemType = itemFactory.getItemType(normalItem); + itemType.updateQuality(); + System.out.println("name, sell-in, quality"); + System.out.println(normalItem); + } + } + + + @Test + void testSulfuraItem() { + ItemFactory itemFactory = new ItemFactory(); + int days = 20; + Item normalItem = new Item(SULFURA.toString(), 10, 40); + for (int i = 0; i < days; i++) { + ItemType itemType = itemFactory.getItemType(normalItem); + itemType.updateQuality(); + System.out.println("name, sell-in, quality"); + System.out.println(normalItem); + } + } + + @Test + void testAgedBrieItem() { + ItemFactory itemFactory = new ItemFactory(); + int days = 20; + Item normalItem = new Item(AGED_BRIE.toString(), 10, 40); + for (int i = 0; i < days; i++) { + ItemType itemType = itemFactory.getItemType(normalItem); + itemType.updateQuality(); + System.out.println("name, sell-in, quality"); + System.out.println(normalItem); + } + } + + @Test + void testBackstagePassItem() { + ItemFactory itemFactory = new ItemFactory(); + int days = 20; + Item normalItem = new Item(BACKSTAGE_PASS.toString(), 15, 40); + for (int i = 0; i < days; i++) { + ItemType itemType = itemFactory.getItemType(normalItem); + itemType.updateQuality(); + System.out.println("name, sell-in, quality"); + System.out.println(normalItem); + } + } + } diff --git a/Java/src/test/java/com/gildedrose/TexttestFixtureTemp.java b/Java/src/test/java/com/gildedrose/TexttestFixtureTemp.java new file mode 100644 index 00000000..c1aa14a6 --- /dev/null +++ b/Java/src/test/java/com/gildedrose/TexttestFixtureTemp.java @@ -0,0 +1,37 @@ +package com.gildedrose; + +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) }; + + GildedRose app = new GildedRose(items); + + int days = 20; + if (args.length > 0) { + days = Integer.parseInt(args[0]) + 1; + } + + for (int i = 0; i < days; i++) { + System.out.println("-------- day " + i + " --------"); + System.out.println("name, sellIn, quality"); + for (Item item : items) { + System.out.println(item); + } + System.out.println(); + app.updateQuality(); + } + } + +}