mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-06-11 03:08:30 +00:00
training refactor
This commit is contained in:
parent
3e0085bfd0
commit
8acefd2ab3
@ -1,5 +1,8 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import com.gildedrose.strategy.ItemStrategyFactory;
|
||||||
|
import com.gildedrose.strategy.ItemStrategyUpdate;
|
||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
@ -9,54 +12,9 @@ class GildedRose {
|
|||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
for (int i = 0; i < items.length; i++) {
|
for (int i = 0; i < items.length; i++) {
|
||||||
if (!items[i].name.equals("Aged Brie")
|
ItemStrategyUpdate strategy = ItemStrategyFactory.getStrategy(items[i]);
|
||||||
&& !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
strategy.update(items[i]);
|
||||||
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,16 @@
|
|||||||
|
package com.gildedrose.strategy;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class AgedBrieStrategy implements ItemStrategyUpdate{
|
||||||
|
@Override
|
||||||
|
public void update(Item item)
|
||||||
|
{
|
||||||
|
item.sellIn--;
|
||||||
|
if (item.quality < 50)
|
||||||
|
item.quality++;
|
||||||
|
if(item.quality < 50 && item.sellIn < 0)
|
||||||
|
item.quality++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
package com.gildedrose.strategy;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class ConcertStrategy implements ItemStrategyUpdate{
|
||||||
|
@Override
|
||||||
|
public void update(Item item){
|
||||||
|
if (item.sellIn == 0)
|
||||||
|
item.quality = 0;
|
||||||
|
else {
|
||||||
|
if (item.quality < 50)
|
||||||
|
item.quality++;
|
||||||
|
if (item.sellIn < 11 && item.quality < 50)
|
||||||
|
item.quality++;
|
||||||
|
if (item.sellIn < 6 && item.quality < 50)
|
||||||
|
item.quality++;
|
||||||
|
}
|
||||||
|
item.sellIn--;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
package com.gildedrose.strategy;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class DefaultItemStrategy implements ItemStrategyUpdate{
|
||||||
|
@Override
|
||||||
|
public void update(Item item){
|
||||||
|
if (item.quality > 0)
|
||||||
|
item.quality--;
|
||||||
|
item.sellIn--;
|
||||||
|
if(item.sellIn < 0 && item.quality > 0)
|
||||||
|
item.quality--;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package com.gildedrose.strategy;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class ItemStrategyFactory {
|
||||||
|
public static ItemStrategyUpdate getStrategy(Item item){
|
||||||
|
switch (item.name) {
|
||||||
|
case "Aged Brie": return new AgedBrieStrategy();
|
||||||
|
case "Backstage passes to a TAFKAL80ETC concert": return new ConcertStrategy();
|
||||||
|
case "Sulfuras, Hand of Ragnaros": return new SulfurasStrategy();
|
||||||
|
default: return new DefaultItemStrategy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
package com.gildedrose.strategy;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public interface ItemStrategyUpdate {
|
||||||
|
void update(Item item);
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package com.gildedrose.strategy;
|
||||||
|
|
||||||
|
import com.gildedrose.Item;
|
||||||
|
|
||||||
|
public class SulfurasStrategy implements ItemStrategyUpdate{
|
||||||
|
@Override
|
||||||
|
public void update(Item item){}
|
||||||
|
}
|
||||||
@ -11,7 +11,17 @@ class GildedRoseTest {
|
|||||||
Item[] items = new Item[] { new Item("foo", 0, 0) };
|
Item[] items = new Item[] { new Item("foo", 0, 0) };
|
||||||
GildedRose app = new GildedRose(items);
|
GildedRose app = new GildedRose(items);
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
assertEquals("fixme", app.items[0].name);
|
assertEquals("foo", app.items[0].name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void defaultItem_shouldDecreaseSellInAndQualityEachDay(){
|
||||||
|
Item[] items = new Item[] { new Item("vest", 20, 19) };
|
||||||
|
GildedRose app = new GildedRose(items);
|
||||||
|
app.updateQuality();
|
||||||
|
assertEquals(19, app.items[0].sellIn);
|
||||||
|
assertEquals(18, app.items[0].quality);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user