From 3b1f1388cfcabe2e1d7b5264687d0e9f98905da3 Mon Sep 17 00:00:00 2001 From: Velizar Todorov Date: Fri, 26 Nov 2021 14:28:05 +0100 Subject: [PATCH] :hammer: move validation logic negative quality check to factory and legendary validation to validation class --- .../gildedrose/item_helpers/ItemFactory.java | 16 +++++++++++++--- .../gildedrose/item_helpers/ItemHandler.java | 17 ----------------- .../com/gildedrose/items/LegendaryItem.java | 11 +++++++++-- .../java/com/gildedrose/items/TestHelper.java | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java b/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java index a5bd0438..3b71d01a 100644 --- a/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java +++ b/Java/src/main/java/com/gildedrose/item_helpers/ItemFactory.java @@ -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; + } } diff --git a/Java/src/main/java/com/gildedrose/item_helpers/ItemHandler.java b/Java/src/main/java/com/gildedrose/item_helpers/ItemHandler.java index 13cc17f2..8ecd8eb6 100644 --- a/Java/src/main/java/com/gildedrose/item_helpers/ItemHandler.java +++ b/Java/src/main/java/com/gildedrose/item_helpers/ItemHandler.java @@ -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; } diff --git a/Java/src/main/java/com/gildedrose/items/LegendaryItem.java b/Java/src/main/java/com/gildedrose/items/LegendaryItem.java index 14fdb63d..78ac5e2f 100644 --- a/Java/src/main/java/com/gildedrose/items/LegendaryItem.java +++ b/Java/src/main/java/com/gildedrose/items/LegendaryItem.java @@ -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; } diff --git a/Java/src/test/java/com/gildedrose/items/TestHelper.java b/Java/src/test/java/com/gildedrose/items/TestHelper.java index ee79e7ad..3e24e9f7 100644 --- a/Java/src/test/java/com/gildedrose/items/TestHelper.java +++ b/Java/src/test/java/com/gildedrose/items/TestHelper.java @@ -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 {