Improved class structure by adding skeleton for specific implementation.

This commit is contained in:
lekshmysasidhar 2023-03-31 11:12:18 +02:00
parent f576dd8f49
commit 95e7036824
9 changed files with 132 additions and 14 deletions

View File

@ -0,0 +1,5 @@
package com.gildedrose;
public class AgedBrie extends Goods {
}

View File

@ -0,0 +1,5 @@
package com.gildedrose;
public class BackStagePasses extends Goods {
}

View File

@ -0,0 +1,5 @@
package com.gildedrose;
public class Generic extends Goods {
}

View File

@ -0,0 +1,5 @@
package com.gildedrose;
public abstract class Goods {
}

View File

@ -0,0 +1,19 @@
package com.gildedrose;
public enum GoodsType {
AGED_BRIE("Aged Brie"),
BACK_STAGE_PASSES("Backstage passes to a TAFKAL80ETC concert"),
SULFURAS("Sulfuras, Hand of Ragnaros");
private final String goodsName;
private GoodsType(String goodsName) {
this.goodsName = goodsName;
}
public String getGoodsName() {
return goodsName;
}
}

View File

@ -0,0 +1,5 @@
package com.gildedrose;
public class Sulfuras extends Goods {
}

View File

@ -0,0 +1,39 @@
package com.gildedrose.factory;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import com.gildedrose.AgedBrie;
import com.gildedrose.BackStagePasses;
import com.gildedrose.Goods;
import com.gildedrose.GoodsType;
import com.gildedrose.Generic;
import com.gildedrose.Sulfuras;
public class GoodsFactory {
private static final Map<String, Supplier<Goods>> GOODS_SUPPLIER;
static {
final Map<String, Supplier<Goods>> goods = new HashMap<>();
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_SUPPLIER = Collections.unmodifiableMap(goods);
}
public static Goods getGoods(String goodsType) {
Supplier<Goods> goods = GOODS_SUPPLIER.get(goodsType);
if (goods == null) {
return new Generic();
}
return goods.get();
}
private GoodsFactory() {
}
}

View File

@ -1,6 +1,5 @@
package com.gildedrose;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@ -13,8 +12,8 @@ class GildedRoseTest {
private static Stream<Arguments> getTestItemsForSellInDayCheck() {
return Stream.of(
Arguments.of(new Item[] { new Item("Aged Brie", 1, 1) }, 0),
Arguments.of(new Item[] { new Item("Sulfuras, Hand of Ragnaros", 1, 1) }, 1)
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)
);
}
@ -28,20 +27,21 @@ class GildedRoseTest {
}
private static Stream<Arguments> getTestItemsForQualityCheck() {
return Stream.of(Arguments.of(new Item[] { new Item("Aged Brie", 1, 1) }, 2),
Arguments.of(new Item[] { new Item("Aged Brie", 1, 50) }, 50),
return Stream.of(
Arguments.of(new Item[] { new Item(GoodsType.AGED_BRIE.getGoodsName(), 1, 1) }, 2),
Arguments.of(new Item[] { new Item(GoodsType.AGED_BRIE.getGoodsName(), 1, 50) }, 50),
Arguments.of(new Item[] { new Item("Aged Brie", -1, 1) }, 3),
Arguments.of(new Item[] { new Item("Aged Brie", -1, 50) }, 50),
Arguments.of(new Item[] { new Item(GoodsType.AGED_BRIE.getGoodsName(), -1, 1) }, 3),
Arguments.of(new Item[] { new Item(GoodsType.AGED_BRIE.getGoodsName(), -1, 50) }, 50),
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 5, 10) }, 13),
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 5, 50) }, 50),
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 10, 10) }, 12),
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 10, 50) }, 50),
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 12, 10) }, 11),
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 12, 50) }, 50),
Arguments.of(new Item[] { new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 5, 10) }, 13),
Arguments.of(new Item[] { new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 5, 50) }, 50),
Arguments.of(new Item[] { new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 10, 10) }, 12),
Arguments.of(new Item[] { new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 10, 50) }, 50),
Arguments.of(new Item[] { new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 12, 10) }, 11),
Arguments.of(new Item[] { new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), 12, 50) }, 50),
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", -1, 10) }, 0),
Arguments.of(new Item[] { new Item(GoodsType.BACK_STAGE_PASSES.getGoodsName(), -1, 10) }, 0),
Arguments.of(new Item[] { new Item("Coffee Day", 1, 1) }, 0),
Arguments.of(new Item[] { new Item("Coffee Day", -1, 1) }, 0),

View File

@ -0,0 +1,35 @@
package com.gildedrose.factory;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import com.gildedrose.AgedBrie;
import com.gildedrose.BackStagePasses;
import com.gildedrose.Generic;
import com.gildedrose.Goods;
import com.gildedrose.GoodsType;
import com.gildedrose.Sulfuras;
class GoodsFactoryTest {
@Test
void testGetGoods() {
// Test AgedBrie
Goods agedBrie = GoodsFactory.getGoods(GoodsType.AGED_BRIE.getGoodsName());
assertTrue(agedBrie instanceof AgedBrie);
// Test BackStagePasses
Goods backStagePasses = GoodsFactory.getGoods(GoodsType.BACK_STAGE_PASSES.getGoodsName());
assertTrue(backStagePasses instanceof BackStagePasses);
// Test Sulfuras
Goods sulfuras = GoodsFactory.getGoods(GoodsType.SULFURAS.getGoodsName());
assertTrue(sulfuras instanceof Sulfuras);
// Test Generic
Goods generic = GoodsFactory.getGoods("normal item");
assertTrue(generic instanceof Generic);
}
}