Adapted logic to handle new product- Conjured

This commit is contained in:
lekshmysasidhar 2023-03-31 13:02:59 +02:00
parent 7e180efd78
commit 0b064047da
5 changed files with 80 additions and 3 deletions

View File

@ -0,0 +1,25 @@
package com.gildedrose;
import static com.gildedrose.rule.ValidationRule.*;
public class Conjured implements Goods {
@Override
public void updateQuality(Item item) {
reduceQualityTwice(item);
}
@Override
public void updateQualityForExpiredItem(Item item) {
if (isExpired(item)) {
reduceQualityTwice(item);
}
}
private void reduceQualityTwice(Item item) {
if (hasMinimumRequiredQuality(item)) {
item.quality = Math.max(0, item.quality - 2);
}
}
}

View File

@ -4,7 +4,9 @@ public enum GoodsType {
AGED_BRIE("Aged Brie"),
BACK_STAGE_PASSES("Backstage passes to a TAFKAL80ETC concert"),
SULFURAS("Sulfuras, Hand of Ragnaros");
SULFURAS("Sulfuras, Hand of Ragnaros"),
CONJURED("Conjured Mana Cake"),
;
private final String goodsName;

View File

@ -7,6 +7,7 @@ import java.util.function.Supplier;
import com.gildedrose.AgedBrie;
import com.gildedrose.BackStagePasses;
import com.gildedrose.Conjured;
import com.gildedrose.Goods;
import com.gildedrose.GoodsType;
import com.gildedrose.Generic;
@ -20,6 +21,7 @@ public class GoodsFactory {
goods.put(GoodsType.AGED_BRIE.getGoodsName(), AgedBrie::new);
goods.put(GoodsType.BACK_STAGE_PASSES.getGoodsName(), BackStagePasses::new);
goods.put(GoodsType.SULFURAS.getGoodsName(), Sulfuras::new);
goods.put(GoodsType.CONJURED.getGoodsName(), Conjured::new);
GOODS_SUPPLIER = Collections.unmodifiableMap(goods);
}

View File

@ -0,0 +1,42 @@
package com.gildedrose;
import static org.junit.jupiter.api.Assertions.*;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class ConjuredTest {
private static Stream<Arguments> getTestItems() {
return Stream.of(Arguments.of(new Item(GoodsType.CONJURED.getGoodsName(), 2, 1), 0),
Arguments.of(new Item(GoodsType.CONJURED.getGoodsName(), 10, 5), 3)
);
}
private static Stream<Arguments> getExpiredTestItems() {
return Stream.of(Arguments.of(new Item(GoodsType.CONJURED.getGoodsName(), -1, 1), 0),
Arguments.of(new Item(GoodsType.CONJURED.getGoodsName(), -1, 5), 3)
);
}
@ParameterizedTest(name = "Test updateQuality - {index}")
@MethodSource("getTestItems")
void testUpdateQuality(Item item, int expectedQuality) {
Conjured conjured = new Conjured();
conjured.updateQuality(item);
assertEquals(expectedQuality, item.quality);
}
@ParameterizedTest(name = "Test updateQualityForExpiredItems - {index}")
@MethodSource("getExpiredTestItems")
void testUpdateQualityForExpiredItem(Item item, int expectedQuality) {
Conjured conjured = new Conjured();
conjured.updateQualityForExpiredItem(item);
assertEquals(expectedQuality, item.quality);
}
}

View File

@ -13,7 +13,8 @@ class GildedRoseTest {
private static Stream<Arguments> getTestItemsForSellInDayCheck() {
return Stream.of(
Arguments.of(new Item[] { new Item(GoodsType.AGED_BRIE.getGoodsName(), 1, 1) }, 0),
Arguments.of(new Item[] { new Item(GoodsType.SULFURAS.getGoodsName(), 1, 1) }, 1)
Arguments.of(new Item[] { new Item(GoodsType.SULFURAS.getGoodsName(), 1, 1) }, 1),
Arguments.of(new Item[] { new Item(GoodsType.CONJURED.getGoodsName(), 1, 1) }, 0)
);
}
@ -48,7 +49,12 @@ class GildedRoseTest {
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)
Arguments.of(new Item[] { new Item(GoodsType.SULFURAS.getGoodsName(), 1, 10) }, 10),
Arguments.of(new Item[] { new Item(GoodsType.CONJURED.getGoodsName(), -1, 10) }, 6),
Arguments.of(new Item[] { new Item(GoodsType.CONJURED.getGoodsName(), 1, 10) }, 8),
Arguments.of(new Item[] { new Item(GoodsType.CONJURED.getGoodsName(), -1, 2) }, 0),
Arguments.of(new Item[] { new Item(GoodsType.CONJURED.getGoodsName(), 1, 2) }, 0)
);
}