From 32cbb6f2e03dc2713da8a754451674d6f8f985e1 Mon Sep 17 00:00:00 2001 From: Velizar Todorov Date: Tue, 30 Nov 2021 18:00:09 +0100 Subject: [PATCH] :hammer: remove QualityValidator add it as a method to interface --- .../gildedrose/item_helpers/ItemFactory.java | 2 +- .../com/gildedrose/item_helpers/ItemType.java | 23 ++++++++++-- .../item_helpers/QualityValidator.java | 37 ------------------- .../com/gildedrose/items/AgedBrieItem.java | 11 ++++++ .../gildedrose/items/BackstagePassItem.java | 10 +++++ .../com/gildedrose/items/ConjuredItem.java | 11 ++++++ .../com/gildedrose/items/LegendaryItem.java | 16 +++++--- .../java/com/gildedrose/items/NormalItem.java | 11 ++++++ .../com/gildedrose/helper/TestHelper.java | 4 +- .../gildedrose/items/LegendaryItemTest.java | 2 +- 10 files changed, 77 insertions(+), 50 deletions(-) delete mode 100644 src/main/java/com/gildedrose/item_helpers/QualityValidator.java diff --git a/src/main/java/com/gildedrose/item_helpers/ItemFactory.java b/src/main/java/com/gildedrose/item_helpers/ItemFactory.java index ea1776a8..df26a13b 100644 --- a/src/main/java/com/gildedrose/item_helpers/ItemFactory.java +++ b/src/main/java/com/gildedrose/item_helpers/ItemFactory.java @@ -17,11 +17,11 @@ public class ItemFactory { } public static ItemType getItemType(Item item) { - QualityValidator.validateQuality(item); ItemType itemType = getItems(item).get(item.name); if (itemType == null) { itemType = new NormalItem(item); } + itemType.validateQuality(); return itemType; } diff --git a/src/main/java/com/gildedrose/item_helpers/ItemType.java b/src/main/java/com/gildedrose/item_helpers/ItemType.java index 1b742452..9ab83a1f 100644 --- a/src/main/java/com/gildedrose/item_helpers/ItemType.java +++ b/src/main/java/com/gildedrose/item_helpers/ItemType.java @@ -1,7 +1,24 @@ package com.gildedrose.item_helpers; -public interface ItemType { - void updateQuality(); +import com.gildedrose.main.Item; - String getName(); +public interface ItemType { + + void updateQuality(); + + void validateQuality(); + + String getName(); + + String QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: "; + String OUT_OF_BOUND_QUALITY_MESSAGE = "Quality cannot be above 50! Current value: "; + String NOT_LEGENDARY_ITEM_ERROR_MESSAGE = "Item is legendary, quality must be always 80! Current value: "; + + static boolean qualityIsNegative(Item item) { + return item.quality < 0; + } + + static boolean qualityIsAbove50(Item item) { + return item.quality > 50; + } } diff --git a/src/main/java/com/gildedrose/item_helpers/QualityValidator.java b/src/main/java/com/gildedrose/item_helpers/QualityValidator.java deleted file mode 100644 index fca4dafe..00000000 --- a/src/main/java/com/gildedrose/item_helpers/QualityValidator.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.gildedrose.item_helpers; - -import com.gildedrose.main.Item; - -import static com.gildedrose.items.LegendaryItem.*; - -public class QualityValidator { - - private QualityValidator() { - } - - public static final String QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: "; - public static final String OUT_OF_BOUND_QUALITY_MESSAGE = "Quality cannot be above 50! Current value: "; - public static final String NOT_LEGENDARY_ITEM_ERROR_MESSAGE = "Item is legendary, quality must be always 80! Current value: "; - - public static void validateQuality(Item item) { - if (isLegendaryWrongQuality(item)) { - throw new IllegalArgumentException(NOT_LEGENDARY_ITEM_ERROR_MESSAGE + item.quality); - } else if (qualityIsNegative(item)) { - throw new IllegalArgumentException(QUALITY_ERROR_MESSAGE + item.quality); - } else if (qualityIsAbove50(item)) { - throw new IllegalArgumentException(OUT_OF_BOUND_QUALITY_MESSAGE + item.quality); - } - } - - public static boolean isLegendaryWrongQuality(Item item) { - return isLegendary(item) && item.quality != LEGENDARY_ITEM_QUALITY; - } - - private static boolean qualityIsNegative(Item item) { - return item.quality < 0; - } - - private static boolean qualityIsAbove50(Item item) { - return item.quality > 50 && isNotLegendary(item); - } -} diff --git a/src/main/java/com/gildedrose/items/AgedBrieItem.java b/src/main/java/com/gildedrose/items/AgedBrieItem.java index 486dd5f0..cbfa260d 100644 --- a/src/main/java/com/gildedrose/items/AgedBrieItem.java +++ b/src/main/java/com/gildedrose/items/AgedBrieItem.java @@ -3,6 +3,8 @@ package com.gildedrose.items; import com.gildedrose.item_helpers.ItemType; import com.gildedrose.main.Item; +import static com.gildedrose.item_helpers.ItemType.qualityIsAbove50; +import static com.gildedrose.item_helpers.ItemType.qualityIsNegative; import static java.lang.Math.min; public class AgedBrieItem implements ItemType { @@ -25,6 +27,15 @@ public class AgedBrieItem implements ItemType { } } + @Override + public void validateQuality() { + if (qualityIsNegative(item)) { + throw new IllegalArgumentException(QUALITY_ERROR_MESSAGE + item.quality); + } else if (qualityIsAbove50(item)) { + throw new IllegalArgumentException(OUT_OF_BOUND_QUALITY_MESSAGE + item.quality); + } + } + public void incrementQuality() { item.quality = min(item.quality + 1, 50); } diff --git a/src/main/java/com/gildedrose/items/BackstagePassItem.java b/src/main/java/com/gildedrose/items/BackstagePassItem.java index 3c2ffc61..37f87200 100644 --- a/src/main/java/com/gildedrose/items/BackstagePassItem.java +++ b/src/main/java/com/gildedrose/items/BackstagePassItem.java @@ -3,6 +3,8 @@ package com.gildedrose.items; import com.gildedrose.item_helpers.ItemType; import com.gildedrose.main.Item; +import static com.gildedrose.item_helpers.ItemType.qualityIsAbove50; +import static com.gildedrose.item_helpers.ItemType.qualityIsNegative; import static java.lang.Math.min; public class BackstagePassItem implements ItemType { @@ -29,6 +31,14 @@ public class BackstagePassItem implements ItemType { } } + @Override + public void validateQuality() { + if (qualityIsNegative(item)) { + throw new IllegalArgumentException(QUALITY_ERROR_MESSAGE + item.quality); + } else if (qualityIsAbove50(item)) + throw new IllegalArgumentException(OUT_OF_BOUND_QUALITY_MESSAGE + item.quality); + } + public void decrementSellInDate() { item.sellIn--; } diff --git a/src/main/java/com/gildedrose/items/ConjuredItem.java b/src/main/java/com/gildedrose/items/ConjuredItem.java index 66a0f415..64173b7d 100644 --- a/src/main/java/com/gildedrose/items/ConjuredItem.java +++ b/src/main/java/com/gildedrose/items/ConjuredItem.java @@ -3,6 +3,8 @@ package com.gildedrose.items; import com.gildedrose.item_helpers.ItemType; import com.gildedrose.main.Item; +import static com.gildedrose.item_helpers.ItemType.qualityIsAbove50; +import static com.gildedrose.item_helpers.ItemType.qualityIsNegative; import static java.lang.Math.max; public class ConjuredItem implements ItemType { @@ -25,6 +27,15 @@ public class ConjuredItem implements ItemType { } } + @Override + public void validateQuality() { + if (qualityIsNegative(item)) { + throw new IllegalArgumentException(QUALITY_ERROR_MESSAGE + item.quality); + } else if (qualityIsAbove50(item)) { + throw new IllegalArgumentException(OUT_OF_BOUND_QUALITY_MESSAGE + item.quality); + } + } + public void decrementSellInDate() { item.sellIn--; } diff --git a/src/main/java/com/gildedrose/items/LegendaryItem.java b/src/main/java/com/gildedrose/items/LegendaryItem.java index 6daf0967..9ccfd3de 100644 --- a/src/main/java/com/gildedrose/items/LegendaryItem.java +++ b/src/main/java/com/gildedrose/items/LegendaryItem.java @@ -14,11 +14,19 @@ public class LegendaryItem implements ItemType { this.item = item; } + @Override public void updateQuality() { decrementSellInDate(); } + @Override + public void validateQuality() { + if (isLegendaryWrongQuality(item)) { + throw new IllegalArgumentException(NOT_LEGENDARY_ITEM_ERROR_MESSAGE + item.quality); + } + } + @Override public String getName() { return LEGENDARY; @@ -28,12 +36,8 @@ public class LegendaryItem implements ItemType { item.sellIn--; } - public static boolean isLegendary(Item item) { - return item.name.equals(LEGENDARY); - } - - public static boolean isNotLegendary(Item item) { - return !item.name.equals(LEGENDARY); + public static boolean isLegendaryWrongQuality(Item item) { + return item.name.equals(LEGENDARY) && item.quality != LEGENDARY_ITEM_QUALITY; } } diff --git a/src/main/java/com/gildedrose/items/NormalItem.java b/src/main/java/com/gildedrose/items/NormalItem.java index f877d085..71874c16 100644 --- a/src/main/java/com/gildedrose/items/NormalItem.java +++ b/src/main/java/com/gildedrose/items/NormalItem.java @@ -3,6 +3,8 @@ package com.gildedrose.items; import com.gildedrose.item_helpers.ItemType; import com.gildedrose.main.Item; +import static com.gildedrose.item_helpers.ItemType.qualityIsAbove50; +import static com.gildedrose.item_helpers.ItemType.qualityIsNegative; import static java.lang.Math.max; public class NormalItem implements ItemType { @@ -24,6 +26,15 @@ public class NormalItem implements ItemType { } } + @Override + public void validateQuality() { + if (qualityIsNegative(item)) { + throw new IllegalArgumentException(QUALITY_ERROR_MESSAGE + item.quality); + } else if (qualityIsAbove50(item)) { + throw new IllegalArgumentException(OUT_OF_BOUND_QUALITY_MESSAGE + item.quality); + } + } + public void decrementSellInDate() { item.sellIn--; } diff --git a/src/test/java/com/gildedrose/helper/TestHelper.java b/src/test/java/com/gildedrose/helper/TestHelper.java index 047a6557..fcea4d71 100644 --- a/src/test/java/com/gildedrose/helper/TestHelper.java +++ b/src/test/java/com/gildedrose/helper/TestHelper.java @@ -3,8 +3,8 @@ package com.gildedrose.helper; import com.gildedrose.main.GildedRose; import com.gildedrose.main.Item; -import static com.gildedrose.item_helpers.QualityValidator.OUT_OF_BOUND_QUALITY_MESSAGE; -import static com.gildedrose.item_helpers.QualityValidator.QUALITY_ERROR_MESSAGE; +import static com.gildedrose.item_helpers.ItemType.OUT_OF_BOUND_QUALITY_MESSAGE; +import static com.gildedrose.item_helpers.ItemType.QUALITY_ERROR_MESSAGE; import static org.junit.jupiter.api.Assertions.*; public class TestHelper { diff --git a/src/test/java/com/gildedrose/items/LegendaryItemTest.java b/src/test/java/com/gildedrose/items/LegendaryItemTest.java index 4168cfb2..c2e70351 100644 --- a/src/test/java/com/gildedrose/items/LegendaryItemTest.java +++ b/src/test/java/com/gildedrose/items/LegendaryItemTest.java @@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import static com.gildedrose.helper.TestHelper.testItem; -import static com.gildedrose.item_helpers.QualityValidator.NOT_LEGENDARY_ITEM_ERROR_MESSAGE; +import static com.gildedrose.item_helpers.ItemType.NOT_LEGENDARY_ITEM_ERROR_MESSAGE; import static com.gildedrose.items.LegendaryItem.LEGENDARY; import static com.gildedrose.items.LegendaryItem.LEGENDARY_ITEM_QUALITY; import static org.junit.jupiter.api.Assertions.assertThrows;