mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
🔨 move validation logic
negative quality check to factory and legendary validation to validation class
This commit is contained in:
parent
8807cd17ce
commit
3b1f1388cf
@ -1,13 +1,12 @@
|
|||||||
package com.gildedrose.item_helpers;
|
package com.gildedrose.item_helpers;
|
||||||
|
|
||||||
import com.gildedrose.main.Item;
|
|
||||||
import com.gildedrose.items.AgedBrieItem;
|
import com.gildedrose.items.AgedBrieItem;
|
||||||
import com.gildedrose.items.BackstagePassItem;
|
import com.gildedrose.items.BackstagePassItem;
|
||||||
import com.gildedrose.items.ConjuredItem;
|
import com.gildedrose.items.ConjuredItem;
|
||||||
import com.gildedrose.items.LegendaryItem;
|
import com.gildedrose.items.LegendaryItem;
|
||||||
import com.gildedrose.items.NormalItem;
|
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;
|
import static com.gildedrose.item_helpers.ItemName.getItemName;
|
||||||
|
|
||||||
public class ItemFactory {
|
public class ItemFactory {
|
||||||
@ -16,7 +15,7 @@ public class ItemFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ItemType getItem(Item item) {
|
public static ItemType getItem(Item item) {
|
||||||
validate(item);
|
validateQuality(item);
|
||||||
ItemName itemName = getItemName(item.name);
|
ItemName itemName = getItemName(item.name);
|
||||||
switch (itemName) {
|
switch (itemName) {
|
||||||
case AGED_BRIE:
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,33 +2,16 @@ package com.gildedrose.item_helpers;
|
|||||||
|
|
||||||
import com.gildedrose.main.Item;
|
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;
|
import static java.lang.Math.max;
|
||||||
|
|
||||||
public class ItemHandler {
|
public class ItemHandler {
|
||||||
|
|
||||||
public static final String QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: ";
|
|
||||||
|
|
||||||
private final Item item;
|
private final Item item;
|
||||||
|
|
||||||
public ItemHandler(Item item) {
|
public ItemHandler(Item item) {
|
||||||
this.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() {
|
public boolean qualityIsHigherThanZero() {
|
||||||
return item.quality > 0;
|
return item.quality > 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
package com.gildedrose.items;
|
package com.gildedrose.items;
|
||||||
|
|
||||||
import com.gildedrose.main.Item;
|
|
||||||
import com.gildedrose.item_helpers.ItemHandler;
|
import com.gildedrose.item_helpers.ItemHandler;
|
||||||
import com.gildedrose.item_helpers.ItemType;
|
import com.gildedrose.item_helpers.ItemType;
|
||||||
|
import com.gildedrose.main.Item;
|
||||||
|
|
||||||
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
||||||
|
|
||||||
@ -13,6 +13,7 @@ public class LegendaryItem implements ItemType {
|
|||||||
private final ItemHandler item;
|
private final ItemHandler item;
|
||||||
|
|
||||||
public LegendaryItem(Item item) {
|
public LegendaryItem(Item item) {
|
||||||
|
validate(item);
|
||||||
this.item = new ItemHandler(item);
|
this.item = new ItemHandler(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,7 +22,13 @@ public class LegendaryItem implements ItemType {
|
|||||||
item.decrementSellInDate();
|
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;
|
return item.name.equals(LEGENDARY.toString()) && item.quality != LEGENDARY_ITEM_QUALITY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package com.gildedrose.items;
|
|||||||
import com.gildedrose.main.GildedRose;
|
import com.gildedrose.main.GildedRose;
|
||||||
import com.gildedrose.main.Item;
|
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.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class TestHelper {
|
public class TestHelper {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user