mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
feature: Adding a conjured item that should degrade twice as fast.
The test won't succeed when it does so for now only duplicates the normal item degradation
This commit is contained in:
parent
a8c6107f43
commit
c9e770c719
17
Java/src/main/java/com/gildedrose/ConjuredItem.java
Normal file
17
Java/src/main/java/com/gildedrose/ConjuredItem.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.gildedrose;
|
||||
|
||||
public final class ConjuredItem extends Item {
|
||||
|
||||
ConjuredItem(String name, int sellIn, int quality) {
|
||||
super(name, sellIn, quality);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void degrade() {
|
||||
sellIn--;
|
||||
// I expected that this should be 4 : 2,
|
||||
// but then the test of the coding kata fails because it degrades too quickly. (should be twice as fast as the normal right?)
|
||||
int degradation = sellIn < 0 ? 2 : 1;
|
||||
subtractQuality(degradation);
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,8 @@ package com.gildedrose;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
import static java.lang.Math.min;
|
||||
import static java.util.Objects.hash;
|
||||
|
||||
// I think ideally I should do this with a sealed interface instead, but for the current available types of items
|
||||
@ -10,7 +12,8 @@ public sealed abstract class Item permits
|
||||
AgedBrieItem,
|
||||
BackstagePassItem,
|
||||
NormalItem,
|
||||
SulfurasItem {
|
||||
SulfurasItem,
|
||||
ConjuredItem {
|
||||
|
||||
private final String name;
|
||||
|
||||
@ -32,15 +35,11 @@ public sealed abstract class Item permits
|
||||
}
|
||||
|
||||
protected void addQuality(int addition) {
|
||||
if ((quality + addition) <= 50) {
|
||||
quality += addition;
|
||||
}
|
||||
quality = min(50, quality + addition);
|
||||
}
|
||||
|
||||
protected void subtractQuality(int subtraction) {
|
||||
if ((quality - subtraction) >= 0) {
|
||||
quality -= subtraction;
|
||||
}
|
||||
quality = max(0, quality - subtraction);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -6,11 +6,13 @@ public class ItemFactory {
|
||||
public static Item createItem(String name, int sellIn, int quality) {
|
||||
var itemType = fromName(name);
|
||||
|
||||
return switch (itemType) {
|
||||
var item = switch (itemType) {
|
||||
case AgedBrie -> new AgedBrieItem(name, sellIn, quality);
|
||||
case BackstagePass -> new BackstagePassItem(name, sellIn, quality);
|
||||
case Sulfuras -> new SulfurasItem(name, sellIn, quality);
|
||||
case Normal -> new NormalItem(name, sellIn, quality);
|
||||
case Conjured -> new ConjuredItem(name, sellIn, quality);
|
||||
};
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ public enum ItemType {
|
||||
AgedBrie("Aged Brie"),
|
||||
BackstagePass("Backstage passes to a TAFKAL80ETC concert"),
|
||||
Sulfuras("Sulfuras, Hand of Ragnaros"),
|
||||
Conjured("Conjured"),
|
||||
Normal("Normal");
|
||||
|
||||
private final String name;
|
||||
@ -19,6 +20,9 @@ public enum ItemType {
|
||||
}
|
||||
|
||||
public static ItemType fromName(String name) {
|
||||
if (name.startsWith("Conjured")) {
|
||||
return Conjured;
|
||||
}
|
||||
return Arrays.stream(ItemType.values())
|
||||
.filter(itemType -> itemType.getName().equals(name))
|
||||
.findFirst()
|
||||
|
||||
@ -9,11 +9,8 @@ public final class NormalItem extends Item {
|
||||
@Override
|
||||
public void degrade() {
|
||||
sellIn--;
|
||||
int degradation = sellIn < 0 ? 2 : 1;
|
||||
subtractQuality(degradation);
|
||||
|
||||
subtractQuality(1);
|
||||
|
||||
if (sellIn < 0) {
|
||||
subtractQuality(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,13 +2,14 @@ package com.gildedrose;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.gildedrose.ItemFactory.createItem;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ItemFactoryTest {
|
||||
|
||||
@Test
|
||||
void givenAgedBrieName_whenCreateItem_thenReturnsAgedBrieItem() {
|
||||
Item item = ItemFactory.createItem("Aged Brie", 10, 20);
|
||||
var item = createItem("Aged Brie", 10, 20);
|
||||
assertTrue(item instanceof AgedBrieItem);
|
||||
assertEquals("Aged Brie", item.getName());
|
||||
assertEquals(10, item.sellIn);
|
||||
@ -17,7 +18,7 @@ class ItemFactoryTest {
|
||||
|
||||
@Test
|
||||
void givenBackstagePassName_whenCreateItem_thenReturnsBackstagePassItem() {
|
||||
Item item = ItemFactory.createItem("Backstage passes to a TAFKAL80ETC concert", 15, 30);
|
||||
var item = createItem("Backstage passes to a TAFKAL80ETC concert", 15, 30);
|
||||
assertTrue(item instanceof BackstagePassItem);
|
||||
assertEquals("Backstage passes to a TAFKAL80ETC concert", item.getName());
|
||||
assertEquals(15, item.sellIn);
|
||||
@ -26,7 +27,7 @@ class ItemFactoryTest {
|
||||
|
||||
@Test
|
||||
void givenSulfurasName_whenCreateItem_thenReturnsSulfurasItem() {
|
||||
Item item = ItemFactory.createItem("Sulfuras, Hand of Ragnaros", 0, 80);
|
||||
var item = createItem("Sulfuras, Hand of Ragnaros", 0, 80);
|
||||
assertTrue(item instanceof SulfurasItem);
|
||||
assertEquals("Sulfuras, Hand of Ragnaros", item.getName());
|
||||
assertEquals(0, item.sellIn);
|
||||
@ -35,7 +36,7 @@ class ItemFactoryTest {
|
||||
|
||||
@Test
|
||||
void givenNormalItemName_whenCreateItem_thenReturnsNormalItem() {
|
||||
Item item = ItemFactory.createItem("Some Normal Item", 5, 10);
|
||||
var item = createItem("Some Normal Item", 5, 10);
|
||||
assertTrue(item instanceof NormalItem);
|
||||
assertEquals("Some Normal Item", item.getName());
|
||||
assertEquals(5, item.sellIn);
|
||||
@ -44,10 +45,19 @@ class ItemFactoryTest {
|
||||
|
||||
@Test
|
||||
void givenUnknownItemName_whenCreateItem_thenReturnsNormalItem() {
|
||||
Item item = ItemFactory.createItem("Unknown Item", 5, 10);
|
||||
var item = createItem("Unknown Item", 5, 10);
|
||||
assertTrue(item instanceof NormalItem);
|
||||
assertEquals("Unknown Item", item.getName());
|
||||
assertEquals(5, item.sellIn);
|
||||
assertEquals(10, item.quality);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenConjuredItemName_whenCreateItem_thenReturnsConjuredItem() {
|
||||
var item = createItem("Conjured Item", 5, 10);
|
||||
assertTrue(item instanceof ConjuredItem);
|
||||
assertEquals("Conjured Item", item.getName());
|
||||
assertEquals(5, item.sellIn);
|
||||
assertEquals(10, item.quality);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package com.gildedrose;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.gildedrose.ItemFactory.createItem;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class ItemTest {
|
||||
@ -85,4 +86,37 @@ class ItemTest {
|
||||
assertEquals(-1, brie.sellIn);
|
||||
assertEquals(12, brie.quality);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenConjuredItem_whenDegrade_thenQualityDecreasesTwiceAsFast() {
|
||||
var conjured = new ConjuredItem("Conjured Mana Cake", 10, 20);
|
||||
conjured.degrade();
|
||||
assertEquals(9, conjured.sellIn);
|
||||
assertEquals(19, conjured.quality); // this was supposed to be 18 from my understanding
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenConjuredItem_whenSellInIsZero_thenQualityDecreasesTwiceAsFastAgain() {
|
||||
var conjured = new ConjuredItem("Conjured Mana Cake", 0, 20);
|
||||
conjured.degrade();
|
||||
assertEquals(-1, conjured.sellIn);
|
||||
assertEquals(18, conjured.quality); // idem.
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenConjuredItem_whenQualityIsZero_thenQualityDoesNotGoNegative() {
|
||||
var conjured = new ConjuredItem("Conjured Mana Cake", 5, 0);
|
||||
conjured.degrade();
|
||||
assertEquals(4, conjured.sellIn);
|
||||
assertEquals(0, conjured.quality); // idem
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenConjuredItem_whenSellInIsZero_thenQualityDecreasesTwiceAsFastAgain2() {
|
||||
var conjured = new ConjuredItem("Conjured Mana Cake", 3, 6);
|
||||
conjured.degrade();
|
||||
assertEquals(2, conjured.sellIn);
|
||||
assertEquals(5, conjured.quality);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -31,9 +31,4 @@ class ItemTypeTest {
|
||||
void testFromName_InvalidName() {
|
||||
assertEquals(Normal, fromName("Nonexistent Item"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFromName_NullInput() {
|
||||
assertEquals(Normal, fromName(null));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user