mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-10 12:11:20 +00:00
GildedRose Refactoring changes
This commit is contained in:
parent
d73f961089
commit
5e8561a113
@ -1,5 +1,8 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import com.gildedrose.updaters.ItemUpdater;
|
||||||
|
import com.gildedrose.updaters.ItemUpdaterFactory;
|
||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
@ -8,55 +11,9 @@ class GildedRose {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
for (int i = 0; i < items.length; i++) {
|
for (Item item : items) {
|
||||||
if (!items[i].name.equals("Aged Brie")
|
ItemUpdater updater = ItemUpdaterFactory.getUpdater(item);
|
||||||
&& !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
updater.update();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,8 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public class GildedRoseConstants {
|
||||||
|
public static final String AGED_BRIE = "Aged Brie";
|
||||||
|
public static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||||
|
public static final String BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
|
public static final String CONJURED = "conjured";
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package com.gildedrose.updaters;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class AgedBrieUpdater extends ItemUpdater {
|
||||||
|
public AgedBrieUpdater(Item item) {
|
||||||
|
super(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update() {
|
||||||
|
increaseQuality(1);
|
||||||
|
decreaseSellIn();
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
increaseQuality(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.gildedrose.updaters;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class BackstagePassUpdater extends ItemUpdater {
|
||||||
|
public BackstagePassUpdater(Item item) {
|
||||||
|
super(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update() {
|
||||||
|
if (item.sellIn <= 0) {
|
||||||
|
item.quality = 0;
|
||||||
|
} else if (item.sellIn <= 5) {
|
||||||
|
increaseQuality(3);
|
||||||
|
} else if (item.sellIn <= 10) {
|
||||||
|
increaseQuality(2);
|
||||||
|
} else {
|
||||||
|
increaseQuality(1);
|
||||||
|
}
|
||||||
|
decreaseSellIn();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package com.gildedrose.updaters;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class ConjuredItemUpdater extends ItemUpdater {
|
||||||
|
public ConjuredItemUpdater(Item item) {
|
||||||
|
super(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update() {
|
||||||
|
decreaseQuality(2);
|
||||||
|
decreaseSellIn();
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
decreaseQuality(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
Java/src/main/java/com/gildedrose/updaters/ItemUpdater.java
Normal file
25
Java/src/main/java/com/gildedrose/updaters/ItemUpdater.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package com.gildedrose.updaters;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public abstract class ItemUpdater {
|
||||||
|
protected final Item item;
|
||||||
|
|
||||||
|
public ItemUpdater(Item item) {
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void update();
|
||||||
|
|
||||||
|
protected void increaseQuality(int amount) {
|
||||||
|
item.quality = Math.min(50, item.quality + amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void decreaseQuality(int amount) {
|
||||||
|
item.quality = Math.max(0, item.quality - amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void decreaseSellIn() {
|
||||||
|
item.sellIn--;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package com.gildedrose.updaters;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
import static com.gildedrose.GildedRoseConstants.*;
|
||||||
|
|
||||||
|
public class ItemUpdaterFactory {
|
||||||
|
public static ItemUpdater getUpdater(Item item) {
|
||||||
|
String name = item.name;
|
||||||
|
|
||||||
|
if (name.equals(AGED_BRIE)) {
|
||||||
|
return new AgedBrieUpdater(item);
|
||||||
|
} else if (name.equals(SULFURAS)) {
|
||||||
|
return new SulfurasUpdater(item);
|
||||||
|
} else if (name.equals(BACKSTAGE_PASS)) {
|
||||||
|
return new BackstagePassUpdater(item);
|
||||||
|
} else if (name.toLowerCase().contains(CONJURED)) {
|
||||||
|
return new ConjuredItemUpdater(item);
|
||||||
|
} else {
|
||||||
|
return new NormalItemUpdater(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package com.gildedrose.updaters;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class NormalItemUpdater extends ItemUpdater {
|
||||||
|
public NormalItemUpdater(Item item) {
|
||||||
|
super(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update() {
|
||||||
|
decreaseQuality(1);
|
||||||
|
decreaseSellIn();
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
decreaseQuality(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package com.gildedrose.updaters;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class SulfurasUpdater extends ItemUpdater {
|
||||||
|
public SulfurasUpdater(Item item) {
|
||||||
|
super(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update() {
|
||||||
|
// Legendary item, do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,16 +2,34 @@ package com.gildedrose;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static com.gildedrose.GildedRoseConstants.*;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
class GildedRoseTest {
|
public class GildedRoseTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void foo() {
|
void agedBrieIncreasesInQuality() {
|
||||||
Item[] items = new Item[] { new Item("foo", 0, 0) };
|
Item[] items = new Item[] { new Item(AGED_BRIE, 2, 0) };
|
||||||
GildedRose app = new GildedRose(items);
|
GildedRose app = new GildedRose(items);
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
assertEquals("fixme", app.items[0].name);
|
assertEquals(1, items[0].quality);
|
||||||
|
assertEquals(1, items[0].sellIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sulfurasDoesNotChange() {
|
||||||
|
Item[] items = new Item[] { new Item(SULFURAS, 0, 80) };
|
||||||
|
GildedRose app = new GildedRose(items);
|
||||||
|
app.updateQuality();
|
||||||
|
assertEquals(80, items[0].quality);
|
||||||
|
assertEquals(0, items[0].sellIn);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void backstagePassIncreasesCorrectly() {
|
||||||
|
Item[] items = new Item[] { new Item(BACKSTAGE_PASS, 5, 10) };
|
||||||
|
GildedRose app = new GildedRose(items);
|
||||||
|
app.updateQuality();
|
||||||
|
assertEquals(13, items[0].quality);
|
||||||
|
assertEquals(4, items[0].sellIn);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user