replaces strings for items names with constants

This commit is contained in:
Gabba 2017-12-16 11:59:57 +00:00
parent e8780706a1
commit e6f4256062
5 changed files with 37 additions and 32 deletions

View File

@ -1,23 +1,23 @@
package com.gildedrose;
import com.gildedrose.item.Item;
import com.gildedrose.item.ItemFactory;
import com.gildedrose.item.CustomisedItemFactory;
import com.gildedrose.item.QualityValues;
class GildedRose {
private static final int LOWEST_QUALITY_VALUE_POSSIBLE = 0;
private final ItemFactory itemFactory;
private final CustomisedItemFactory customisedItemFactory;
Item[] items;
public GildedRose(Item[] items) {
this.itemFactory = new ItemFactory();
this.customisedItemFactory = new CustomisedItemFactory();
this.items = items;
}
public void updateQuality() {
for (Item item : items) {
itemFactory.customiseItem(item).updateState();
customisedItemFactory.customiseItem(item).updateState();
if (hasReachedLowestQualityValue(item)) {
item.quality = LOWEST_QUALITY_VALUE_POSSIBLE;
} else if (hasReachedHighestQualityValue(item)) {

View File

@ -0,0 +1,20 @@
package com.gildedrose.item;
public class CustomisedItemFactory {
public final static String SULFURAS = "Sulfuras, Hand of Ragnaros";
public final static String BRIE = "Aged Brie";
public final static String BACKSTAGE_PASSES_ITEM = "Backstage passes to a TAFKAL80ETC concert";
public CustomisedItem customiseItem(Item item) {
if (item.name.equals(SULFURAS)) {
return new Sulfuras(item);
} else if (item.name.equals(BRIE)) {
return new AgedBrie(item);
} else if (item.name.equals(BACKSTAGE_PASSES_ITEM)) {
return new BackstagePassesItem(item);
} else {
return new StandardItem(item);
}
}
}

View File

@ -1,16 +0,0 @@
package com.gildedrose.item;
public class ItemFactory {
public CustomisedItem customiseItem(Item item) {
if (item.name.equals("Sulfuras, Hand of Ragnaros")) {
return new Sulfuras(item);
} else if (item.name.equals("Aged Brie")) {
return new AgedBrie(item);
} else if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
return new BackstagePassesItem(item);
} else {
return new StandardItem(item);
}
}
}

View File

@ -3,7 +3,7 @@ package com.gildedrose.item;
public final class QualityValues {
public static int highestValuePossible(Item item) {
if (item.name.equals("Sulfuras, Hand of Ragnaros")) {
if (item.name.equals(CustomisedItemFactory.SULFURAS)) {
return 80;
}
return 50;

View File

@ -3,6 +3,7 @@ package com.gildedrose;
import static org.junit.Assert.*;
import com.gildedrose.item.Item;
import com.gildedrose.item.CustomisedItemFactory;
import org.junit.Test;
public class GildedRoseTest {
@ -27,7 +28,7 @@ public class GildedRoseTest {
@Test
public void brieDecreasesSellByDayNumberEachTime() {
GildedRose app = newGildedRose("Aged Brie", 0, 0);
GildedRose app = newGildedRose(CustomisedItemFactory.BRIE, 0, 0);
app.updateQuality();
@ -36,7 +37,7 @@ public class GildedRoseTest {
@Test
public void backstagePassesItemDecreasesSellByDayNumberEachTime() {
GildedRose app = newGildedRose("Backstage passes to a TAFKAL80ETC concert", 0, 0);
GildedRose app = newGildedRose(CustomisedItemFactory.BACKSTAGE_PASSES_ITEM, 0, 0);
app.updateQuality();
@ -45,7 +46,7 @@ public class GildedRoseTest {
@Test
public void brieIncreasesInQualityEachTime() {
GildedRose app = newGildedRose("Aged Brie", 1, 1);
GildedRose app = newGildedRose(CustomisedItemFactory.BRIE, 1, 1);
app.updateQuality();
@ -54,7 +55,7 @@ public class GildedRoseTest {
@Test
public void brieQualityCannotGoAboveFiftyWhenIncreasing() {
GildedRose app = newGildedRose("Aged Brie", 1, 49);
GildedRose app = newGildedRose(CustomisedItemFactory.BRIE, 1, 49);
app.updateQuality();
app.updateQuality();
@ -64,7 +65,7 @@ public class GildedRoseTest {
@Test
public void backstagePassesItemDecreasesQualityByOneIfSellByDayMoreThanEleven() {
GildedRose app = newGildedRose("Backstage passes to a TAFKAL80ETC concert", 12, 1);
GildedRose app = newGildedRose(CustomisedItemFactory.BACKSTAGE_PASSES_ITEM, 12, 1);
app.updateQuality();
@ -73,7 +74,7 @@ public class GildedRoseTest {
@Test
public void backstagePassesItemDecreasesQualityByTwoIfSellByDayIsMoreThanSix() {
GildedRose app = newGildedRose("Backstage passes to a TAFKAL80ETC concert", 10, 1);
GildedRose app = newGildedRose(CustomisedItemFactory.BACKSTAGE_PASSES_ITEM, 10, 1);
app.updateQuality();
@ -82,7 +83,7 @@ public class GildedRoseTest {
@Test
public void backstagePassesItemDecreasesQualityByThreeIfSellByDayIsMoreThanZero() {
GildedRose app = newGildedRose("Backstage passes to a TAFKAL80ETC concert", 5, 1);
GildedRose app = newGildedRose(CustomisedItemFactory.BACKSTAGE_PASSES_ITEM, 5, 1);
app.updateQuality();
@ -91,7 +92,7 @@ public class GildedRoseTest {
@Test
public void backstagePassesItemQualityDropsToZeroIfSellByDayIsZeroOrLess() {
GildedRose app = newGildedRose("Backstage passes to a TAFKAL80ETC concert", 0,50);
GildedRose app = newGildedRose(CustomisedItemFactory.BACKSTAGE_PASSES_ITEM, 0,50);
app.updateQuality();
@ -100,7 +101,7 @@ public class GildedRoseTest {
@Test
public void backstagePassesItemQualityCannotGoAboveFiftyWhenIncreasing() {
GildedRose app = newGildedRose("Backstage passes to a TAFKAL80ETC concert", 5, 50);
GildedRose app = newGildedRose(CustomisedItemFactory.BACKSTAGE_PASSES_ITEM, 5, 50);
app.updateQuality();
@ -136,14 +137,14 @@ public class GildedRoseTest {
@Test
public void sulfurasHasQualityEighty() {
GildedRose app = newGildedRose("Sulfuras, Hand of Ragnaros", 1, 80);
GildedRose app = newGildedRose(CustomisedItemFactory.SULFURAS, 1, 80);
assertEquals(80, itemQualityValue(app));
}
@Test
public void sulfurasItemDoesNotAlterValues() {
GildedRose app = newGildedRose("Sulfuras, Hand of Ragnaros", 1, 80);
GildedRose app = newGildedRose(CustomisedItemFactory.SULFURAS, 1, 80);
app.updateQuality();