From 2b4307cd3185ed0619211709242b5b9edc3a2148 Mon Sep 17 00:00:00 2001 From: Bjorn Misseghers Date: Tue, 13 Apr 2021 11:25:58 +0200 Subject: [PATCH] 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 --- .../quality/DecreasingQualityBehavior.java | 18 +++++-- .../DecreasingQualityBehaviorTest.java | 52 ++++++++++++++++++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/Java/src/main/java/com/gildedrose/behavior/quality/DecreasingQualityBehavior.java b/Java/src/main/java/com/gildedrose/behavior/quality/DecreasingQualityBehavior.java index 4b00258e..7c668254 100644 --- a/Java/src/main/java/com/gildedrose/behavior/quality/DecreasingQualityBehavior.java +++ b/Java/src/main/java/com/gildedrose/behavior/quality/DecreasingQualityBehavior.java @@ -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)); diff --git a/Java/src/test/java/com/gildedrose/behavior/quality/DecreasingQualityBehaviorTest.java b/Java/src/test/java/com/gildedrose/behavior/quality/DecreasingQualityBehaviorTest.java index ff53009c..60ef973b 100644 --- a/Java/src/test/java/com/gildedrose/behavior/quality/DecreasingQualityBehaviorTest.java +++ b/Java/src/test/java/com/gildedrose/behavior/quality/DecreasingQualityBehaviorTest.java @@ -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); }