🔨 remove QualityValidator

add it as a method to interface
This commit is contained in:
Velizar Todorov 2021-11-30 18:00:09 +01:00
parent 83c26fa828
commit 32cbb6f2e0
10 changed files with 77 additions and 50 deletions

View File

@ -17,11 +17,11 @@ public class ItemFactory {
} }
public static ItemType getItemType(Item item) { public static ItemType getItemType(Item item) {
QualityValidator.validateQuality(item);
ItemType itemType = getItems(item).get(item.name); ItemType itemType = getItems(item).get(item.name);
if (itemType == null) { if (itemType == null) {
itemType = new NormalItem(item); itemType = new NormalItem(item);
} }
itemType.validateQuality();
return itemType; return itemType;
} }

View File

@ -1,7 +1,24 @@
package com.gildedrose.item_helpers; package com.gildedrose.item_helpers;
public interface ItemType { import com.gildedrose.main.Item;
void updateQuality();
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;
}
} }

View File

@ -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);
}
}

View File

@ -3,6 +3,8 @@ package com.gildedrose.items;
import com.gildedrose.item_helpers.ItemType; import com.gildedrose.item_helpers.ItemType;
import com.gildedrose.main.Item; 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; import static java.lang.Math.min;
public class AgedBrieItem implements ItemType { 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() { public void incrementQuality() {
item.quality = min(item.quality + 1, 50); item.quality = min(item.quality + 1, 50);
} }

View File

@ -3,6 +3,8 @@ package com.gildedrose.items;
import com.gildedrose.item_helpers.ItemType; import com.gildedrose.item_helpers.ItemType;
import com.gildedrose.main.Item; 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; import static java.lang.Math.min;
public class BackstagePassItem implements ItemType { 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() { public void decrementSellInDate() {
item.sellIn--; item.sellIn--;
} }

View File

@ -3,6 +3,8 @@ package com.gildedrose.items;
import com.gildedrose.item_helpers.ItemType; import com.gildedrose.item_helpers.ItemType;
import com.gildedrose.main.Item; 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; import static java.lang.Math.max;
public class ConjuredItem implements ItemType { 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() { public void decrementSellInDate() {
item.sellIn--; item.sellIn--;
} }

View File

@ -14,11 +14,19 @@ public class LegendaryItem implements ItemType {
this.item = item; this.item = item;
} }
@Override @Override
public void updateQuality() { public void updateQuality() {
decrementSellInDate(); decrementSellInDate();
} }
@Override
public void validateQuality() {
if (isLegendaryWrongQuality(item)) {
throw new IllegalArgumentException(NOT_LEGENDARY_ITEM_ERROR_MESSAGE + item.quality);
}
}
@Override @Override
public String getName() { public String getName() {
return LEGENDARY; return LEGENDARY;
@ -28,12 +36,8 @@ public class LegendaryItem implements ItemType {
item.sellIn--; item.sellIn--;
} }
public static boolean isLegendary(Item item) { public static boolean isLegendaryWrongQuality(Item item) {
return item.name.equals(LEGENDARY); return item.name.equals(LEGENDARY) && item.quality != LEGENDARY_ITEM_QUALITY;
}
public static boolean isNotLegendary(Item item) {
return !item.name.equals(LEGENDARY);
} }
} }

View File

@ -3,6 +3,8 @@ package com.gildedrose.items;
import com.gildedrose.item_helpers.ItemType; import com.gildedrose.item_helpers.ItemType;
import com.gildedrose.main.Item; 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; import static java.lang.Math.max;
public class NormalItem implements ItemType { 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() { public void decrementSellInDate() {
item.sellIn--; item.sellIn--;
} }

View File

@ -3,8 +3,8 @@ package com.gildedrose.helper;
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.QualityValidator.OUT_OF_BOUND_QUALITY_MESSAGE; import static com.gildedrose.item_helpers.ItemType.OUT_OF_BOUND_QUALITY_MESSAGE;
import static com.gildedrose.item_helpers.QualityValidator.QUALITY_ERROR_MESSAGE; import static com.gildedrose.item_helpers.ItemType.QUALITY_ERROR_MESSAGE;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class TestHelper { public class TestHelper {

View File

@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;
import static com.gildedrose.helper.TestHelper.testItem; 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;
import static com.gildedrose.items.LegendaryItem.LEGENDARY_ITEM_QUALITY; import static com.gildedrose.items.LegendaryItem.LEGENDARY_ITEM_QUALITY;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;