Configurations for individual item types

This commit is contained in:
Vijay G 2023-06-12 21:53:52 +05:30
parent fb969858cb
commit d1f7a4c85e
6 changed files with 145 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package com.gildedrose.config;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import com.gildedrose.enums.ProductType;
import com.gildedrose.generic.ItemType;
import com.gildedrose.types.AgedBrie;
import com.gildedrose.types.Backstage;
import com.gildedrose.types.Others;
import com.gildedrose.types.Sulfuras;
/***
* ProductFactory defines the instance of each items based on description of the item
* @author VIJAY G
*
*/
public class ProductFactory {
@SuppressWarnings("rawtypes")
private static Map<ProductType, ItemType> productMap = new HashMap<>();
static {
productMap.put(ProductType.AGED_BRIE, new AgedBrie());
productMap.put(ProductType.SULFURAS, new Sulfuras());
productMap.put(ProductType.BACKSTAGE_PASSES, new Backstage());
}
@SuppressWarnings("rawtypes")
public static ItemType get(ProductType productType) {
ItemType itemType = productMap.get(productType);
if(Objects.isNull(itemType)) {
return new Others();
}
return itemType;
}
}

View File

@ -0,0 +1,27 @@
package com.gildedrose.enums;
/***
* ProductType describes the type of Items
* @author VIJAY G
*
*/
public enum ProductType {
AGED_BRIE("Aged Brie"),
BACKSTAGE_PASSES("Backstage passes to a TAFKAL80ETC concert"),
SULFURAS("Sulfuras, Hand of Ragnaros");
private final String productName;
private ProductType(String productName) {
this.productName = productName;
}
public static ProductType getEnumByString(String code){
for(ProductType e : ProductType.values()){
if(e.productName.equals(code)) return e.valueOf(e.name());
}
return null;
}
}

View File

@ -0,0 +1,23 @@
package com.gildedrose.types;
import com.gildedrose.Item;
import com.gildedrose.enums.CountType;
import com.gildedrose.generic.ItemType;
/***
* "Aged Brie" actually increases in Quality the older it gets
* the Quality of an item is never more than 50
* @author VIJAY G
*
*/
public class AgedBrie extends ItemType<AgedBrie> {
public AgedBrie() {
super(false, CountType.MINUS, CountType.PLUS);
}
public int getIncrementor(Item item) {
return (item.sellIn <= 0) ? 2 : INCREMENTOR;
}
}

View File

@ -0,0 +1,22 @@
package com.gildedrose.types;
import com.gildedrose.Item;
import com.gildedrose.enums.CountType;
import com.gildedrose.generic.ItemType;
/***
* "Backstage passes", like aged brie, increases in Quality as its SellIn value approaches;
* @author VIJAY G
*
*/
public class Backstage extends ItemType<Backstage> {
public Backstage() {
super(true, CountType.MINUS, CountType.PLUS);
}
public boolean isNeutralized(Item item) {
return (item.sellIn < 0) ? true : false;
}
}

View File

@ -0,0 +1,18 @@
package com.gildedrose.types;
import com.gildedrose.enums.CountType;
import com.gildedrose.generic.ItemType;
/***
* Once the sell by date has passed, Quality degrades twice as fast
* The Quality of an item is never negative
* @author VIJAY G
*
*/
public class Others extends ItemType<Others> {
public Others() {
super(false, CountType.MINUS, CountType.MINUS);
}
}

View File

@ -0,0 +1,17 @@
package com.gildedrose.types;
import com.gildedrose.enums.CountType;
import com.gildedrose.generic.ItemType;
/***
* "Sulfuras", being a legendary item, never has to be sold or decreases in Quality
* @author VIJAY G
*
*/
public class Sulfuras extends ItemType<Sulfuras> {
public Sulfuras() {
super(true, CountType.NONE, CountType.NONE);
}
}