mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
Refactor GildedRose class using Strategy Pattern for item update quality
This commit is contained in:
parent
ee4e35a2f8
commit
cbe262583e
@ -1,62 +1,99 @@
|
||||
package com.gildedrose;
|
||||
|
||||
class GildedRose {
|
||||
Item[] items;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public GildedRose(Item[] items) {
|
||||
this.items = items;
|
||||
class GildedRose {
|
||||
|
||||
private final static int MIN_QUALITY = 0;
|
||||
private final static int MAX_QUALITY = 50;
|
||||
|
||||
private final Item[] items;
|
||||
private final Map<String, UpdateQualityStrategy> strategyMap;
|
||||
|
||||
private interface UpdateQualityStrategy {
|
||||
void updateQuality(Item item);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private static class NormalItemUpdateStrategy implements UpdateQualityStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
if (item.quality > MIN_QUALITY) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
item.sellIn = item.sellIn - 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;
|
||||
}
|
||||
}
|
||||
if (item.sellIn < 0 && item.quality > MIN_QUALITY) {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class BackstagePassesUpdateStrategy implements UpdateQualityStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
if (item.quality < MAX_QUALITY) {
|
||||
item.quality = item.quality + 1;
|
||||
|
||||
if (item.sellIn < 11 && item.quality < MAX_QUALITY) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
|
||||
if (item.sellIn < 6 && item.quality < MAX_QUALITY) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
|
||||
item.sellIn = item.sellIn - 1;
|
||||
|
||||
if (item.sellIn < MIN_QUALITY) {
|
||||
item.quality = MIN_QUALITY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class AgedBrieUpdateStrategy implements UpdateQualityStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
if (item.quality < MAX_QUALITY) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
|
||||
item.sellIn = item.sellIn - 1;
|
||||
|
||||
if (item.sellIn < MIN_QUALITY && item.quality < MAX_QUALITY) {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class SulfurasUpdateStrategy implements UpdateQualityStrategy {
|
||||
@Override
|
||||
public void updateQuality(Item item) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public GildedRose(Item[] items) {
|
||||
this.items = items;
|
||||
this.strategyMap = new HashMap<String, UpdateQualityStrategy>(){{
|
||||
//default value
|
||||
put(null, new NormalItemUpdateStrategy());
|
||||
// update strategies for each possible key
|
||||
put("Backstage passes to a TAFKAL80ETC concert", new BackstagePassesUpdateStrategy());
|
||||
put("Aged Brie", new AgedBrieUpdateStrategy());
|
||||
put("Sulfuras, Hand of Ragnaros", new SulfurasUpdateStrategy());
|
||||
}};
|
||||
}
|
||||
|
||||
public void updateQuality() {
|
||||
for (Item item : items) {
|
||||
UpdateQualityStrategy strategy = strategyMap.getOrDefault(item.name, strategyMap.get(null));
|
||||
strategy.updateQuality(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user