mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 06:21:29 +00:00
feat: conjured-items - Refactored the code to use the strategies.
This commit is contained in:
parent
5e318b83ec
commit
d559f07acd
@ -1,14 +0,0 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class AgedBrieItemStrategyImpl implements ItemStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
item.sellIn = item.sellIn - 1;
|
||||
if (item.sellIn < 0) {
|
||||
item.quality = item.quality < 50 ? item.quality + 1 : 50;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class BackStageItemStrategyImpl implements ItemStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
if (item.sellIn < 11) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (item.sellIn < 6) {
|
||||
if (item.quality < 50) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
item.sellIn = item.sellIn - 1;
|
||||
|
||||
if (item.sellIn < 0) {
|
||||
item.quality = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public class ConjuredItemStrategyImpl implements ItemStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
item.quality = item.quality > 2 ? item.quality - 2 : 0;
|
||||
item.sellIn = item.sellIn - 1;
|
||||
}
|
||||
}
|
||||
@ -1,62 +1,61 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.gildedrose.strategy.AgedBrieItemStrategyImpl;
|
||||
import com.gildedrose.strategy.BackStageItemStrategyImpl;
|
||||
import com.gildedrose.strategy.ConjuredItemStrategyImpl;
|
||||
import com.gildedrose.strategy.ItemStrategy;
|
||||
import com.gildedrose.strategy.SulfurasItemStrategyImpl;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
class GildedRose {
|
||||
Item[] items;
|
||||
private static final ItemStrategy DEFAULT_STRATEGY = new ItemStrategy() {
|
||||
};
|
||||
private static final Map<String, ItemStrategy> STRATEGIES = new TreeMap<>();
|
||||
|
||||
public GildedRose(Item[] items) {
|
||||
this.items = items;
|
||||
static final String AGED_BRIE = "Aged Brie";
|
||||
static final String CONJURED = "Conjured Mana Cake";
|
||||
static final String DEXTERITY_VEST = "+5 Dexterity Vest";
|
||||
static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||
static final String ELIXIR_OF_THE_MONGOOSE = "Elixir of the Mongoose";
|
||||
static final String BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert";
|
||||
|
||||
private static final List<String> KNOWN_ITEM_NAMES =
|
||||
List.of(AGED_BRIE, CONJURED, DEXTERITY_VEST, SULFURAS, ELIXIR_OF_THE_MONGOOSE,
|
||||
BACKSTAGE_PASSES);
|
||||
|
||||
static {
|
||||
STRATEGIES.put(DEXTERITY_VEST, DEFAULT_STRATEGY);
|
||||
STRATEGIES.put(ELIXIR_OF_THE_MONGOOSE, DEFAULT_STRATEGY);
|
||||
STRATEGIES.put(AGED_BRIE, new AgedBrieItemStrategyImpl());
|
||||
STRATEGIES.put(CONJURED, new ConjuredItemStrategyImpl());
|
||||
STRATEGIES.put(SULFURAS, new SulfurasItemStrategyImpl());
|
||||
STRATEGIES.put(BACKSTAGE_PASSES, new BackStageItemStrategyImpl());
|
||||
}
|
||||
|
||||
private final Item[] items;
|
||||
|
||||
public GildedRose(Item[] items) {
|
||||
if (items == null || items.length == 0) {
|
||||
throw new IllegalArgumentException("Items cannot be empty");
|
||||
}
|
||||
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;
|
||||
public void updateQuality() {
|
||||
Arrays.stream(items).forEach(item -> findStrategy(item).updateQuality(item));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private ItemStrategy findStrategy(Item item) {
|
||||
if (KNOWN_ITEM_NAMES.contains(item.name)) {
|
||||
return STRATEGIES.get(item.name);
|
||||
}
|
||||
return DEFAULT_STRATEGY;
|
||||
}
|
||||
|
||||
public Item[] getItems() {
|
||||
return items;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public interface ItemStrategy {
|
||||
default void updateQuality(Item item) {
|
||||
if (item.quality > 0) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
item.sellIn = item.sellIn - 1;
|
||||
if (item.sellIn < 0 && item.quality > 0) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.gildedrose.strategy;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public class AgedBrieItemStrategyImpl implements ItemStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
item.sellIn--;
|
||||
int increment = item.sellIn < 0 ? 2 * INCREASE_RATE : INCREASE_RATE;
|
||||
increaseQuality(item, increment);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package com.gildedrose.strategy;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public class BackStageItemStrategyImpl implements ItemStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
item.sellIn--;
|
||||
|
||||
if (item.sellIn < 0) {
|
||||
item.quality = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
int increment = INCREASE_RATE;
|
||||
if (item.sellIn < 5) {
|
||||
increment += 2;
|
||||
} else if (item.sellIn < 10) {
|
||||
increment++;
|
||||
}
|
||||
|
||||
increaseQuality(item, increment);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package com.gildedrose.strategy;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public class ConjuredItemStrategyImpl implements ItemStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
item.sellIn--;
|
||||
int decrement = item.sellIn < 0 ? 4 * DEGRADATION_RATE : 2 * DEGRADATION_RATE;
|
||||
decreaseQuality(item, decrement);
|
||||
}
|
||||
}
|
||||
24
Java/src/main/java/com/gildedrose/strategy/ItemStrategy.java
Normal file
24
Java/src/main/java/com/gildedrose/strategy/ItemStrategy.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.gildedrose.strategy;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public interface ItemStrategy {
|
||||
int MINIMUM_QUALITY = 0;
|
||||
int MAXIMUM_QUALITY = 50;
|
||||
int DEGRADATION_RATE = 1;
|
||||
int INCREASE_RATE = 1;
|
||||
|
||||
default void updateQuality(Item item) {
|
||||
item.sellIn--;
|
||||
int decrement = item.sellIn < 0 ? 2 * DEGRADATION_RATE : DEGRADATION_RATE;
|
||||
decreaseQuality(item, decrement);
|
||||
}
|
||||
|
||||
default void increaseQuality(Item item, int amount) {
|
||||
item.quality = Math.min(MAXIMUM_QUALITY, item.quality + amount);
|
||||
}
|
||||
|
||||
default void decreaseQuality(Item item, int amount) {
|
||||
item.quality = Math.max(MINIMUM_QUALITY, item.quality - amount);
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,10 @@
|
||||
package com.gildedrose;
|
||||
package com.gildedrose.strategy;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public class SulfurasItemStrategyImpl implements ItemStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
// Quality remains intact. Not for sel!
|
||||
// Quality remains unchanged. Not for sale!
|
||||
}
|
||||
}
|
||||
@ -1,17 +1,15 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class GildedRoseTest {
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class GildedRoseTest {
|
||||
@Test
|
||||
void foo() {
|
||||
Item[] items = new Item[] { new Item("foo", 0, 0) };
|
||||
void test() {
|
||||
Item[] items = new Item[] {new Item("foo", 0, 0)};
|
||||
GildedRose app = new GildedRose(items);
|
||||
app.updateQuality();
|
||||
assertEquals("fixme", app.items[0].name);
|
||||
assertEquals("foo", app.getItems()[0].name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -5,16 +5,16 @@ public class TexttestFixture {
|
||||
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) };
|
||||
new Item(GildedRose.DEXTERITY_VEST, 10, 20), //
|
||||
new Item(GildedRose.AGED_BRIE, 2, 0), //
|
||||
new Item(GildedRose.ELIXIR_OF_THE_MONGOOSE, 5, 7), //
|
||||
new Item(GildedRose.SULFURAS, 0, 80), //
|
||||
new Item(GildedRose.SULFURAS, -1, 80),
|
||||
new Item(GildedRose.BACKSTAGE_PASSES, 15, 20),
|
||||
new Item(GildedRose.BACKSTAGE_PASSES, 10, 49),
|
||||
new Item(GildedRose.BACKSTAGE_PASSES, 5, 49),
|
||||
// this conjured item does not work properly yet
|
||||
new Item(GildedRose.CONJURED, 3, 6)};
|
||||
|
||||
GildedRose app = new GildedRose(items);
|
||||
|
||||
@ -33,5 +33,4 @@ public class TexttestFixture {
|
||||
app.updateQuality();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user