mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-18 16:01:42 +00:00
Allow passing of quality decrease numbers via
the constructor We need to be able to configure other levels of decay. When passing no parameters, the behavior remains as it was
This commit is contained in:
parent
c31a2163dd
commit
2b4307cd31
@ -6,11 +6,23 @@ public class DecreasingQualityBehavior implements QualityBehavior {
|
||||
|
||||
public static final int MAX_QUALITY_LEVEL = 50;
|
||||
public static final int MIN_QUALITY_LEVEL = 0;
|
||||
public static final int DEFAULT_QUALITY_DECREASE = 1;
|
||||
public static final int NORMAL_QUALITY_DECREASE = 1;
|
||||
public static final int FASTER_QUALITY_DECREASE = 2;
|
||||
|
||||
private final int normalDecreaseSpeed;
|
||||
private final int fasterDecreaseSpeed;
|
||||
|
||||
private DecreasingQualityBehavior(int normalDecreaseSpeed, int fasterDecreaseSpeed) {
|
||||
this.normalDecreaseSpeed = normalDecreaseSpeed;
|
||||
this.fasterDecreaseSpeed = fasterDecreaseSpeed;
|
||||
}
|
||||
|
||||
public static DecreasingQualityBehavior newInstance() {
|
||||
return new DecreasingQualityBehavior();
|
||||
return new DecreasingQualityBehavior(NORMAL_QUALITY_DECREASE, FASTER_QUALITY_DECREASE);
|
||||
}
|
||||
|
||||
public static DecreasingQualityBehavior newInstance(int normalDecreaseSpeed, int fasterDecreaseSpeed) {
|
||||
return new DecreasingQualityBehavior(normalDecreaseSpeed, fasterDecreaseSpeed);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -23,7 +35,7 @@ public class DecreasingQualityBehavior implements QualityBehavior {
|
||||
}
|
||||
|
||||
private int getQualityDecreaseAmount(Item item) {
|
||||
return item.sellIn > 0 ? DEFAULT_QUALITY_DECREASE : FASTER_QUALITY_DECREASE;
|
||||
return item.sellIn > 0 ? normalDecreaseSpeed : fasterDecreaseSpeed;
|
||||
}
|
||||
private int limitQuality(int newQuality) {
|
||||
return Math.max(MIN_QUALITY_LEVEL, Math.min(MAX_QUALITY_LEVEL, newQuality));
|
||||
|
||||
@ -9,10 +9,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
public class DecreasingQualityBehaviorTest {
|
||||
|
||||
private QualityBehavior qualityBehavior;
|
||||
private QualityBehavior fastDecreasingQualityBehavior;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
qualityBehavior = new DecreasingQualityBehavior();
|
||||
qualityBehavior = DecreasingQualityBehavior.newInstance();
|
||||
fastDecreasingQualityBehavior = DecreasingQualityBehavior.newInstance(2,4);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -63,6 +65,54 @@ public class DecreasingQualityBehaviorTest {
|
||||
assertEquals(0, item.quality);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fastDecreaseQuality() {
|
||||
Item item = getItem(5,10);
|
||||
fastDecreasingQualityBehavior.processQualityUpdate(item);
|
||||
|
||||
assertEquals(8, item.quality);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fastDecreaseNegativeQuality() {
|
||||
Item item = getItem(5,-10);
|
||||
fastDecreasingQualityBehavior.processQualityUpdate(item);
|
||||
|
||||
assertEquals(0, item.quality);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fastDecreaseQualityZero() {
|
||||
Item item = getItem(5,0);
|
||||
fastDecreasingQualityBehavior.processQualityUpdate(item);
|
||||
|
||||
assertEquals(0, item.quality);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fastDecreaseQualityFaster() {
|
||||
Item item = getItem(0,8);
|
||||
fastDecreasingQualityBehavior.processQualityUpdate(item);
|
||||
|
||||
assertEquals(4, item.quality);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fastDecreaseQualityFasterNegativeSellIn() {
|
||||
Item item = getItem(-1,8);
|
||||
fastDecreasingQualityBehavior.processQualityUpdate(item);
|
||||
|
||||
assertEquals(4, item.quality);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fastDecreaseQualityFasterRespectLowerLimit() {
|
||||
Item item = getItem(0,1);
|
||||
fastDecreasingQualityBehavior.processQualityUpdate(item);
|
||||
|
||||
assertEquals(0, item.quality);
|
||||
}
|
||||
|
||||
private Item getItem(int sellIn, int quality) {
|
||||
return new Item("SomeItem", sellIn, quality);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user