Add Conjured item to business logic

This commit is contained in:
Roberto de la Banda 2021-09-11 15:27:18 +02:00
parent 79851ff2e9
commit c3b2ebe7af
6 changed files with 1364 additions and 673 deletions

View File

@ -0,0 +1,19 @@
package com.gildedrose;
public class Conjured extends InventoryItem {
public Conjured(Item item) {
super(item);
}
@Override
void age() {
decreaseQuality();
decreaseSellIn();
if (item.sellIn < Constants.SELLIN_DAY) decreaseQuality();
}
@Override
protected void decreaseQuality() {
if (item.quality > Constants.MIN_QUALITY) item.quality -= 2;
}
}

View File

@ -4,6 +4,7 @@ public final class Constants {
public static final String BACKSTAGE = "Backstage passes to a TAFKAL80ETC concert"; public static final String BACKSTAGE = "Backstage passes to a TAFKAL80ETC concert";
public static final String AGED_BRIE = "Aged Brie"; public static final String AGED_BRIE = "Aged Brie";
public static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; public static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
public static final String CONJURED = "Conjured Mana Cake";
public static final int MAX_QUALITY = 50; public static final int MAX_QUALITY = 50;
public static final int MIN_QUALITY = 0; public static final int MIN_QUALITY = 0;
public static final int SELLIN_DAY = 0; public static final int SELLIN_DAY = 0;

View File

@ -13,13 +13,14 @@ public class InventoryItem {
if (item.name.equals(SULFURAS)) return new Sulfuras(item); if (item.name.equals(SULFURAS)) return new Sulfuras(item);
if (item.name.equals(AGED_BRIE)) return new AgedBrie(item); if (item.name.equals(AGED_BRIE)) return new AgedBrie(item);
if (item.name.equals(BACKSTAGE)) return new Backstage(item); if (item.name.equals(BACKSTAGE)) return new Backstage(item);
if (item.name.equals(CONJURED)) return new Conjured(item);
return new InventoryItem(item); return new InventoryItem(item);
} }
void age() { void age() {
decreaseQuality(); decreaseQuality();
decreaseSellIn(); decreaseSellIn();
if (item.sellIn < 0) decreaseQuality(); if (item.sellIn < Constants.SELLIN_DAY) decreaseQuality();
} }
protected void decreaseSellIn() { protected void decreaseSellIn() {
@ -27,6 +28,6 @@ public class InventoryItem {
} }
protected void decreaseQuality() { protected void decreaseQuality() {
if (item.quality > 0) item.quality--; if (item.quality > Constants.MIN_QUALITY) item.quality--;
} }
} }

View File

@ -30,14 +30,14 @@ class GildedRoseTest {
@Test @Test
void increaseAgedBrieQualityWhenItGetsOlder() { void increaseAgedBrieQualityWhenItGetsOlder() {
Item[] items = new Item[]{new Item("Aged Brie", 0, 2)}; Item[] items = new Item[]{new Item(Constants.AGED_BRIE, 0, 2)};
GildedRose.updateQuality(new GildedRose(items)); GildedRose.updateQuality(new GildedRose(items));
assertEquals(4, items[0].quality); assertEquals(4, items[0].quality);
} }
@Test @Test
void neverMustHaveAnItemWithMoreThan50OfQuality() { void neverMustHaveAnItemWithMoreThan50OfQuality() {
Item[] items = new Item[]{new Item("Aged Brie", 0, 50)}; Item[] items = new Item[]{new Item(Constants.AGED_BRIE, 0, 50)};
GildedRose.updateQuality(new GildedRose(items)); GildedRose.updateQuality(new GildedRose(items));
assertEquals(50, items[0].quality); assertEquals(50, items[0].quality);
} }
@ -45,26 +45,33 @@ class GildedRoseTest {
@Test @Test
void neverModifySulfurasQuality() { void neverModifySulfurasQuality() {
Item[] items = new Item[]{new Item("Sulfuras, Hand of Ragnaros", 0, 50)}; Item[] items = new Item[]{new Item(Constants.SULFURAS, 0, 50)};
GildedRose.updateQuality(new GildedRose(items)); GildedRose.updateQuality(new GildedRose(items));
assertEquals(50, items[0].quality); assertEquals(50, items[0].quality);
} }
@Test @Test
void neverModifySulfurasQualityEvenISGreaterThan50() { void neverModifySulfurasQualityEvenISGreaterThan50() {
Item[] items = new Item[]{new Item("Sulfuras, Hand of Ragnaros", 0, 80)}; Item[] items = new Item[]{new Item(Constants.SULFURAS, 0, 80)};
GildedRose.updateQuality(new GildedRose(items)); GildedRose.updateQuality(new GildedRose(items));
assertEquals(80, items[0].quality); assertEquals(80, items[0].quality);
} }
@Test @Test
void mustIncreaseBackstagePassesQualityWhenItsSellInApproaches() { void mustIncreaseBackstagePassesQualityWhenItsSellInApproaches() {
Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), Item[] items = new Item[]{new Item(Constants.BACKSTAGE, 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 20), new Item(Constants.BACKSTAGE, 10, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 20)}; new Item(Constants.BACKSTAGE, 5, 20)};
GildedRose.updateQuality(new GildedRose(items)); GildedRose.updateQuality(new GildedRose(items));
assertEquals(21, items[0].quality); assertEquals(21, items[0].quality);
assertEquals(22, items[1].quality); assertEquals(22, items[1].quality);
assertEquals(23, items[2].quality); assertEquals(23, items[2].quality);
} }
@Test
void mustDecreaseQualityTwiceAsFastIfItemIsConjured() {
Item[] items = new Item[]{new Item(Constants.CONJURED, 0, 20)};
GildedRose.updateQuality(new GildedRose(items));
assertEquals(16, items[0].quality);
}
} }

View File

@ -19,7 +19,7 @@ class GoldenMasterApprovalTest {
} }
private String[] itemTypes() { private String[] itemTypes() {
return new String[]{"common item", "Aged Brie", "Backstage passes to a TAFKAL80ETC concert", "Sulfuras, Hand of Ragnaros"}; return new String[]{Constants.AGED_BRIE, Constants.BACKSTAGE, "Common item", Constants.CONJURED, Constants.SULFURAS};
} }
private Integer[] sellInRangeValues() { private Integer[] sellInRangeValues() {