mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Refactroring completed: 4 methods extracted to update each item indivdaully. Additionally, unit test added to handle most and edge cases
This commit is contained in:
parent
100fb3620a
commit
baaada9449
@ -1,62 +1,103 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
|
|
||||||
|
private final String AGED_BRIE = "Aged Brie";
|
||||||
|
private final String BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
|
private 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 updateAgedBrie(Item item) {
|
||||||
|
|
||||||
|
// quality for any item can not exceed 50
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1; // age brie quality update
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
// update sell in date
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
// quality for age brie gets better as it ages past its sellIn
|
||||||
|
if (item.sellIn < 0 && item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1; // age brie quality update
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateBackStage(Item item) {
|
||||||
|
// no items quality gets above 50
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1; // backstage quality update
|
||||||
|
|
||||||
|
// quality increases twice if 10 days to sellIn
|
||||||
|
if (item.sellIn < 11 && item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
// quality increases by three if 5 days to sellIn
|
||||||
|
if (item.sellIn < 6 && item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// update sell in date for Backstage item
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
// quality drops to zero if it's past concert date
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
item.quality = item.quality - item.quality;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateSulfuras(Item item) {
|
||||||
|
// Sulfuras is a legendary item
|
||||||
|
// nothing happens for the sulfuras Item
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateNormalItem(Item item) {
|
||||||
|
|
||||||
|
// normal item quality decreases by 1 every day
|
||||||
|
if (item.quality > 0) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// update sell in date for Normal item
|
||||||
|
item.sellIn = item.sellIn - 1;
|
||||||
|
|
||||||
|
// quality drops twice if item is past sellIn Date
|
||||||
|
if (item.sellIn < 0 && item.quality > 0) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
for (int i = 0; i < items.length; i++) {
|
for (Item item : this.items) {
|
||||||
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")) {
|
// only sulfuras don't sell
|
||||||
if (items[i].sellIn < 11) {
|
// hence update sellin value for all items except sulfuras here
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1;
|
if (item.name != SULFURAS) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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")) {
|
switch (item.name) {
|
||||||
items[i].sellIn = items[i].sellIn - 1;
|
case AGED_BRIE:
|
||||||
}
|
updateAgedBrie(item);
|
||||||
|
continue;
|
||||||
|
|
||||||
if (items[i].sellIn < 0) {
|
case BACKSTAGE_PASS:
|
||||||
if (!items[i].name.equals("Aged Brie")) {
|
updateBackStage(item);
|
||||||
if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
continue;
|
||||||
if (items[i].quality > 0) {
|
case SULFURAS:
|
||||||
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
|
updateSulfuras(item);
|
||||||
items[i].quality = items[i].quality - 1;
|
continue;
|
||||||
}
|
default:
|
||||||
}
|
updateNormalItem(item);
|
||||||
} else {
|
continue;
|
||||||
items[i].quality = items[i].quality - items[i].quality;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (items[i].quality < 50) {
|
|
||||||
items[i].quality = items[i].quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,17 +1,158 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class GildedRoseTest {
|
class GildedRoseTest {
|
||||||
|
private final String AGED_BRIE = "Aged Brie";
|
||||||
|
private final String BACKSTAGE = "Backstage passes to a TAFKAL80ETC concert";
|
||||||
|
private final String SULFURAS = "Sulfuras, Hand of Ragnaros";
|
||||||
|
private final String CAKE = "Conjured Mana Cake";
|
||||||
|
private final int BACKSTAGE_QUALITY_INCREASE_10_DAYS = 2;
|
||||||
|
private final int BACKSTAGE_QUALITY_INCREASE_5_DAYS = 3;
|
||||||
|
private GildedRose app;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
this.app = new GildedRose(new Item[]{});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
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);
|
this.app.items = items;
|
||||||
app.updateQuality();
|
this.app.updateQuality();
|
||||||
assertEquals("fixme", app.items[0].name);
|
assertEquals("foo", app.items[0].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testNoNegativeQuality() {
|
||||||
|
Item[] items = new Item[]{
|
||||||
|
new Item(AGED_BRIE, 0, 0),
|
||||||
|
new Item(BACKSTAGE, 0, 0),
|
||||||
|
new Item(SULFURAS, 0, 0),
|
||||||
|
new Item(CAKE, 0, 0)
|
||||||
|
};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
this.app.updateQuality();
|
||||||
|
this.app.updateQuality();
|
||||||
|
|
||||||
|
// assert the quality is not negative
|
||||||
|
for (Item item :
|
||||||
|
this.app.items) {
|
||||||
|
assertTrue(item.quality >= 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAgedBrieQualityIncrease() {
|
||||||
|
Item[] items = new Item[]{new Item(AGED_BRIE, 0, 10)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
this.app.updateQuality();
|
||||||
|
this.app.updateQuality();
|
||||||
|
assertTrue(this.app.items[0].quality > 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testQualityNeverMoreThan50() {
|
||||||
|
Item[] items = new Item[]{new Item(AGED_BRIE, 0, 50)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
this.app.updateQuality();
|
||||||
|
this.app.updateQuality();
|
||||||
|
assertTrue(this.app.items[0].quality <= 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSulfurasQualityNeverChange() {
|
||||||
|
Item[] items = new Item[]{new Item(SULFURAS, 2, 10)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
this.app.updateQuality();
|
||||||
|
this.app.updateQuality();
|
||||||
|
this.app.updateQuality();
|
||||||
|
assertEquals(10,this.app.items[0].quality);
|
||||||
|
assertEquals(2,this.app.items[0].sellIn );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBackstagePassQuality10DaysBeforeSellIn() {
|
||||||
|
Item[] items = new Item[]{new Item(BACKSTAGE, 9, 10)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
int noOfDays = 3;
|
||||||
|
for (int day = 0; day < noOfDays; day++) {
|
||||||
|
this.app.updateQuality();
|
||||||
|
}
|
||||||
|
int expectedQuality = 10 + BACKSTAGE_QUALITY_INCREASE_10_DAYS * noOfDays;
|
||||||
|
assertEquals(expectedQuality, this.app.items[0].quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBackstagePassQuality5DaysBeforeSellIn() {
|
||||||
|
Item[] items = new Item[]{new Item(BACKSTAGE, 5, 10)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
int noOfDays = 3;
|
||||||
|
for (int day = 0; day < noOfDays; day++) {
|
||||||
|
this.app.updateQuality();
|
||||||
|
}
|
||||||
|
int expectedQuality = 10 + BACKSTAGE_QUALITY_INCREASE_5_DAYS * noOfDays;
|
||||||
|
assertEquals(expectedQuality, this.app.items[0].quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBackstagePassQuality6DaysBeforeSellIn() {
|
||||||
|
Item[] items = new Item[]{new Item(BACKSTAGE, 6, 10)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
this.app.updateQuality();
|
||||||
|
int expectedQuality = 12;
|
||||||
|
assertEquals(expectedQuality, this.app.items[0].quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBackstagePassQuality11DaysBeforeSellIn() {
|
||||||
|
Item[] items = new Item[]{new Item(BACKSTAGE, 11, 10)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
this.app.updateQuality();
|
||||||
|
int expectedQuality = 11;
|
||||||
|
assertEquals(expectedQuality, this.app.items[0].quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBackstagePassQualityAfterSellIn() {
|
||||||
|
Item[] items = new Item[]{new Item(BACKSTAGE, 1, 10)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
int noOfDays = 2;
|
||||||
|
for (int day = 0; day < noOfDays; day++) {
|
||||||
|
this.app.updateQuality();
|
||||||
|
}
|
||||||
|
int expectedQuality = 0;
|
||||||
|
assertEquals(expectedQuality, this.app.items[0].quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testNormalItemQualityDegradeTwiceAsFast() {
|
||||||
|
Item[] items = new Item[]{new Item(CAKE, 0, 10)};
|
||||||
|
this.app.items = items;
|
||||||
|
// update quality two times
|
||||||
|
int noOfDays = 2;
|
||||||
|
for (int day = 0; day < noOfDays; day++) {
|
||||||
|
this.app.updateQuality();
|
||||||
|
}
|
||||||
|
int expectedQuality = 6;
|
||||||
|
assertEquals(expectedQuality, this.app.items[0].quality);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ public class TexttestFixture {
|
|||||||
|
|
||||||
GildedRose app = new GildedRose(items);
|
GildedRose app = new GildedRose(items);
|
||||||
|
|
||||||
int days = 2;
|
int days = 30;
|
||||||
if (args.length > 0) {
|
if (args.length > 0) {
|
||||||
days = Integer.parseInt(args[0]) + 1;
|
days = Integer.parseInt(args[0]) + 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user