mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Test cases for "Conjured" items and code refactored
This commit is contained in:
parent
263a07d58d
commit
03b3d1d901
@ -8,55 +8,71 @@ class GildedRose {
|
||||
}
|
||||
|
||||
public void updateQuality() {
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
if (!items[i].name.equals("Aged Brie")
|
||||
&& !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||
if (items[i].quality > 0) {
|
||||
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||
items[i].quality = items[i].quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (items[i].quality < 50) {
|
||||
items[i].quality = items[i].quality + 1;
|
||||
|
||||
if (items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||
if (items[i].sellIn < 11) {
|
||||
if (items[i].quality < 50) {
|
||||
items[i].quality = items[i].quality + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (items[i].sellIn < 6) {
|
||||
if (items[i].quality < 50) {
|
||||
items[i].quality = items[i].quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||
items[i].sellIn = items[i].sellIn - 1;
|
||||
}
|
||||
|
||||
if (items[i].sellIn < 0) {
|
||||
if (!items[i].name.equals("Aged Brie")) {
|
||||
if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||
if (items[i].quality > 0) {
|
||||
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||
items[i].quality = items[i].quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
items[i].quality = items[i].quality - items[i].quality;
|
||||
}
|
||||
} else {
|
||||
if (items[i].quality < 50) {
|
||||
items[i].quality = items[i].quality + 1;
|
||||
}
|
||||
}
|
||||
for (Item item : items) {
|
||||
String name = item.name;
|
||||
updateSellIn(item, name);
|
||||
switch (name) {
|
||||
case "Sulfuras, Hand of Ragnaros":
|
||||
break;
|
||||
case "Aged Brie":
|
||||
increaseQualityByOne(item);
|
||||
break;
|
||||
case "Backstage passes to a TAFKAL80ETC concert":
|
||||
updateBackstagePassesQuality(item);
|
||||
break;
|
||||
case "Conjured Mana Cake":
|
||||
updateConjuredQuality(item);
|
||||
break;
|
||||
default:
|
||||
updateQualityOfDefaultItems(item);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateQualityOfDefaultItems(Item item) {
|
||||
if (item.sellIn >= 0)
|
||||
decreaseQualityByOne(item);
|
||||
else {
|
||||
decreaseQualityByOne(item);
|
||||
decreaseQualityByOne(item);
|
||||
}
|
||||
}
|
||||
|
||||
private void decreaseQualityByOne(Item item) {
|
||||
if (item.quality > 0)
|
||||
item.quality--;
|
||||
}
|
||||
|
||||
private void updateSellIn(Item item, String name) {
|
||||
if (!name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||
item.sellIn = item.sellIn - 1;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateConjuredQuality(Item item) {
|
||||
decreaseQualityByOne(item);
|
||||
decreaseQualityByOne(item);
|
||||
}
|
||||
|
||||
private void increaseQualityByOne(Item item) {
|
||||
if (isValueLessThanBy(item.quality, 50)) {
|
||||
item.quality++;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValueLessThanBy(int value, int target) {
|
||||
return value < target;
|
||||
}
|
||||
|
||||
/*Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less but
|
||||
Quality drops to 0 after the concert*/
|
||||
private void updateBackstagePassesQuality(Item item) {
|
||||
if (isValueLessThanBy(item.sellIn, 11)) {
|
||||
increaseQualityByOne(item);
|
||||
increaseQualityByOne(item);
|
||||
}
|
||||
if (isValueLessThanBy(item.sellIn, 6) || item.sellIn > 10)
|
||||
increaseQualityByOne(item);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,10 +8,34 @@ class GildedRoseTest {
|
||||
|
||||
@Test
|
||||
void foo() {
|
||||
Item[] items = new Item[] { new Item("foo", 0, 0) };
|
||||
Item[] items = new Item[] { new Item("fixme", 0, 0) };
|
||||
GildedRose app = new GildedRose(items);
|
||||
app.updateQuality();
|
||||
assertEquals("fixme", app.items[0].name);
|
||||
}
|
||||
|
||||
@Test
|
||||
void agedBrieTest() {
|
||||
Item[] items = new Item[] { new Item("Aged Brie", 2, 0) };
|
||||
GildedRose app = new GildedRose(items);
|
||||
app.updateQuality();
|
||||
assertEquals("Aged Brie", app.items[0].name);
|
||||
assertEquals(1, app.items[0].quality);
|
||||
assertEquals(1, app.items[0].sellIn);
|
||||
app.updateQuality();
|
||||
assertEquals(2, app.items[0].quality);
|
||||
assertEquals(0, app.items[0].sellIn);
|
||||
}
|
||||
@Test
|
||||
void conjuredTest() {
|
||||
Item[] items = new Item[] { new Item("Conjured Mana Cake", 3, 6) };
|
||||
GildedRose app = new GildedRose(items);
|
||||
app.updateQuality();
|
||||
assertEquals("Conjured Mana Cake", app.items[0].name);
|
||||
assertEquals(4, app.items[0].quality);
|
||||
assertEquals(2, app.items[0].sellIn);
|
||||
app.updateQuality();
|
||||
assertEquals(2, app.items[0].quality);
|
||||
assertEquals(1, app.items[0].sellIn);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user