mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-18 16:01:42 +00:00
Create InventoryItem class and refactor access methods
This commit is contained in:
parent
688d88ab1d
commit
54ec015566
60
Java/src/main/java/com/gildedrose/AgedBrie.java
Normal file
60
Java/src/main/java/com/gildedrose/AgedBrie.java
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import static com.gildedrose.Constants.*;
|
||||||
|
|
||||||
|
public class AgedBrie extends InventoryItem {
|
||||||
|
public AgedBrie(Item item) {
|
||||||
|
super(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void age() {
|
||||||
|
if (item.name.equals(AGED_BRIE)
|
||||||
|
|| item.name.equals(BACKSTAGE)) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
|
||||||
|
if (item.name.equals(BACKSTAGE)) {
|
||||||
|
if (item.sellIn < 11) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.sellIn < 6) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (item.quality > 0) {
|
||||||
|
if (!item.name.equals(SULFURAS)) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!item.name.equals(SULFURAS)) {
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
if (item.name.equals(AGED_BRIE)) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!item.name.equals(BACKSTAGE)) {
|
||||||
|
if (item.quality > 0) {
|
||||||
|
if (!item.name.equals(SULFURAS)) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
item.quality = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
60
Java/src/main/java/com/gildedrose/Backstage.java
Normal file
60
Java/src/main/java/com/gildedrose/Backstage.java
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import static com.gildedrose.Constants.*;
|
||||||
|
|
||||||
|
public class Backstage extends InventoryItem {
|
||||||
|
public Backstage(Item item) {
|
||||||
|
super(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void age() {
|
||||||
|
if (item.name.equals(AGED_BRIE)
|
||||||
|
|| item.name.equals(BACKSTAGE)) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
|
||||||
|
if (item.name.equals(BACKSTAGE)) {
|
||||||
|
if (item.sellIn < 11) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.sellIn < 6) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (item.quality > 0) {
|
||||||
|
if (!item.name.equals(SULFURAS)) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!item.name.equals(SULFURAS)) {
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
if (item.name.equals(AGED_BRIE)) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!item.name.equals(BACKSTAGE)) {
|
||||||
|
if (item.quality > 0) {
|
||||||
|
if (!item.name.equals(SULFURAS)) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
item.quality = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
Java/src/main/java/com/gildedrose/Constants.java
Normal file
9
Java/src/main/java/com/gildedrose/Constants.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public final class Constants {
|
||||||
|
public static final String BACKSTAGE = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
|
public static final String AGED_BRIE = "Aged Brie";
|
||||||
|
public static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||||
|
private Constants() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,65 +1,17 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
public static final String BACKSTAGE = "Backstage passes to a TAFKAL80ETC concert";
|
|
||||||
public static final String AGED_BRIE = "Aged Brie";
|
|
||||||
public static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
public GildedRose(Item[] items) {
|
public GildedRose(Item[] items) {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
public static void updateQuality(GildedRose gildedRose) {
|
||||||
for (Item item : items) {
|
Arrays.stream(gildedRose.items).forEach(item -> InventoryItem.createInventoryItem(item).age());
|
||||||
if (!item.name.equals(AGED_BRIE)
|
|
||||||
&& !item.name.equals(BACKSTAGE)) {
|
|
||||||
if (item.quality > 0) {
|
|
||||||
if (!item.name.equals(SULFURAS)) {
|
|
||||||
item.quality = item.quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (item.quality < 50) {
|
|
||||||
item.quality = item.quality + 1;
|
|
||||||
|
|
||||||
if (item.name.equals(BACKSTAGE)) {
|
|
||||||
if (item.sellIn < 11) {
|
|
||||||
if (item.quality < 50) {
|
|
||||||
item.quality = item.quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.sellIn < 6) {
|
|
||||||
if (item.quality < 50) {
|
|
||||||
item.quality = item.quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!item.name.equals(SULFURAS)) {
|
|
||||||
item.sellIn = item.sellIn - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.sellIn < 0) {
|
|
||||||
if (!item.name.equals(AGED_BRIE)) {
|
|
||||||
if (!item.name.equals(BACKSTAGE)) {
|
|
||||||
if (item.quality > 0) {
|
|
||||||
if (!item.name.equals(SULFURAS)) {
|
|
||||||
item.quality = item.quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
item.quality = item.quality - item.quality;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (item.quality < 50) {
|
|
||||||
item.quality = item.quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
32
Java/src/main/java/com/gildedrose/InventoryItem.java
Normal file
32
Java/src/main/java/com/gildedrose/InventoryItem.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import static com.gildedrose.Constants.*;
|
||||||
|
|
||||||
|
public class InventoryItem {
|
||||||
|
protected final Item item;
|
||||||
|
|
||||||
|
InventoryItem(Item item) {
|
||||||
|
this.item = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InventoryItem createInventoryItem(Item item) {
|
||||||
|
if (item.name.equals(SULFURAS)) return new Sulfuras(item);
|
||||||
|
if (item.name.equals(AGED_BRIE)) return new AgedBrie(item);
|
||||||
|
if (item.name.equals(BACKSTAGE)) return new Backstage(item);
|
||||||
|
return new InventoryItem(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
void age() {
|
||||||
|
decreaseQuality();
|
||||||
|
decreaseSellIn();
|
||||||
|
if (item.sellIn < 0) decreaseQuality();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void decreaseSellIn() {
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void decreaseQuality() {
|
||||||
|
if (item.quality > 0) item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Java/src/main/java/com/gildedrose/Sulfuras.java
Normal file
12
Java/src/main/java/com/gildedrose/Sulfuras.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package com.gildedrose;
|
||||||
|
|
||||||
|
public class Sulfuras extends InventoryItem {
|
||||||
|
public Sulfuras(Item item) {
|
||||||
|
super(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void age() {
|
||||||
|
// Sulfuras never changes its properties
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -10,35 +10,35 @@ class GildedRoseTest {
|
|||||||
void foo() {
|
void foo() {
|
||||||
Item[] items = new Item[]{new Item("foo", 0, 0)};
|
Item[] items = new Item[]{new Item("foo", 0, 0)};
|
||||||
GildedRose app = new GildedRose(items);
|
GildedRose app = new GildedRose(items);
|
||||||
app.updateQuality();
|
GildedRose.updateQuality(app);
|
||||||
assertEquals("foo", app.items[0].name);
|
assertEquals("foo", app.items[0].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void degradesTwiceAsFastAfterSellInDateHasPassed() {
|
void degradesTwiceAsFastAfterSellInDateHasPassed() {
|
||||||
Item[] items = new Item[]{new Item("foo", 0, 2)};
|
Item[] items = new Item[]{new Item("foo", 0, 2)};
|
||||||
new GildedRose(items).updateQuality();
|
GildedRose.updateQuality(new GildedRose(items));
|
||||||
assertEquals(0, items[0].quality);
|
assertEquals(0, items[0].quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void neverMustHaveNegativeQuality() {
|
void neverMustHaveNegativeQuality() {
|
||||||
Item[] items = new Item[]{new Item("foo", 0, 0)};
|
Item[] items = new Item[]{new Item("foo", 0, 0)};
|
||||||
new GildedRose(items).updateQuality();
|
GildedRose.updateQuality(new GildedRose(items));
|
||||||
assertEquals(0, items[0].quality);
|
assertEquals(0, items[0].quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void increaseAgedBrieQualityWhenItGetsOlder() {
|
void increaseAgedBrieQualityWhenItGetsOlder() {
|
||||||
Item[] items = new Item[]{new Item("Aged Brie", 0, 2)};
|
Item[] items = new Item[]{new Item("Aged Brie", 0, 2)};
|
||||||
new GildedRose(items).updateQuality();
|
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("Aged Brie", 0, 50)};
|
||||||
new GildedRose(items).updateQuality();
|
GildedRose.updateQuality(new GildedRose(items));
|
||||||
assertEquals(50, items[0].quality);
|
assertEquals(50, items[0].quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,14 +46,14 @@ 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("Sulfuras, Hand of Ragnaros", 0, 50)};
|
||||||
new GildedRose(items).updateQuality();
|
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("Sulfuras, Hand of Ragnaros", 0, 80)};
|
||||||
new GildedRose(items).updateQuality();
|
GildedRose.updateQuality(new GildedRose(items));
|
||||||
assertEquals(80, items[0].quality);
|
assertEquals(80, items[0].quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ class GildedRoseTest {
|
|||||||
Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
|
Item[] items = new Item[]{new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
|
||||||
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 20),
|
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 20),
|
||||||
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 20)};
|
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 20)};
|
||||||
new GildedRose(items).updateQuality();
|
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);
|
||||||
|
|||||||
@ -36,7 +36,7 @@ class GoldenMasterApprovalTest {
|
|||||||
.setSellIn(sellIn)
|
.setSellIn(sellIn)
|
||||||
.setQuality(quality)
|
.setQuality(quality)
|
||||||
.createItem();
|
.createItem();
|
||||||
new GildedRose(new Item[]{item}).updateQuality();
|
GildedRose.updateQuality(new GildedRose(new Item[]{item}));
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,7 +30,7 @@ public class TexttestFixture {
|
|||||||
System.out.println(item);
|
System.out.println(item);
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
app.updateQuality();
|
GildedRose.updateQuality(app);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user