mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
🔨 add validation for items
This commit is contained in:
parent
9298afef4b
commit
598301355e
@ -1,12 +1,13 @@
|
||||
package com.gildedrose.item_helpers;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
import com.gildedrose.items.AgedBrie;
|
||||
import com.gildedrose.items.BackstagePass;
|
||||
import com.gildedrose.items.Conjured;
|
||||
import com.gildedrose.items.Legendary;
|
||||
import com.gildedrose.items.Normal;
|
||||
import com.gildedrose.items.AgedBrieItem;
|
||||
import com.gildedrose.items.BackstagePassItem;
|
||||
import com.gildedrose.items.ConjuredItem;
|
||||
import com.gildedrose.items.LegendaryItem;
|
||||
import com.gildedrose.items.NormalItem;
|
||||
|
||||
import static com.gildedrose.item_helpers.ItemHandler.validate;
|
||||
import static com.gildedrose.item_helpers.ItemName.getItemName;
|
||||
|
||||
public class ItemFactory {
|
||||
@ -15,18 +16,20 @@ public class ItemFactory {
|
||||
}
|
||||
|
||||
public static ItemType getItem(Item item) {
|
||||
validate(item);
|
||||
ItemName itemName = getItemName(item.name);
|
||||
switch (itemName) {
|
||||
case AGED_BRIE:
|
||||
return new AgedBrie(item);
|
||||
return new AgedBrieItem(item);
|
||||
case LEGENDARY:
|
||||
return new Legendary(item);
|
||||
return new LegendaryItem(item);
|
||||
case BACKSTAGE_PASS:
|
||||
return new BackstagePass(item);
|
||||
return new BackstagePassItem(item);
|
||||
case CONJURED:
|
||||
return new Conjured(item);
|
||||
return new ConjuredItem(item);
|
||||
default:
|
||||
return new Normal(item);
|
||||
return new NormalItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,11 +2,14 @@ package com.gildedrose.item_helpers;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
|
||||
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
||||
import static java.lang.Math.max;
|
||||
|
||||
public class ItemHandler {
|
||||
|
||||
private static final int LEGENDARY_ITEM_QUALITY = 80;
|
||||
protected static final String NEGATIVE_QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: ";
|
||||
protected static final String NOT_LEGENDARY_ITEM_ERROR_MESSAGE = "Item is legendary, quality must be always 80! Current value: ";
|
||||
|
||||
private final Item item;
|
||||
|
||||
@ -14,6 +17,23 @@ public class ItemHandler {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
static void validate(Item item) {
|
||||
if (qualityIsNegative(item)) {
|
||||
throw new IllegalArgumentException(NEGATIVE_QUALITY_ERROR_MESSAGE + item.quality);
|
||||
}
|
||||
if (isNotLegendary(item)) {
|
||||
throw new IllegalArgumentException(NOT_LEGENDARY_ITEM_ERROR_MESSAGE + item.quality);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean qualityIsNegative(Item item) {
|
||||
return item.quality < 0;
|
||||
}
|
||||
|
||||
private static boolean isNotLegendary(Item item) {
|
||||
return item.name.equals(LEGENDARY.toString()) && item.quality != 80;
|
||||
}
|
||||
|
||||
public void decrementSellInDate() {
|
||||
item.sellIn--;
|
||||
}
|
||||
|
||||
@ -4,11 +4,11 @@ import com.gildedrose.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
public class AgedBrie implements ItemType {
|
||||
public class AgedBrieItem implements ItemType {
|
||||
|
||||
private final ItemHandler item;
|
||||
|
||||
public AgedBrie(Item item) {
|
||||
public AgedBrieItem(Item item) {
|
||||
this.item = new ItemHandler(item);
|
||||
}
|
||||
|
||||
@ -4,11 +4,11 @@ import com.gildedrose.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
public class BackstagePass implements ItemType {
|
||||
public class BackstagePassItem implements ItemType {
|
||||
|
||||
private final ItemHandler item;
|
||||
|
||||
public BackstagePass(Item item) {
|
||||
public BackstagePassItem(Item item) {
|
||||
this.item = new ItemHandler(item);
|
||||
}
|
||||
|
||||
@ -4,11 +4,11 @@ import com.gildedrose.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
public class Conjured implements ItemType {
|
||||
public class ConjuredItem implements ItemType {
|
||||
|
||||
private final ItemHandler item;
|
||||
|
||||
public Conjured(Item item) {
|
||||
public ConjuredItem(Item item) {
|
||||
this.item = new ItemHandler(item);
|
||||
}
|
||||
|
||||
@ -23,4 +23,5 @@ public class Conjured implements ItemType {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -4,11 +4,11 @@ import com.gildedrose.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
public class Legendary implements ItemType {
|
||||
public class LegendaryItem implements ItemType {
|
||||
|
||||
private final ItemHandler item;
|
||||
|
||||
public Legendary(Item item) {
|
||||
public LegendaryItem(Item item) {
|
||||
this.item = new ItemHandler(item);
|
||||
}
|
||||
|
||||
@ -4,11 +4,11 @@ import com.gildedrose.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
public class Normal implements ItemType {
|
||||
public class NormalItem implements ItemType {
|
||||
|
||||
private final ItemHandler item;
|
||||
|
||||
public Normal(Item item) {
|
||||
public NormalItem(Item item) {
|
||||
this.item = new ItemHandler(item);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user