diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index e6feb751..9385b980 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -3,60 +3,88 @@ package com.gildedrose; class GildedRose { Item[] items; + String itemName; + int itemQuality; + int itemSellIn; + 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; + itemName = items[i].name; + itemQuality = items[i].quality; + itemSellIn = items[i].sellIn; + + if (!itemIsAgedBrie(itemName) + && !itemIsBackstagePasses(itemName)) { + if (itemQuality > 0) { + if (!itemIsSulfuras(itemName)) { + decreaseByOne(itemQuality); } } } else { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; + if (itemQuality < 50) { + itemQuality = itemQuality + 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 (itemIsBackstagePasses(itemName)) { + if (itemSellIn < 11) { + if (itemQuality < 50) { + increaseByOne(itemQuality); } } - if (items[i].sellIn < 6) { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; + if (itemSellIn < 6) { + if (itemQuality < 50) { + increaseByOne(itemQuality); } } } } } - if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) { - items[i].sellIn = items[i].sellIn - 1; + if (!itemIsSulfuras(itemName)) { + decreaseByOne(itemSellIn); } - 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; + if (itemSellIn < 0) { + if (!itemIsAgedBrie(itemName)) { + if (!itemIsBackstagePasses(itemName)) { + if (itemQuality > 0) { + if (!itemIsSulfuras(itemName)) { + decreaseByOne(itemQuality); } } } else { - items[i].quality = items[i].quality - items[i].quality; + decreaseByOne(itemQuality); } } else { - if (items[i].quality < 50) { - items[i].quality = items[i].quality + 1; + if (itemQuality < 50) { + increaseByOne(itemQuality); } } } } } -} \ No newline at end of file + + public void increaseByOne (int itemValue) { + itemValue++; + } + + public void decreaseByOne (int itemValue) { + itemValue--; + } + + public boolean itemIsSulfuras(String itemName) { + return ProjectConstants.SULFURAS.equals(itemName) ? true : false; + } + + public boolean itemIsBackstagePasses(String itemName) { + return ProjectConstants.BACKSTAGE_PASSES.equals(itemName) ? true : false; + } + + public boolean itemIsAgedBrie(String itemName) { + return ProjectConstants.AGED_BRIE.equals(itemName) ? true : false; + } +} diff --git a/Java/src/main/java/com/gildedrose/ProjectConstants.java b/Java/src/main/java/com/gildedrose/ProjectConstants.java new file mode 100644 index 00000000..2cb087d2 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/ProjectConstants.java @@ -0,0 +1,7 @@ +package com.gildedrose; + +class ProjectConstants { + public static final String AGED_BRIE = "Aged Brie"; + public static final String BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert"; + public static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; +} diff --git a/Java/src/test/java/com/gildedrose/GildedRoseTest.java b/Java/src/test/java/com/gildedrose/GildedRoseTest.java index 8ae29eec..490a1670 100644 --- a/Java/src/test/java/com/gildedrose/GildedRoseTest.java +++ b/Java/src/test/java/com/gildedrose/GildedRoseTest.java @@ -1,17 +1,46 @@ package com.gildedrose; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeAll; + +import static org.junit.jupiter.api.Assertions.*; -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); + GildedRose app; + + @BeforeAll + public void init() { + Item[] items = new Item[] { new Item(ProjectConstants.SULFURAS, 0, 0) }; + app = new GildedRose(items); } + @Test + public void increaseByOne() { + int initial = 2; + app.increaseByOne(initial); + assertEquals("3", initial); + } + + @Test + public void decreaseByOne() { + int initial = 2; + app.decreaseByOne(initial); + assertEquals("1", initial); + } + + @Test + public void itemIsSulfuras() { + String itemName = app.items[0].name; + boolean result = app.itemIsSulfuras(itemName); + assertTrue(result); + } + + @Test + public void ItemIsAgedBrieFalse() { + String itemName = app.items[0].name; + boolean result = app.itemIsAgedBrie(itemName); + assertFalse(result); + } }