Refactored Code : Moved implementation of expired items quality update to specific classes.

This commit is contained in:
lekshmysasidhar 2023-03-31 12:31:33 +02:00
parent cec7b6ad04
commit df61c9c901
11 changed files with 98 additions and 40 deletions

View File

@ -8,6 +8,14 @@ public class AgedBrie implements Goods {
addQualityWhenWithInLimit(item);
}
@Override
public void updateQualityForExpiredItem(Item item) {
if (item.sellIn < 0) {
addQualityWhenWithInLimit(item);
}
}
private void addQualityWhenWithInLimit(Item item) {
if (item.quality < MAX_ALLOWED_QUALITY) {

View File

@ -5,6 +5,7 @@ public class BackStagePasses implements Goods {
private static final int MAX_ALLOWED_QUALITY = 50;
private static final int SELL_IN_MAX_THRESHOLD_DAY = 11;
private static final int SELL_IN_MIN_THRESHOLD_DAY = 6;
private static final int ZERO = 0;
@Override
public void updateQuality(Item item) {
@ -22,4 +23,11 @@ public class BackStagePasses implements Goods {
item.quality++;
}
}
@Override
public void updateQualityForExpiredItem(Item item) {
if (item.sellIn < 0) {
item.quality = ZERO;
}
}
}

View File

@ -7,4 +7,11 @@ public class Generic implements Goods {
item.quality--;
}
}
@Override
public void updateQualityForExpiredItem(Item item) {
if (item.sellIn < 0 && item.quality > 0) {
item.quality--;
}
}
}

View File

@ -9,47 +9,13 @@ class GildedRose {
this.items = items;
}
public void updateQuality() {
public void updateQuality() {
for (Item item : items) {
Goods goods = GoodsFactory.getGoods(item.name);
goods.updateQuality(item);
goods.updateSellInDays(item);
updateQualityForExpiredItem(item);
}
}
private void addQualityWhenWithInLimit(Item item) {
if (item.quality < 50) {
item.quality = item.quality + 1;
goods.updateQualityForExpiredItem(item);
}
}
private void updateQualityForExpiredItem(Item item) {
if (item.sellIn < 0) {
if (!item.name.equals("Aged Brie")) {
if (!item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.quality > 0) {
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
updateQualityForExpiredItemNormal(item);
}
}
} else {
updateQualityForExpiredItemBackstagePasses(item);
}
} else {
addQualityWhenWithInLimit(item);
}
}
}
private void updateQualityForExpiredItemBackstagePasses(Item item) {
item.quality = 0;
}
private void updateQualityForExpiredItemNormal(Item item) {
item.quality--;
}
}

View File

@ -7,4 +7,6 @@ public interface Goods {
default void updateSellInDays(Item item) {
item.sellIn--;
}
public void updateQualityForExpiredItem(Item item);
}

View File

@ -15,4 +15,10 @@ public class Sulfuras implements Goods {
// decreases in quality.
}
@Override
public void updateQualityForExpiredItem(Item item) {
// Do nothing because Sulfuras being a legendary item, never has to be sold or
// decreases in quality.
}
}

View File

@ -23,6 +23,13 @@ class AgedBrieTest {
Arguments.of(new Item(GoodsType.AGED_BRIE.getGoodsName(), 1, 50), 0)
);
}
private static Stream<Arguments> getTestExpiredItemsWithExpectedQuality() {
return Stream.of(
Arguments.of(new Item(GoodsType.AGED_BRIE.getGoodsName(), -1, 2), 3),
Arguments.of(new Item(GoodsType.AGED_BRIE.getGoodsName(), 1, 2), 2)
);
}
@ParameterizedTest(name = "Test updateQuality - {index}")
@ -41,5 +48,13 @@ class AgedBrieTest {
assertEquals(expectedSellin, item.sellIn);
}
@ParameterizedTest(name = "Test updateQuality for expired items - {index}")
@MethodSource("getTestExpiredItemsWithExpectedQuality")
void testtestUpdateQualityForExpiredItems(Item item, int expectedQuality) {
AgedBrie agedBrie = new AgedBrie();
agedBrie.updateQualityForExpiredItem(item);
assertEquals(expectedQuality, item.quality);
}
}

View File

@ -24,6 +24,12 @@ class BackStagePassesTest {
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 1, 50), 0)
);
}
private static Stream<Arguments> getTestExpiredItemsWithExpectedQuality() {
return Stream.of(
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), -1, 2), 0),
Arguments.of(new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 1, 2), 2));
}
@ -33,7 +39,6 @@ class BackStagePassesTest {
BackStagePasses backStagePasses = new BackStagePasses();
backStagePasses.updateQuality(item);
assertEquals(expectedQuality, item.quality);
}
@ParameterizedTest(name = "Test updateSellIn - {index}")
@ -42,7 +47,14 @@ class BackStagePassesTest {
BackStagePasses backStagePasses = new BackStagePasses();
backStagePasses.updateSellInDays(item);
assertEquals(expectedSellin, item.sellIn);
}
@ParameterizedTest(name = "Test updateQuality for expired items - {index}")
@MethodSource("getTestExpiredItemsWithExpectedQuality")
void testUpdateQualityForExpiredItems(Item item, int expectedQuality) {
BackStagePasses backStagePasses = new BackStagePasses();
backStagePasses.updateQualityForExpiredItem(item);
assertEquals(expectedQuality, item.quality);
}
}

View File

@ -23,6 +23,14 @@ class GenericTest {
Arguments.of(new Item("Generic Item", 2, 2), 1)
);
}
private static Stream<Arguments> getTestExpiredItemsWithExpectedQuality() {
return Stream.of(
Arguments.of(new Item("Generic Item", -1, 1), 0),
Arguments.of(new Item("Generic Item", 0, 2), 2),
Arguments.of(new Item("Generic Item", -1, -1), -1)
);
}
@ -32,7 +40,6 @@ class GenericTest {
Generic generic = new Generic();
generic.updateQuality(item);
assertEquals(expectedQuality, item.quality);
}
@ParameterizedTest(name = "Test updateSellIn - {index}")
@ -42,5 +49,13 @@ class GenericTest {
generic.updateSellInDays(item);
assertEquals(expectedSellin, item.sellIn);
}
@ParameterizedTest(name = "Test updateQuality for expired items- {index}")
@MethodSource("getTestExpiredItemsWithExpectedQuality")
void testUpdateQualityForExpiredItems(Item item, int expectedQuality) {
Generic generic = new Generic();
generic.updateQualityForExpiredItem(item);
assertEquals(expectedQuality, item.quality);
}
}

View File

@ -45,7 +45,10 @@ class GildedRoseTest {
Arguments.of(new Item[] { new Item("Coffee Day", 1, 1) }, 0),
Arguments.of(new Item[] { new Item("Coffee Day", -1, 1) }, 0),
Arguments.of(new Item[] { new Item("Coffee Day", -1, 2) }, 0)
Arguments.of(new Item[] { new Item("Coffee Day", -1, 2) }, 0),
Arguments.of(new Item[] { new Item(GoodsType.SULFURAS.getGoodsName(), -1, 10) }, 10),
Arguments.of(new Item[] { new Item(GoodsType.SULFURAS.getGoodsName(), 1, 10) }, 10)
);
}

View File

@ -21,6 +21,13 @@ class SulfurasTest {
Arguments.of(new Item(GoodsType.SULFURAS.getGoodsName(), 2, 2), 2),
Arguments.of(new Item(GoodsType.SULFURAS.getGoodsName(), 1, 50), 1));
}
private static Stream<Arguments> getTestExpiredItemsWithExpectedQuality() {
return Stream.of(
Arguments.of(new Item(GoodsType.SULFURAS.getGoodsName(), -1, 2), 2),
Arguments.of(new Item(GoodsType.SULFURAS.getGoodsName(), -1, 50), 50)
);
}
@ParameterizedTest(name = "Test updateQuality - {index}")
@MethodSource("getTestItemsWithExpectedQuality")
@ -37,5 +44,14 @@ class SulfurasTest {
sulfurus.updateSellInDays(item);
assertEquals(expectedSellin, item.sellIn);
}
@ParameterizedTest(name = "Test updateQuality for expired items- {index}")
@MethodSource("getTestExpiredItemsWithExpectedQuality")
void testUpdateQualityForExpiredItems(Item item, int expectedQuality) {
Sulfuras sulfurus = new Sulfuras();
sulfurus.updateQualityForExpiredItem(item);
assertEquals(expectedQuality, item.quality);
}
}