mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
proper refactor
now this i'm happy with. fixed a few bugs along the way.
This commit is contained in:
parent
33f9bc4d5f
commit
33f800aa51
@ -1,29 +1,22 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class GildedRose {
|
||||
Item[] items;
|
||||
private final List<ItemWrapper> itemWrappers;
|
||||
|
||||
public GildedRose(Item[] items) {
|
||||
this.items = items;
|
||||
this.itemWrappers = new ArrayList<>(items.length);
|
||||
Arrays.stream(items).forEach(item -> itemWrappers.add(new ItemWrapper(item)));
|
||||
}
|
||||
|
||||
public void updateQuality() {
|
||||
Arrays.stream(items).forEach(this::updateQuality);
|
||||
}
|
||||
|
||||
public void updateQuality(Item item) {
|
||||
if (item.name.equals("Aged Brie")) {
|
||||
AgedBrieStrategy.INSTANCE.applyTo(item);
|
||||
} else if (item.name.startsWith("Backstage passes")) {
|
||||
BackstagePassesStrategy.INSTANCE.applyTo(item);
|
||||
} else if (item.name.startsWith("Conjured")) {
|
||||
ConjuredStrategy.INSTANCE.applyTo(item);
|
||||
} else {
|
||||
DefaultStrategy.INSTANCE.applyTo(item);
|
||||
}
|
||||
itemWrappers.forEach(ItemWrapper::updateQuality);
|
||||
}
|
||||
}
|
||||
|
||||
32
Java/src/main/java/com/gildedrose/ItemWrapper.java
Normal file
32
Java/src/main/java/com/gildedrose/ItemWrapper.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.gildedrose.strategies.AgedBrieStrategy;
|
||||
import com.gildedrose.strategies.BackstagePassesStrategy;
|
||||
import com.gildedrose.strategies.ConjuredStrategy;
|
||||
import com.gildedrose.strategies.DefaultStrategy;
|
||||
import com.gildedrose.strategies.QualityUpdateStrategy;
|
||||
import com.gildedrose.strategies.SulfurasStrategy;
|
||||
|
||||
public class ItemWrapper {
|
||||
private final Item item;
|
||||
private final QualityUpdateStrategy strategy;
|
||||
|
||||
public ItemWrapper(Item item) {
|
||||
this.item = item;
|
||||
if (item.name.equals("Aged Brie")) {
|
||||
strategy = AgedBrieStrategy.INSTANCE;
|
||||
} else if (item.name.startsWith("Backstage passes")) {
|
||||
strategy = BackstagePassesStrategy.INSTANCE;
|
||||
} else if (item.name.startsWith("Conjured")) {
|
||||
strategy = ConjuredStrategy.INSTANCE;
|
||||
} else if (item.name.startsWith("Sulfuras")) {
|
||||
strategy = SulfurasStrategy.INSTANCE;
|
||||
} else {
|
||||
strategy = DefaultStrategy.INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
public void updateQuality() {
|
||||
strategy.applyTo(item);
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,13 @@
|
||||
package com.gildedrose;
|
||||
package com.gildedrose.strategies;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public enum AgedBrieStrategy implements QualityUpdateStrategy {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public void applyTo(Item item) {
|
||||
item.quality = Math.min(50, item.sellIn < 0 ? item.quality+2 : item.quality+1);
|
||||
item.quality = Math.min(50, item.sellIn <= 0 ? item.quality+2 : item.quality+1);
|
||||
--item.sellIn;
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
package com.gildedrose;
|
||||
package com.gildedrose.strategies;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public enum BackstagePassesStrategy implements QualityUpdateStrategy {
|
||||
INSTANCE;
|
||||
@ -7,9 +9,9 @@ public enum BackstagePassesStrategy implements QualityUpdateStrategy {
|
||||
public void applyTo(Item item) {
|
||||
if (item.sellIn <= 0) {
|
||||
item.quality = 0;
|
||||
} else if (item.sellIn >= 10) {
|
||||
} else if (item.sellIn > 10) {
|
||||
item.quality = Math.min(50, item.quality+1);
|
||||
} else if (item.sellIn >= 5) {
|
||||
} else if (item.sellIn > 5) {
|
||||
item.quality = Math.min(50, item.quality+2);
|
||||
} else {
|
||||
item.quality = Math.min(50, item.quality+3);
|
||||
@ -1,4 +1,6 @@
|
||||
package com.gildedrose;
|
||||
package com.gildedrose.strategies;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public enum ConjuredStrategy implements QualityUpdateStrategy {
|
||||
INSTANCE;
|
||||
@ -1,4 +1,6 @@
|
||||
package com.gildedrose;
|
||||
package com.gildedrose.strategies;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public enum DefaultStrategy implements QualityUpdateStrategy {
|
||||
INSTANCE;
|
||||
@ -6,6 +8,6 @@ public enum DefaultStrategy implements QualityUpdateStrategy {
|
||||
@Override
|
||||
public void applyTo(Item item) {
|
||||
item.quality = Math.max(0, item.sellIn > 0 ? (item.quality-1) : (item.quality-2));
|
||||
item.sellIn = item.sellIn - 1;
|
||||
--item.sellIn;
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
package com.gildedrose;
|
||||
package com.gildedrose.strategies;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public interface QualityUpdateStrategy {
|
||||
void applyTo(Item item);
|
||||
@ -0,0 +1,12 @@
|
||||
package com.gildedrose.strategies;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
public enum SulfurasStrategy implements QualityUpdateStrategy{
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public void applyTo(Item item) {
|
||||
//do nothing
|
||||
}
|
||||
}
|
||||
@ -108,7 +108,6 @@ class GildedRoseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
@DisplayName("Conjured Item")
|
||||
void updateQualityConjuredItem() {
|
||||
Item conjuredItemUnderTest = new Item("Conjured Sample Item", 1, 8);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user