From b7c2e8b3d7443297f8040df649e18aabdac9c3c5 Mon Sep 17 00:00:00 2001 From: EGYThomas Date: Mon, 1 Jul 2024 11:17:38 +0300 Subject: [PATCH 1/2] Added ItemUpdater Interface with Implementations for each item class. Added Tests. Updated GildedRose updateItems() method. --- .../main/java/com/gildedrose/GildedRose.java | 131 +++++++++++------- .../main/java/com/gildedrose/ItemUpdater.java | 52 +++++++ .../java/com/gildedrose/GildedRoseTest.java | 87 +++++++++++- .../java/com/gildedrose/TexttestFixture.java | 2 +- 4 files changed, 219 insertions(+), 53 deletions(-) create mode 100644 Java/src/main/java/com/gildedrose/ItemUpdater.java diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 87a3b9264d..b4fc26bccc 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,62 +1,95 @@ package com.gildedrose; +import com.gildedrose.ItemUpdater; class GildedRose { - Item[] items; + Item[] items; // Inventory* public GildedRose(Item[] 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; - } - } + // Instantiate updaters for each item type. (Can I instantiate itemupdater only when i need it?) + ItemUpdater defaultUpdater = new DefaultItemUpdater(); + ItemUpdater agedBrieUpdater = new AgedBrieUpdater(); + ItemUpdater backstagePassUpdater = new BackStagePassUpdater(); + ItemUpdater conjuredItemUpdater = new ConjuredItemUpdater(); - if (items[i].sellIn < 6) { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; - } - } - } - } + // Iterate through items and call corresponding updater + for (Item item : items) { + String name = item.name; + switch (name) { + case "Aged Brie": + agedBrieUpdater.update(item); + break; + case "Backstage passes to a TAFKAL80ETC concert": + backstagePassUpdater.update(item); + break; + case "Sulfuras, Hand of Ragnaros": + break; + case "Conjured": + conjuredItemUpdater.update(item); + break; + default: + defaultUpdater.update(item); } + } + } - 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; - } - } - } - } + +// /* +// Aged Brie increases in quality the older it gets. +// */ +// private void updateAgedBrie(Item item) { +// if (item.quality == 50) +// return; +// item.quality = item.quality + 1; +// // Do I have to decrease sellIn? +// } +// +// +// private void updateAnyItem(Item item) { +// if (item.quality <= 0 || item.sellIn <= 0) +// return; +// if (item.sellIn > 0) +// item.quality = item.quality - 1; +// else +// item.quality = item.quality - 2; +// +// item.sellIn = item.sellIn - 1; +// } +// +// /* +// like aged brie, increases in Quality as its SellIn value approaches; +// Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but +// Quality drops to 0 after the concert +// */ +// private void updateBackStageTickets(Item item) { +// if (item.sellIn <= 0) { +// item.quality = 0; +// } else if (item.quality >= 50 && item.sellIn > 0) { +// item.sellIn--; +// } else if (item.sellIn < 10 && item.quality > 5) { +// item.quality = item.quality + 2; +// item.sellIn--; +// } else if (item.sellIn < 5) { +// item.quality = item.quality + 3; +// item.sellIn--; +// } +// } +// +// +// /* +// "Conjured" items degrade in Quality twice as fast as normal items +// */ +// private void updateConjured (Item item){ +// if (item.quality <= 0 || item.sellIn <= 0) +// return; +// if (item.sellIn > 0) +// item.quality = item.quality - 2; +// else +// item.quality = item.quality - 4; +// item.sellIn = item.sellIn - 1; +// } } -} + diff --git a/Java/src/main/java/com/gildedrose/ItemUpdater.java b/Java/src/main/java/com/gildedrose/ItemUpdater.java new file mode 100644 index 0000000000..561711b369 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/ItemUpdater.java @@ -0,0 +1,52 @@ +package com.gildedrose; + +public interface ItemUpdater { + void update(Item item); +} + +class DefaultItemUpdater implements ItemUpdater { + @Override + public void update(Item item) { + decreaseQuality(item); + item.sellIn--; + if (item.sellIn < 0) + decreaseQuality(item); + } + + protected void decreaseQuality(Item item) { + if (item.quality > 0) + item.quality--; + } +} + +class AgedBrieUpdater implements ItemUpdater { + @Override + public void update(Item item) { + if (item.quality < 50) + item.quality++; + item.sellIn--; + } +} + +class BackStagePassUpdater implements ItemUpdater { + @Override + public void update(Item item) { + if (item.quality < 50) + item.quality++; + if (item.sellIn <= 10 && item.quality < 50) + item.quality++; + if (item.sellIn <= 5 && item.quality < 50) + item.quality++; + item.sellIn--; + if (item.sellIn < 0) + item.quality = 0; + } +} + +class ConjuredItemUpdater extends DefaultItemUpdater { + @Override + protected void decreaseQuality(Item item) { + if (item.quality > 0) + item.quality -= 2; + } +} diff --git a/Java/src/test/java/com/gildedrose/GildedRoseTest.java b/Java/src/test/java/com/gildedrose/GildedRoseTest.java index 8ae29eec21..4b0d875244 100644 --- a/Java/src/test/java/com/gildedrose/GildedRoseTest.java +++ b/Java/src/test/java/com/gildedrose/GildedRoseTest.java @@ -3,15 +3,96 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class GildedRoseTest { @Test - void foo() { - Item[] items = new Item[] { new Item("foo", 0, 0) }; + void ElixirDepreciates() throws Exception{ + Item[] items = new Item[]{new Item("Elixir", 0, 40)}; GildedRose app = new GildedRose(items); app.updateQuality(); - assertEquals("fixme", app.items[0].name); + app.updateQuality(); + assertEquals(-2, app.items[0].sellIn); + assertEquals(36, app.items[0].quality); + } + + // TODO Write test cases for each item. Check Edge cases. + + @Test + void backStage_Pass_Expires_After_Concert() throws Exception { + Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 2, 15)}; + GildedRose app = new GildedRose(items); + app.updateQuality(); + app.updateQuality(); + app.updateQuality(); + assertEquals(-1, app.items[0].sellIn); + assertEquals(0, app.items[0].quality); + } + + @Test + public void backStage_quality_increases_twiceAsFast_10daysOrLess_before_concert() throws Exception { + Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 10, 20)}; + GildedRose app = new GildedRose(items); + + app.updateQuality(); + + assertEquals(9, app.items[0].sellIn); + assertEquals(22, app.items[0].quality); + } + + @Test + public void quality_Never_Below_zero() throws Exception { + Item[] items = new Item[]{new Item("Bread", 10, 0)}; + GildedRose app = new GildedRose(items); + + app.updateQuality(); + + assertEquals(9, app.items[0].sellIn); + assertEquals(0, app.items[0].quality); + } + + @Test + public void sulfuras_hasConstant_quality_and_sellIn() throws Exception { + Item[] items = new Item[]{new Item("Sulfuras, Hand of Ragnaros", 8, 30)}; + GildedRose app = new GildedRose(items); + + app.updateQuality(); + + assertEquals(8, app.items[0].sellIn); + assertEquals(30, app.items[0].quality); + } + + @Test + public void aged_brie_quality_isMax50() throws Exception { + Item[] items = new Item[]{new Item("Aged Brie", 10, 50)}; + GildedRose app = new GildedRose(items); + + app.updateQuality(); + + assertEquals(9, app.items[0].sellIn); + assertEquals(50, app.items[0].quality); + } + + @Test + public void backStage_quality_isMax50() throws Exception { + Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 10, 50)}; + GildedRose app = new GildedRose(items); + + app.updateQuality(); + + assertEquals(9, app.items[0].sellIn); + assertEquals(50, app.items[0].quality); + } + @Test + public void anyItem_quality_isMax50() throws Exception { + Item[] items = new Item[]{new Item("Bread", 10, 50)}; + GildedRose app = new GildedRose(items); + + app.updateQuality(); + + assertEquals(9, app.items[0].sellIn); + assertEquals(49, app.items[0].quality); } } diff --git a/Java/src/test/java/com/gildedrose/TexttestFixture.java b/Java/src/test/java/com/gildedrose/TexttestFixture.java index d059c88fc4..5e3b4addd6 100644 --- a/Java/src/test/java/com/gildedrose/TexttestFixture.java +++ b/Java/src/test/java/com/gildedrose/TexttestFixture.java @@ -18,7 +18,7 @@ public static void main(String[] args) { GildedRose app = new GildedRose(items); - int days = 2; + int days = 20; if (args.length > 0) { days = Integer.parseInt(args[0]) + 1; } From fef66224dd08e88368c4f7e9d9f67d72a27d4394 Mon Sep 17 00:00:00 2001 From: EGYThomas Date: Mon, 1 Jul 2024 11:21:43 +0300 Subject: [PATCH 2/2] Edited Comment in GildedRose. Removed commented code in GildedRose. --- .../main/java/com/gildedrose/GildedRose.java | 64 ++----------------- 1 file changed, 4 insertions(+), 60 deletions(-) diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index b4fc26bccc..62f26404b5 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,4 +1,5 @@ package com.gildedrose; + import com.gildedrose.ItemUpdater; class GildedRose { @@ -7,8 +8,9 @@ class GildedRose { public GildedRose(Item[] items) { this.items = items; } + public void updateQuality() { - // Instantiate updaters for each item type. (Can I instantiate itemupdater only when i need it?) + // Instantiate updaters for each item type ItemUpdater defaultUpdater = new DefaultItemUpdater(); ItemUpdater agedBrieUpdater = new AgedBrieUpdater(); ItemUpdater backstagePassUpdater = new BackStagePassUpdater(); @@ -34,62 +36,4 @@ public void updateQuality() { } } } - - - -// /* -// Aged Brie increases in quality the older it gets. -// */ -// private void updateAgedBrie(Item item) { -// if (item.quality == 50) -// return; -// item.quality = item.quality + 1; -// // Do I have to decrease sellIn? -// } -// -// -// private void updateAnyItem(Item item) { -// if (item.quality <= 0 || item.sellIn <= 0) -// return; -// if (item.sellIn > 0) -// item.quality = item.quality - 1; -// else -// item.quality = item.quality - 2; -// -// item.sellIn = item.sellIn - 1; -// } -// -// /* -// like aged brie, increases in Quality as its SellIn value approaches; -// Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but -// Quality drops to 0 after the concert -// */ -// private void updateBackStageTickets(Item item) { -// if (item.sellIn <= 0) { -// item.quality = 0; -// } else if (item.quality >= 50 && item.sellIn > 0) { -// item.sellIn--; -// } else if (item.sellIn < 10 && item.quality > 5) { -// item.quality = item.quality + 2; -// item.sellIn--; -// } else if (item.sellIn < 5) { -// item.quality = item.quality + 3; -// item.sellIn--; -// } -// } -// -// -// /* -// "Conjured" items degrade in Quality twice as fast as normal items -// */ -// private void updateConjured (Item item){ -// if (item.quality <= 0 || item.sellIn <= 0) -// return; -// if (item.sellIn > 0) -// item.quality = item.quality - 2; -// else -// item.quality = item.quality - 4; -// item.sellIn = item.sellIn - 1; -// } - } - +}