mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-10 20:21:26 +00:00
refactor conditional to polymorphism - Sulfuras
This commit is contained in:
parent
d9d0bb2060
commit
cf46998049
@ -62,8 +62,7 @@ class GildedRose {
|
|||||||
item.quality = MINIMUM_QUALITY;
|
item.quality = MINIMUM_QUALITY;
|
||||||
}
|
}
|
||||||
} else if (isSulfuras) {
|
} else if (isSulfuras) {
|
||||||
//sulfuras is doing nothing
|
InventoryRuleEngine.processUpdateInventoryRule(itemAdapter);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//standard item
|
//standard item
|
||||||
if (shouldDecreaseQuality(item)) {
|
if (shouldDecreaseQuality(item)) {
|
||||||
|
|||||||
@ -0,0 +1,27 @@
|
|||||||
|
package com.gildedrose.application.sulfuras;
|
||||||
|
|
||||||
|
import com.gildedrose.core.rules.UpdateInventoryTemplateRule;
|
||||||
|
import com.gildedrose.domain.item.ItemAdapter;
|
||||||
|
|
||||||
|
public class SulfurasRule extends UpdateInventoryTemplateRule {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean canSubtractSellIn(final ItemAdapter itemAdapter) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getQualityFactor(final boolean isExpired, final ItemAdapter itemAdapter) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean canIncreaseQuality(final ItemAdapter itemAdapter) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean canDecreaseQuality(final ItemAdapter itemAdapter) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
package com.gildedrose.core;
|
package com.gildedrose.core;
|
||||||
|
|
||||||
import com.gildedrose.application.agedbrie.AgedBrieRule;
|
import com.gildedrose.application.agedbrie.AgedBrieRule;
|
||||||
|
import com.gildedrose.application.sulfuras.SulfurasRule;
|
||||||
import com.gildedrose.core.rules.UpdateInventoryTemplateRule;
|
import com.gildedrose.core.rules.UpdateInventoryTemplateRule;
|
||||||
import com.gildedrose.domain.item.ItemAdapter;
|
import com.gildedrose.domain.item.ItemAdapter;
|
||||||
import com.gildedrose.domain.item.ItemType;
|
import com.gildedrose.domain.item.ItemType;
|
||||||
@ -12,6 +13,7 @@ public class InventoryRuleEngine {
|
|||||||
|
|
||||||
private static final Map<ItemType, UpdateInventoryTemplateRule> updateInventoryRules = new HashMap<>(){{
|
private static final Map<ItemType, UpdateInventoryTemplateRule> updateInventoryRules = new HashMap<>(){{
|
||||||
put(ItemType.AGEG_BRIE, new AgedBrieRule());
|
put(ItemType.AGEG_BRIE, new AgedBrieRule());
|
||||||
|
put(ItemType.SULFURAS, new SulfurasRule());
|
||||||
}};
|
}};
|
||||||
|
|
||||||
public static void processUpdateInventoryRule(ItemAdapter itemAdapter) {
|
public static void processUpdateInventoryRule(ItemAdapter itemAdapter) {
|
||||||
|
|||||||
@ -1,39 +0,0 @@
|
|||||||
package com.gildedrose;
|
|
||||||
|
|
||||||
import com.gildedrose.domain.item.Item;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
class SulfurasTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void sulfurasNeverChanges() {
|
|
||||||
//given
|
|
||||||
Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", -1, 80);
|
|
||||||
Item[] items = new Item[] {sulfuras};
|
|
||||||
|
|
||||||
//when
|
|
||||||
GildedRose app = new GildedRose(items);
|
|
||||||
app.updateQuality();
|
|
||||||
|
|
||||||
//then
|
|
||||||
assertEquals(0, app.items[0].sellIn);
|
|
||||||
assertEquals(80, app.items[0].quality);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void sulfurasQualityLevelIsAlways80() {
|
|
||||||
//given
|
|
||||||
Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 0, 80);
|
|
||||||
Item[] items = new Item[] {sulfuras};
|
|
||||||
|
|
||||||
//when
|
|
||||||
GildedRose app = new GildedRose(items);
|
|
||||||
app.updateQuality();
|
|
||||||
|
|
||||||
//then
|
|
||||||
assertEquals(80, app.items[0].quality);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,6 +1,6 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
import com.gildedrose.item.Item;
|
import com.gildedrose.domain.item.Item;
|
||||||
|
|
||||||
public class TexttestFixture {
|
public class TexttestFixture {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -20,7 +20,7 @@ public class TexttestFixture {
|
|||||||
|
|
||||||
GildedRose app = new GildedRose(items);
|
GildedRose app = new GildedRose(items);
|
||||||
|
|
||||||
int days = 3;
|
int days = 5;
|
||||||
if (args.length > 0) {
|
if (args.length > 0) {
|
||||||
days = Integer.parseInt(args[0]) + 1;
|
days = Integer.parseInt(args[0]) + 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,40 @@
|
|||||||
|
package com.gildedrose.application.sulfuras;
|
||||||
|
|
||||||
|
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 SulfurasRuleTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sulfurasNeverChanges() {
|
||||||
|
//given
|
||||||
|
Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", -1, 80);
|
||||||
|
ItemAdapter sulfurasAdapter = new ItemAdapter(ItemType.SULFURAS, sulfuras);
|
||||||
|
SulfurasRule sulfurasRule = new SulfurasRule();
|
||||||
|
|
||||||
|
//when
|
||||||
|
sulfurasRule.processItem(sulfurasAdapter);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(-1, sulfurasAdapter.getItem().sellIn);
|
||||||
|
assertEquals(80, sulfurasAdapter.getItem().quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sulfurasQualityLevelIsAlways80() {
|
||||||
|
//given
|
||||||
|
Item sulfuras = new Item("Sulfuras, Hand of Ragnaros", 0, 80);
|
||||||
|
ItemAdapter sulfurasAdapter = new ItemAdapter(ItemType.SULFURAS, sulfuras);
|
||||||
|
SulfurasRule sulfurasRule = new SulfurasRule();
|
||||||
|
|
||||||
|
//when
|
||||||
|
sulfurasRule.processItem(sulfurasAdapter);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(80, sulfurasAdapter.getItem().quality);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user