mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-17 23:41:27 +00:00
🐛 check for quality above 50
This commit is contained in:
parent
ffb8a9209c
commit
40a260bb03
@ -15,7 +15,7 @@ public class ItemFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ItemType getItemType(Item item) {
|
public static ItemType getItemType(Item item) {
|
||||||
validateQuality(item);
|
QualityValidator.validateQuality(item);
|
||||||
ItemName itemName = getItemName(item.name);
|
ItemName itemName = getItemName(item.name);
|
||||||
switch (itemName) {
|
switch (itemName) {
|
||||||
case AGED_BRIE:
|
case AGED_BRIE:
|
||||||
@ -31,15 +31,4 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package com.gildedrose.item_helpers;
|
|||||||
import com.gildedrose.main.Item;
|
import com.gildedrose.main.Item;
|
||||||
|
|
||||||
import static java.lang.Math.max;
|
import static java.lang.Math.max;
|
||||||
|
import static java.lang.Math.min;
|
||||||
|
|
||||||
public class ItemHandler {
|
public class ItemHandler {
|
||||||
|
|
||||||
@ -37,15 +38,15 @@ public class ItemHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void incrementQuality() {
|
public void incrementQuality() {
|
||||||
item.quality = max(item.quality + 1, 0);
|
item.quality = min(item.quality + 1, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void incrementQualityBy2() {
|
public void incrementQualityBy2() {
|
||||||
item.quality = max(item.quality + 2, 0);
|
item.quality = min(item.quality + 2, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void incrementQualityBy3() {
|
public void incrementQualityBy3() {
|
||||||
item.quality = max(item.quality + 3, 0);
|
item.quality = min(item.quality + 3, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void decrementQuality() {
|
public void decrementQuality() {
|
||||||
|
|||||||
@ -0,0 +1,37 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,11 +9,10 @@ import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
|||||||
public class LegendaryItem implements ItemType {
|
public class LegendaryItem implements ItemType {
|
||||||
|
|
||||||
public static final int LEGENDARY_ITEM_QUALITY = 80;
|
public static final int LEGENDARY_ITEM_QUALITY = 80;
|
||||||
public static final String NOT_LEGENDARY_ITEM_ERROR_MESSAGE = "Item is legendary, quality must be always 80! Current value: ";
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,14 +21,12 @@ public class LegendaryItem implements ItemType {
|
|||||||
item.decrementSellInDate();
|
item.decrementSellInDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validate(Item item) {
|
public static boolean isLegendary(Item item) {
|
||||||
if (notLegendary(item)) {
|
return item.name.equals(LEGENDARY.toString());
|
||||||
throw new IllegalArgumentException(NOT_LEGENDARY_ITEM_ERROR_MESSAGE + item.quality);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean notLegendary(Item item) {
|
public static boolean isNotLegendary(Item item) {
|
||||||
return item.name.equals(LEGENDARY.toString()) && item.quality != LEGENDARY_ITEM_QUALITY;
|
return !item.name.equals(LEGENDARY.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +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.ItemFactory.QUALITY_ERROR_MESSAGE;
|
import static com.gildedrose.item_helpers.QualityValidator.OUT_OF_BOUND_QUALITY_MESSAGE;
|
||||||
|
import static com.gildedrose.item_helpers.QualityValidator.QUALITY_ERROR_MESSAGE;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class TestHelper {
|
public class TestHelper {
|
||||||
@ -27,4 +28,11 @@ public class TestHelper {
|
|||||||
assertTrue(actualMessage.contains(QUALITY_ERROR_MESSAGE));
|
assertTrue(actualMessage.contains(QUALITY_ERROR_MESSAGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void testItemQualityAboveLimitException(Item item) {
|
||||||
|
GildedRose gildedRose = new GildedRose(item);
|
||||||
|
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
|
||||||
|
String actualMessage = exception.getMessage();
|
||||||
|
assertTrue(actualMessage.contains(OUT_OF_BOUND_QUALITY_MESSAGE));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,10 +8,9 @@ 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.helper.TestHelper.testItemException;
|
import static com.gildedrose.item_helpers.QualityValidator.NOT_LEGENDARY_ITEM_ERROR_MESSAGE;
|
||||||
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
||||||
import static com.gildedrose.items.LegendaryItem.LEGENDARY_ITEM_QUALITY;
|
import static com.gildedrose.items.LegendaryItem.LEGENDARY_ITEM_QUALITY;
|
||||||
import static com.gildedrose.items.LegendaryItem.NOT_LEGENDARY_ITEM_ERROR_MESSAGE;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
@ -43,10 +42,4 @@ class LegendaryItemTest {
|
|||||||
assertTrue(actualMessage.contains(NOT_LEGENDARY_ITEM_ERROR_MESSAGE));
|
assertTrue(actualMessage.contains(NOT_LEGENDARY_ITEM_ERROR_MESSAGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(4)
|
|
||||||
void negativeQualityFail() {
|
|
||||||
testItemException(itemError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,15 +6,15 @@ import org.junit.jupiter.api.Order;
|
|||||||
import org.junit.jupiter.api.Test;
|
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.*;
|
||||||
import static com.gildedrose.helper.TestHelper.testItemException;
|
|
||||||
import static com.gildedrose.item_helpers.ItemName.NORMAL;
|
import static com.gildedrose.item_helpers.ItemName.NORMAL;
|
||||||
|
|
||||||
@TestMethodOrder(OrderAnnotation.class)
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
class NormalItemTest {
|
class NormalItemTest {
|
||||||
|
|
||||||
private final Item item = new Item(NORMAL.toString(), 5, 20);
|
private final Item item = new Item(NORMAL.toString(), 5, 20);
|
||||||
private final Item itemError = new Item(NORMAL.toString(), 10, -5);
|
private final Item itemNegativeQuality = new Item(NORMAL.toString(), 10, -5);
|
||||||
|
private final Item itemAboveLimitQuality = new Item(NORMAL.toString(), 10, 60);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
@ -31,6 +31,12 @@ class NormalItemTest {
|
|||||||
@Test
|
@Test
|
||||||
@Order(3)
|
@Order(3)
|
||||||
void negativeQualityFail() {
|
void negativeQualityFail() {
|
||||||
testItemException(itemError);
|
testItemException(itemNegativeQuality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(4)
|
||||||
|
void QualityAboveLimitFail() {
|
||||||
|
testItemQualityAboveLimitException(itemAboveLimitQuality);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,6 +43,9 @@ class MultipleItemsTest {
|
|||||||
int days = 20;
|
int days = 20;
|
||||||
for (int i = 0; i < days; i++) {
|
for (int i = 0; i < days; i++) {
|
||||||
gildedRose.updateQuality();
|
gildedRose.updateQuality();
|
||||||
|
for (Item item : items) {
|
||||||
|
System.out.println(item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user