🔨 move validation logic

negative quality check to factory and legendary validation to validation
class
This commit is contained in:
Velizar Todorov 2021-11-26 14:28:05 +01:00
parent 8807cd17ce
commit 3b1f1388cf
4 changed files with 23 additions and 23 deletions

View File

@ -1,13 +1,12 @@
package com.gildedrose.item_helpers;
import com.gildedrose.main.Item;
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 com.gildedrose.main.Item;
import static com.gildedrose.item_helpers.ItemHandler.validate;
import static com.gildedrose.item_helpers.ItemName.getItemName;
public class ItemFactory {
@ -16,7 +15,7 @@ public class ItemFactory {
}
public static ItemType getItem(Item item) {
validate(item);
validateQuality(item);
ItemName itemName = getItemName(item.name);
switch (itemName) {
case AGED_BRIE:
@ -32,4 +31,15 @@ public class ItemFactory {
}
}
public static final String QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: ";
private static void validateQuality(Item item) {
if (qualityIsNegative(item)) {
throw new IllegalArgumentException(QUALITY_ERROR_MESSAGE + item.quality);
}
}
private static boolean qualityIsNegative(Item item) {
return item.quality < 0;
}
}

View File

@ -2,33 +2,16 @@ package com.gildedrose.item_helpers;
import com.gildedrose.main.Item;
import static com.gildedrose.items.LegendaryItem.NOT_LEGENDARY_ITEM_ERROR_MESSAGE;
import static com.gildedrose.items.LegendaryItem.isNotLegendary;
import static java.lang.Math.max;
public class ItemHandler {
public static final String QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: ";
private final Item item;
public ItemHandler(Item item) {
this.item = item;
}
static void validate(Item item) {
if (qualityIsNegative(item)) {
throw new IllegalArgumentException(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;
}
public boolean qualityIsHigherThanZero() {
return item.quality > 0;
}

View File

@ -1,8 +1,8 @@
package com.gildedrose.items;
import com.gildedrose.main.Item;
import com.gildedrose.item_helpers.ItemHandler;
import com.gildedrose.item_helpers.ItemType;
import com.gildedrose.main.Item;
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
@ -13,6 +13,7 @@ public class LegendaryItem implements ItemType {
private final ItemHandler item;
public LegendaryItem(Item item) {
validate(item);
this.item = new ItemHandler(item);
}
@ -21,7 +22,13 @@ public class LegendaryItem implements ItemType {
item.decrementSellInDate();
}
public static boolean isNotLegendary(Item item) {
private void validate(Item item) {
if (notLegendary(item)) {
throw new IllegalArgumentException(NOT_LEGENDARY_ITEM_ERROR_MESSAGE + item.quality);
}
}
public boolean notLegendary(Item item) {
return item.name.equals(LEGENDARY.toString()) && item.quality != LEGENDARY_ITEM_QUALITY;
}

View File

@ -3,7 +3,7 @@ package com.gildedrose.items;
import com.gildedrose.main.GildedRose;
import com.gildedrose.main.Item;
import static com.gildedrose.item_helpers.ItemHandler.QUALITY_ERROR_MESSAGE;
import static com.gildedrose.item_helpers.ItemFactory.QUALITY_ERROR_MESSAGE;
import static org.junit.jupiter.api.Assertions.*;
public class TestHelper {