mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 17:21:38 +00:00
Added ItemUpdater Interface with Implementations for each item class. Added Tests. Updated GildedRose updateItems() method.
This commit is contained in:
parent
e2abba77cb
commit
b7c2e8b3d7
@ -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;
|
||||
// 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].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;
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// /*
|
||||
// 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;
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
52
Java/src/main/java/com/gildedrose/ItemUpdater.java
Normal file
52
Java/src/main/java/com/gildedrose/ItemUpdater.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
@ -3,15 +3,96 @@ package com.gildedrose;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ public class TexttestFixture {
|
||||
|
||||
GildedRose app = new GildedRose(items);
|
||||
|
||||
int days = 2;
|
||||
int days = 20;
|
||||
if (args.length > 0) {
|
||||
days = Integer.parseInt(args[0]) + 1;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user