Add staged quality increase behavior

Depending on the sellIn value, we can have a different rate of quality
increase. There always needs to be a default quality increase defined.
And then we need to define the stages at with the quality increase
changes.
This commit is contained in:
Bjorn Misseghers 2021-04-13 10:06:21 +02:00
parent e68fab24b4
commit a7ff7f32cd
3 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.gildedrose.behavior.quality;
public class QualityStage {
private final int sellInCutoff;
private final int qualityIncrease;
private QualityStage(int sellInCutoff, int qualityIncrease) {
this.sellInCutoff = sellInCutoff;
this.qualityIncrease = qualityIncrease;
}
public static QualityStage of(int sellInCutoff, int qualityIncrease) {
return new QualityStage(sellInCutoff, qualityIncrease);
}
public int getSellInCutoff() {
return sellInCutoff;
}
public int getQualityIncrease() {
return qualityIncrease;
}
}

View File

@ -0,0 +1,52 @@
package com.gildedrose.behavior.quality;
import com.gildedrose.Item;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class StagedIncreasingQualityBehavior implements QualityBehavior {
public static final int MAX_QUALITY_LEVEL = 50;
public static final int MIN_QUALITY_LEVEL = 0;
private final List<QualityStage> stages;
private final QualityStage defaultStage;
private StagedIncreasingQualityBehavior(List<QualityStage> stages, int defaultQualityIncrease) {
this.stages = getSortedQualityStages(stages);
this.defaultStage = QualityStage.of(0, defaultQualityIncrease);
}
public static StagedIncreasingQualityBehavior withStages(List<QualityStage> stages, int defaultQualityIncrease) {
return new StagedIncreasingQualityBehavior(stages, defaultQualityIncrease);
}
@Override
public void processQualityUpdate(Item item) {
increaseQuality(item);
}
private void increaseQuality(Item item) {
if (item.sellIn <= 0) {
item.quality = 0;
} else {
final QualityStage stage = findStage(item);
item.quality = limitQuality(item.quality + stage.getQualityIncrease());
}
}
private int limitQuality(int newQuality) {
return Math.max(MIN_QUALITY_LEVEL, Math.min(MAX_QUALITY_LEVEL, newQuality));
}
private QualityStage findStage(Item item) {
return stages.stream().filter(qualityStage -> qualityStage.getSellInCutoff() >= item.sellIn)
.findFirst().orElse(defaultStage);
}
private List<QualityStage> getSortedQualityStages(List<QualityStage> stagesUnsorted) {
return stagesUnsorted.stream().sorted(Comparator.comparingInt(QualityStage::getSellInCutoff)).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,59 @@
package com.gildedrose.behavior.quality;
import com.gildedrose.Item;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StagedIncreasingQualityBehaviorTest {
private QualityBehavior stagedQualityBehavior;
private QualityStage stage1 = QualityStage.of(10, 2);
private QualityStage stage2 = QualityStage.of(5, 3);
@BeforeEach
public void setUp() throws Exception {
stagedQualityBehavior = StagedIncreasingQualityBehavior.withStages(new ArrayList<>(Arrays.asList(stage1, stage2)), 1);
}
@Test
void stagedIncrease() {
Item item = getItem(11, 1);
stagedQualityBehavior.processQualityUpdate(item);
assertEquals(2, item.quality);
item.sellIn = 10;
stagedQualityBehavior.processQualityUpdate(item);
assertEquals(4, item.quality);
item.sellIn = 6;
stagedQualityBehavior.processQualityUpdate(item);
assertEquals(6, item.quality);
item.sellIn = 5;
stagedQualityBehavior.processQualityUpdate(item);
assertEquals(9, item.quality);
item.sellIn = 1;
stagedQualityBehavior.processQualityUpdate(item);
assertEquals(12, item.quality);
item.sellIn = 0;
stagedQualityBehavior.processQualityUpdate(item);
assertEquals(0, item.quality);
item.sellIn = -10;
item.quality = 10;
stagedQualityBehavior.processQualityUpdate(item);
assertEquals(0, item.quality);
}
private Item getItem(int sellIn, int quality) {
return new Item("SomeItem", sellIn, quality);
}
}