mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-10 04:01:19 +00:00
Adding new feature - Conjured item
This commit is contained in:
parent
0238cf2fe2
commit
7c87dedf57
@ -0,0 +1,27 @@
|
|||||||
|
package com.gildedrose.application.conjured;
|
||||||
|
|
||||||
|
import com.gildedrose.core.rule.UpdateInventoryTemplateRule;
|
||||||
|
import com.gildedrose.domain.item.ItemAdapter;
|
||||||
|
|
||||||
|
public class ConjuredRule extends UpdateInventoryTemplateRule {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean canSubtractSellIn(ItemAdapter itemAdapter) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getQualityFactor(boolean isExpired, ItemAdapter itemAdapter) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean canIncreaseQuality(boolean isExpired, ItemAdapter itemAdapter) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean canDecreaseQuality(boolean isExpired, ItemAdapter itemAdapter) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@ package com.gildedrose.core.rule;
|
|||||||
|
|
||||||
import com.gildedrose.application.agedbrie.AgedBrieRule;
|
import com.gildedrose.application.agedbrie.AgedBrieRule;
|
||||||
import com.gildedrose.application.backstagepasses.BackstagePassesRule;
|
import com.gildedrose.application.backstagepasses.BackstagePassesRule;
|
||||||
|
import com.gildedrose.application.conjured.ConjuredRule;
|
||||||
import com.gildedrose.application.standard.StandardItemRule;
|
import com.gildedrose.application.standard.StandardItemRule;
|
||||||
import com.gildedrose.application.sulfuras.SulfurasRule;
|
import com.gildedrose.application.sulfuras.SulfurasRule;
|
||||||
import com.gildedrose.domain.item.ItemAdapter;
|
import com.gildedrose.domain.item.ItemAdapter;
|
||||||
@ -17,6 +18,7 @@ public class InventoryRuleEngine {
|
|||||||
put(ItemType.SULFURAS, new SulfurasRule());
|
put(ItemType.SULFURAS, new SulfurasRule());
|
||||||
put(ItemType.BACKSTAGE_PASSES, new BackstagePassesRule());
|
put(ItemType.BACKSTAGE_PASSES, new BackstagePassesRule());
|
||||||
put(ItemType.STANDARD, new StandardItemRule());
|
put(ItemType.STANDARD, new StandardItemRule());
|
||||||
|
put(ItemType.CONJURED, new ConjuredRule());
|
||||||
}};
|
}};
|
||||||
|
|
||||||
public static void applyUpdateRule(ItemAdapter itemAdapter) {
|
public static void applyUpdateRule(ItemAdapter itemAdapter) {
|
||||||
|
|||||||
@ -10,6 +10,7 @@ public final class ItemAdapterSimpleFactory {
|
|||||||
case AGEG_BRIE -> createAgedBrie(item);
|
case AGEG_BRIE -> createAgedBrie(item);
|
||||||
case BACKSTAGE_PASSES -> createBackStagePasses(item);
|
case BACKSTAGE_PASSES -> createBackStagePasses(item);
|
||||||
case SULFURAS -> createSulfuras(item);
|
case SULFURAS -> createSulfuras(item);
|
||||||
|
case CONJURED -> createConjured(item);
|
||||||
default -> createStandardItem(item);
|
default -> createStandardItem(item);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -29,4 +30,8 @@ public final class ItemAdapterSimpleFactory {
|
|||||||
private static ItemAdapter createStandardItem(Item item) {
|
private static ItemAdapter createStandardItem(Item item) {
|
||||||
return new ItemAdapter(ItemType.STANDARD, item);
|
return new ItemAdapter(ItemType.STANDARD, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ItemAdapter createConjured(Item item) {
|
||||||
|
return new ItemAdapter(ItemType.CONJURED, item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,8 @@ public enum ItemType {
|
|||||||
STANDARD("Standard item"),
|
STANDARD("Standard item"),
|
||||||
AGEG_BRIE("Aged Brie"),
|
AGEG_BRIE("Aged Brie"),
|
||||||
BACKSTAGE_PASSES("Backstage passes to a TAFKAL80ETC concert"),
|
BACKSTAGE_PASSES("Backstage passes to a TAFKAL80ETC concert"),
|
||||||
SULFURAS ("Sulfuras, Hand of Ragnaros")
|
SULFURAS ("Sulfuras, Hand of Ragnaros"),
|
||||||
|
CONJURED("Conjured Mana Cake")
|
||||||
;
|
;
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|||||||
@ -15,7 +15,6 @@ public class TexttestFixture {
|
|||||||
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
|
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
|
||||||
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
|
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
|
||||||
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
|
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
|
||||||
// this conjured item does not work properly yet
|
|
||||||
new Item("Conjured Mana Cake", 3, 6)
|
new Item("Conjured Mana Cake", 3, 6)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,41 @@
|
|||||||
|
package com.gildedrose.application.conjured;
|
||||||
|
|
||||||
|
import com.gildedrose.domain.item.Item;
|
||||||
|
import com.gildedrose.domain.item.ItemAdapter;
|
||||||
|
import com.gildedrose.domain.item.ItemType;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class ConjuredRuleTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void conjuredItemQualityDecreasesByTwo() {
|
||||||
|
//given
|
||||||
|
Item testItem = new Item("Conjured Mana Cake", 3, 6);
|
||||||
|
ItemAdapter itemAdapter = new ItemAdapter(ItemType.CONJURED, testItem);
|
||||||
|
ConjuredRule conjuredRule = new ConjuredRule();
|
||||||
|
|
||||||
|
//when
|
||||||
|
conjuredRule.processItem(itemAdapter);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(2, itemAdapter.getItem().sellIn);
|
||||||
|
assertEquals(4, itemAdapter.getItem().quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void conjuredItemQualityDecreasesByTwoWhenExpired() {
|
||||||
|
//given
|
||||||
|
Item testItem = new Item("Conjured Mana Cake", 0, 6);
|
||||||
|
ItemAdapter itemAdapter = new ItemAdapter(ItemType.CONJURED, testItem);
|
||||||
|
ConjuredRule conjuredRule = new ConjuredRule();
|
||||||
|
|
||||||
|
//when
|
||||||
|
conjuredRule.processItem(itemAdapter);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(-1, itemAdapter.getItem().sellIn);
|
||||||
|
assertEquals(4, itemAdapter.getItem().quality);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -62,4 +62,18 @@ public class InventoryRuleEngineTest {
|
|||||||
assertEquals(4, itemAdapter.getItem().sellIn);
|
assertEquals(4, itemAdapter.getItem().sellIn);
|
||||||
assertEquals(6, itemAdapter.getItem().quality);
|
assertEquals(6, itemAdapter.getItem().quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldApplyConjuredRule() {
|
||||||
|
//given
|
||||||
|
ItemAdapter itemAdapter = new ItemAdapter(ItemType.CONJURED,
|
||||||
|
new Item("Conjured Mana Cake", 3, 6));
|
||||||
|
|
||||||
|
//when
|
||||||
|
InventoryRuleEngine.applyUpdateRule(itemAdapter);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(2, itemAdapter.getItem().sellIn);
|
||||||
|
assertEquals(4, itemAdapter.getItem().quality);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,4 +56,17 @@ public class ItemAdapterSimpleFactoryTest {
|
|||||||
assertEquals(standardItem.quality, result.getItem().quality);
|
assertEquals(standardItem.quality, result.getItem().quality);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldCreateConjuredItemAdapter(){
|
||||||
|
//given
|
||||||
|
Item conjured = new Item("Conjured Mana Cake", 3, 6);
|
||||||
|
//when
|
||||||
|
ItemAdapter result = ItemAdapterSimpleFactory.createItemAdapter(conjured);
|
||||||
|
//then
|
||||||
|
assertEquals(conjured.name, result.getItem().name);
|
||||||
|
assertEquals(conjured.sellIn, result.getItem().sellIn);
|
||||||
|
assertEquals(conjured.quality, result.getItem().quality);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,9 +18,9 @@ public class ItemTypeTest {
|
|||||||
@Test
|
@Test
|
||||||
void findByValue_BackStagePasses() {
|
void findByValue_BackStagePasses() {
|
||||||
// given
|
// given
|
||||||
String agedBrie = "Backstage passes to a TAFKAL80ETC concert";
|
String backstage = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
//when
|
//when
|
||||||
ItemType result = ItemType.findByValue(agedBrie);
|
ItemType result = ItemType.findByValue(backstage);
|
||||||
//then
|
//then
|
||||||
assertEquals(ItemType.BACKSTAGE_PASSES, result);
|
assertEquals(ItemType.BACKSTAGE_PASSES, result);
|
||||||
}
|
}
|
||||||
@ -28,9 +28,9 @@ public class ItemTypeTest {
|
|||||||
@Test
|
@Test
|
||||||
void findByValue_Sulfuras() {
|
void findByValue_Sulfuras() {
|
||||||
// given
|
// given
|
||||||
String agedBrie = "Sulfuras, Hand of Ragnaros";
|
String sulfuras = "Sulfuras, Hand of Ragnaros";
|
||||||
//when
|
//when
|
||||||
ItemType result = ItemType.findByValue(agedBrie);
|
ItemType result = ItemType.findByValue(sulfuras);
|
||||||
//then
|
//then
|
||||||
assertEquals(ItemType.SULFURAS, result);
|
assertEquals(ItemType.SULFURAS, result);
|
||||||
}
|
}
|
||||||
@ -38,10 +38,20 @@ public class ItemTypeTest {
|
|||||||
@Test
|
@Test
|
||||||
void findByValue_StandardItem() {
|
void findByValue_StandardItem() {
|
||||||
// given
|
// given
|
||||||
String agedBrie = "Elixir of the Mongoose";
|
String standardItem = "Elixir of the Mongoose";
|
||||||
//when
|
//when
|
||||||
ItemType result = ItemType.findByValue(agedBrie);
|
ItemType result = ItemType.findByValue(standardItem);
|
||||||
//then
|
//then
|
||||||
assertEquals(ItemType.STANDARD, result);
|
assertEquals(ItemType.STANDARD, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findByValue_Conjured() {
|
||||||
|
// given
|
||||||
|
String conjured = "Conjured Mana Cake";
|
||||||
|
//when
|
||||||
|
ItemType result = ItemType.findByValue(conjured);
|
||||||
|
//then
|
||||||
|
assertEquals(ItemType.CONJURED, result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user