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;
public class BackstageBrieQualityUpdater implements ItemQualityUpdater {
public class BackstageQualityUpdater implements ItemQualityUpdater {
private final Item item;
public BackstageBrieQualityUpdater(Item item) {
public BackstageQualityUpdater(Item item) {
this.item = item;
}

View File

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

View File

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

View File

@ -5,7 +5,7 @@ public class ItemQualityUpdateStrategyFactory {
switch (item.name) {
case "Sulfuras, Hand of Ragnaros": return new SulfrasQualityUpdater(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);
default: return new DefaultQualityUpdater(item);
}

View File

@ -10,7 +10,7 @@ class ItemQualityUpdateStrategyFactoryTest {
void itemQualityUpdaterFor() {
testFactoryFor("Sulfuras, Hand of Ragnaros", SulfrasQualityUpdater.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("Conjured", ConjuredQualityUpdater.class);
}

View File

@ -11,4 +11,34 @@ Feature: Conjured items quality update
| Conjured | 3 | 2 | 1 | 1 |
| Conjured | 1 | 1 | 0 | 0 |
| 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 |