Refactored to extract common code

Added standard item to cucumber features
This commit is contained in:
Xavier Levaux 2020-09-26 11:23:28 +02:00
parent 3792d69022
commit d68387dc6b
7 changed files with 66 additions and 28 deletions

View File

@ -0,0 +1,21 @@
package com.gildedrose;
public abstract class AbstractItemQualityUpdater implements ItemQualityUpdater {
protected Item item;
public AbstractItemQualityUpdater(Item item) {
this.item = item;
}
/**
* Decrease quality of an item, but never go below 0
* @param decrementValue Value the quality should be decreased by
*/
void decreaseQuality(int decrementValue) {
item.quality = item.quality - decrementValue;
if (item.quality < 0) {
item.quality = 0;
}
}
}

View File

@ -1,10 +1,10 @@
package com.gildedrose; package com.gildedrose;
public class BackstageBrieQualityUpdater implements ItemQualityUpdater { public class BackstageQualityUpdater implements ItemQualityUpdater {
private final Item item; private final Item item;
public BackstageBrieQualityUpdater(Item item) { public BackstageQualityUpdater(Item item) {
this.item = item; this.item = item;
} }

View File

@ -1,30 +1,22 @@
package com.gildedrose; package com.gildedrose;
public class ConjuredQualityUpdater implements ItemQualityUpdater { public class ConjuredQualityUpdater extends AbstractItemQualityUpdater {
private final Item item; private final int dailyQualityDegradation = 2;
private final int normalDailyQualityDegradation = 2;
public ConjuredQualityUpdater(Item item) { public ConjuredQualityUpdater(Item item) {
this.item = item; super(item);
} }
@Override @Override
public void updateQuality() { public void updateQuality() {
decreaseQuality(normalDailyQualityDegradation); decreaseQuality(dailyQualityDegradation);
item.sellIn = item.sellIn - 1; item.sellIn = item.sellIn - 1;
if (item.sellIn < 0) { if (item.sellIn < 0) {
decreaseQuality(normalDailyQualityDegradation); decreaseQuality(dailyQualityDegradation);
}
}
void decreaseQuality(int decrementValue) {
item.quality = item.quality - decrementValue;
if (item.quality < 0) {
item.quality = 0;
} }
} }
} }

View File

@ -1,25 +1,20 @@
package com.gildedrose; package com.gildedrose;
public class DefaultQualityUpdater implements ItemQualityUpdater { public class DefaultQualityUpdater extends AbstractItemQualityUpdater {
private final int dailyQualityDegradation = 1;
private final Item item;
public DefaultQualityUpdater(Item item) { public DefaultQualityUpdater(Item item) {
this.item = item; super(item);
} }
@Override @Override
public void updateQuality() { public void updateQuality() {
if (item.quality > 0) { decreaseQuality(dailyQualityDegradation);
item.quality = item.quality - 1;
}
item.sellIn = item.sellIn - 1; item.sellIn = item.sellIn - 1;
if (item.sellIn < 0) { if (item.sellIn < 0) {
if (item.quality > 0) { decreaseQuality(dailyQualityDegradation);
item.quality = item.quality - 1;
}
} }
} }
} }

View File

@ -5,7 +5,7 @@ public class ItemQualityUpdateStrategyFactory {
switch (item.name) { switch (item.name) {
case "Sulfuras, Hand of Ragnaros": return new SulfrasQualityUpdater(item); case "Sulfuras, Hand of Ragnaros": return new SulfrasQualityUpdater(item);
case "Aged Brie": return new AgedBrieQualityUpdater(item); case "Aged Brie": return new AgedBrieQualityUpdater(item);
case "Backstage passes to a TAFKAL80ETC concert": return new BackstageBrieQualityUpdater(item); case "Backstage passes to a TAFKAL80ETC concert": return new BackstageQualityUpdater(item);
case "Conjured": return new ConjuredQualityUpdater(item); case "Conjured": return new ConjuredQualityUpdater(item);
default: return new DefaultQualityUpdater(item); default: return new DefaultQualityUpdater(item);
} }

View File

@ -10,7 +10,7 @@ class ItemQualityUpdateStrategyFactoryTest {
void itemQualityUpdaterFor() { void itemQualityUpdaterFor() {
testFactoryFor("Sulfuras, Hand of Ragnaros", SulfrasQualityUpdater.class); testFactoryFor("Sulfuras, Hand of Ragnaros", SulfrasQualityUpdater.class);
testFactoryFor("Aged Brie", AgedBrieQualityUpdater.class); testFactoryFor("Aged Brie", AgedBrieQualityUpdater.class);
testFactoryFor("Backstage passes to a TAFKAL80ETC concert", BackstageBrieQualityUpdater.class); testFactoryFor("Backstage passes to a TAFKAL80ETC concert", BackstageQualityUpdater.class);
testFactoryFor("xyz", DefaultQualityUpdater.class); testFactoryFor("xyz", DefaultQualityUpdater.class);
testFactoryFor("Conjured", ConjuredQualityUpdater.class); testFactoryFor("Conjured", ConjuredQualityUpdater.class);
} }

View File

@ -12,3 +12,33 @@ Feature: Conjured items quality update
| Conjured | 1 | 1 | 0 | 0 | | Conjured | 1 | 1 | 0 | 0 |
| Conjured | 6 | 0 | 2 | -1 | | Conjured | 6 | 0 | 2 | -1 |
| Conjured | 4 | 0 | 0 | -1 | | Conjured | 4 | 0 | 0 | -1 |
Scenario Outline: Standard items quality degrades by 1 each day,
but degrade by 2 when sellIn is negative
Given name is "<name>", quality is "<quality>" and sellIn is "<sellIn>"
When I calculateQuality
Then I should have new quality "<newQuality>" and new sellIn "<newSellIn>"
Examples:
| name | quality | sellIn | newQuality | newSellIn |
| foo | 0 | -1 | 0 | -2 |
| foo | 1 | -1 | 0 | -2 |
| foo | 49 | -1 | 47 | -2 |
| foo | 50 | -1 | 48 | -2 |
| foo | 0 | 0 | 0 | -1 |
| foo | 1 | 0 | 0 | -1 |
| foo | 49 | 0 | 47 | -1 |
| foo | 50 | 0 | 48 | -1 |
| foo | 0 | 5 | 0 | 4 |
| foo | 1 | 5 | 0 | 4 |
| foo | 49 | 5 | 48 | 4 |
| foo | 50 | 5 | 49 | 4 |
| foo | 0 | 6 | 0 | 5 |
| foo | 1 | 6 | 0 | 5 |
| foo | 49 | 6 | 48 | 5 |
| foo | 50 | 6 | 49 | 5 |
| foo | 0 | 11 | 0 | 10 |
| foo | 1 | 11 | 0 | 10 |
| foo | 49 | 11 | 48 | 10 |
| foo | 50 | 11 | 49 | 10 |