mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
🔨 refactoring
This commit is contained in:
parent
65e15957de
commit
701a3c9eb0
@ -1,6 +1,6 @@
|
||||
package com.gildedrose.item_helpers;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
import com.gildedrose.main.Item;
|
||||
import com.gildedrose.items.AgedBrieItem;
|
||||
import com.gildedrose.items.BackstagePassItem;
|
||||
import com.gildedrose.items.ConjuredItem;
|
||||
|
||||
@ -1,15 +1,14 @@
|
||||
package com.gildedrose.item_helpers;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
import com.gildedrose.main.Item;
|
||||
|
||||
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
||||
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 {
|
||||
|
||||
private static final int LEGENDARY_ITEM_QUALITY = 80;
|
||||
protected static final String NEGATIVE_QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: ";
|
||||
protected static final String NOT_LEGENDARY_ITEM_ERROR_MESSAGE = "Item is legendary, quality must be always 80! Current value: ";
|
||||
protected static final String QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: ";
|
||||
|
||||
private final Item item;
|
||||
|
||||
@ -19,7 +18,7 @@ public class ItemHandler {
|
||||
|
||||
static void validate(Item item) {
|
||||
if (qualityIsNegative(item)) {
|
||||
throw new IllegalArgumentException(NEGATIVE_QUALITY_ERROR_MESSAGE + item.quality);
|
||||
throw new IllegalArgumentException(QUALITY_ERROR_MESSAGE + item.quality);
|
||||
}
|
||||
if (isNotLegendary(item)) {
|
||||
throw new IllegalArgumentException(NOT_LEGENDARY_ITEM_ERROR_MESSAGE + item.quality);
|
||||
@ -30,8 +29,12 @@ public class ItemHandler {
|
||||
return item.quality < 0;
|
||||
}
|
||||
|
||||
private static boolean isNotLegendary(Item item) {
|
||||
return item.name.equals(LEGENDARY.toString()) && item.quality != 80;
|
||||
public boolean qualityIsHigherThanZero() {
|
||||
return item.quality > 0;
|
||||
}
|
||||
|
||||
public void makeQualityZero() {
|
||||
item.quality = 0;
|
||||
}
|
||||
|
||||
public void decrementSellInDate() {
|
||||
@ -42,32 +45,6 @@ public class ItemHandler {
|
||||
return item.sellIn >= 0;
|
||||
}
|
||||
|
||||
public void incrementQualityByTwo() {
|
||||
item.quality = max(item.quality + 2, 0);
|
||||
}
|
||||
|
||||
public void incrementQuality() {
|
||||
item.quality++;
|
||||
}
|
||||
|
||||
public void decrementQuality() {
|
||||
item.quality--;
|
||||
}
|
||||
|
||||
public void decrementQualityBy4() {
|
||||
item.quality = max(item.quality - 4, 0);
|
||||
}
|
||||
|
||||
public void decrementQualityBy2() {
|
||||
item.quality = max(item.quality - 2, 0);
|
||||
}
|
||||
|
||||
public void setLegendaryQuality() {
|
||||
if (item.quality != LEGENDARY_ITEM_QUALITY) {
|
||||
item.quality = LEGENDARY_ITEM_QUALITY;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean sellInLessThan5Days() {
|
||||
return item.sellIn >= 0 && item.sellIn <= 5;
|
||||
}
|
||||
@ -76,20 +53,32 @@ public class ItemHandler {
|
||||
return item.sellIn >= 5 && item.sellIn <= 10;
|
||||
}
|
||||
|
||||
public boolean sellInDaysMoreThan10Days() {
|
||||
public boolean sellInMoreThan10Days() {
|
||||
return item.sellIn >= 10;
|
||||
}
|
||||
|
||||
public boolean qualityIsHigherThanZero() {
|
||||
return item.quality > 0;
|
||||
public void incrementQuality() {
|
||||
item.quality++;
|
||||
}
|
||||
|
||||
public void makeQualityZero() {
|
||||
item.quality = 0;
|
||||
public void incrementQualityBy2() {
|
||||
item.quality = max(item.quality + 2, 0);
|
||||
}
|
||||
|
||||
public void incrementQualityBy3() {
|
||||
item.quality = max(item.quality + 3, 0);
|
||||
}
|
||||
|
||||
public void decrementQuality() {
|
||||
item.quality--;
|
||||
}
|
||||
|
||||
public void decrementQualityBy2() {
|
||||
item.quality = max(item.quality - 2, 0);
|
||||
}
|
||||
|
||||
public void decrementQualityBy4() {
|
||||
item.quality = max(item.quality - 4, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
package com.gildedrose.item_helpers;
|
||||
|
||||
public interface ItemType {
|
||||
|
||||
void updateQuality();
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.gildedrose.items;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
import com.gildedrose.main.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
@ -18,7 +18,7 @@ public class AgedBrieItem implements ItemType {
|
||||
if (item.beforeSellInDate()) {
|
||||
item.incrementQuality();
|
||||
} else {
|
||||
item.incrementQualityByTwo();
|
||||
item.incrementQualityBy2();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.gildedrose.items;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
import com.gildedrose.main.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
@ -15,10 +15,10 @@ public class BackstagePassItem implements ItemType {
|
||||
@Override
|
||||
public void updateQuality() {
|
||||
item.decrementSellInDate();
|
||||
if (item.sellInDaysMoreThan10Days()) {
|
||||
if (item.sellInMoreThan10Days()) {
|
||||
item.incrementQuality();
|
||||
} else if (item.sellInLessThan10Days()) {
|
||||
item.incrementQualityByTwo();
|
||||
item.incrementQualityBy2();
|
||||
} else if (item.sellInLessThan5Days()) {
|
||||
item.incrementQualityBy3();
|
||||
} else {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.gildedrose.items;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
import com.gildedrose.main.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
|
||||
@ -1,11 +1,15 @@
|
||||
package com.gildedrose.items;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
import com.gildedrose.main.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
|
||||
|
||||
public class LegendaryItem implements ItemType {
|
||||
|
||||
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;
|
||||
|
||||
public LegendaryItem(Item item) {
|
||||
@ -15,7 +19,10 @@ public class LegendaryItem implements ItemType {
|
||||
@Override
|
||||
public void updateQuality() {
|
||||
item.decrementSellInDate();
|
||||
item.setLegendaryQuality();
|
||||
}
|
||||
|
||||
public static boolean isNotLegendary(Item item) {
|
||||
return item.name.equals(LEGENDARY.toString()) && item.quality != LEGENDARY_ITEM_QUALITY;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.gildedrose.items;
|
||||
|
||||
import com.gildedrose.Item;
|
||||
import com.gildedrose.main.Item;
|
||||
import com.gildedrose.item_helpers.ItemHandler;
|
||||
import com.gildedrose.item_helpers.ItemType;
|
||||
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
package com.gildedrose;
|
||||
package com.gildedrose.main;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.gildedrose.item_helpers.ItemFactory.getItem;
|
||||
import static java.util.Collections.singletonList;
|
||||
|
||||
class GildedRose {
|
||||
List<Item> items;
|
||||
public class GildedRose {
|
||||
private final List<Item> items;
|
||||
|
||||
public GildedRose(List<Item> items) {
|
||||
this.items = items;
|
||||
@ -1,4 +1,4 @@
|
||||
package com.gildedrose;
|
||||
package com.gildedrose.main;
|
||||
|
||||
public class Item {
|
||||
|
||||
@ -14,8 +14,8 @@ public class Item {
|
||||
this.quality = quality;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name + ", " + this.sellIn + ", " + this.quality;
|
||||
}
|
||||
}
|
||||
@ -1,24 +1,16 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.gildedrose.main.GildedRose;
|
||||
import com.gildedrose.main.Item;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.gildedrose.item_helpers.ItemName.*;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class GildedRoseTest {
|
||||
|
||||
@Test
|
||||
void foo() {
|
||||
List<Item> items = singletonList(new Item("foo", 0, 0));
|
||||
GildedRose app = new GildedRose(items);
|
||||
app.updateQuality();
|
||||
assertEquals("foo", app.items.get(0).name);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNormalItem() {
|
||||
int days = 20;
|
||||
@ -46,9 +38,9 @@ class GildedRoseTest {
|
||||
|
||||
|
||||
@Test
|
||||
void testSulfuraItem() {
|
||||
void testLegendaryItem() {
|
||||
int days = 20;
|
||||
Item legendaryItem = new Item(LEGENDARY.toString(), 10, 40);
|
||||
Item legendaryItem = new Item(LEGENDARY.toString(), 10, 80);
|
||||
GildedRose app = new GildedRose(legendaryItem);
|
||||
for (int i = 0; i < days; i++) {
|
||||
app.updateQuality();
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.gildedrose.main.GildedRose;
|
||||
import com.gildedrose.main.Item;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class TexttestFixture {
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.gildedrose.main.GildedRose;
|
||||
import com.gildedrose.main.Item;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class TexttestFixtureTemp {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user