mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
Code Refactoring :Separated expired item quality check and common logic into its own method for clearer code flow
This commit is contained in:
parent
8fb53ac4d3
commit
816efb008e
@ -22,15 +22,11 @@ class GildedRose {
|
|||||||
|
|
||||||
if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||||
if (item.sellIn < 11) {
|
if (item.sellIn < 11) {
|
||||||
if (item.quality < 50) {
|
addQualityWhenWithInLimit(item);
|
||||||
item.quality = item.quality + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.sellIn < 6) {
|
if (item.sellIn < 6) {
|
||||||
if (item.quality < 50) {
|
addQualityWhenWithInLimit(item);
|
||||||
item.quality = item.quality + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,25 +36,33 @@ class GildedRose {
|
|||||||
updateSellInDays(item);
|
updateSellInDays(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.sellIn < 0) {
|
updateQualityForExpiredItem(item);
|
||||||
if (!item.name.equals("Aged Brie")) {
|
|
||||||
if (!item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
|
||||||
if (item.quality > 0) {
|
|
||||||
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
|
|
||||||
item.quality = item.quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
item.quality = item.quality - item.quality;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (item.quality < 50) {
|
|
||||||
item.quality = item.quality + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addQualityWhenWithInLimit(Item item) {
|
||||||
|
if (item.quality < 50) {
|
||||||
|
item.quality = item.quality + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateQualityForExpiredItem(Item item) {
|
||||||
|
if (item.sellIn < 0) {
|
||||||
|
if (!item.name.equals("Aged Brie")) {
|
||||||
|
if (!item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
|
||||||
|
if (item.quality > 0) {
|
||||||
|
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
|
||||||
|
item.quality = item.quality - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
item.quality = item.quality - item.quality;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
addQualityWhenWithInLimit(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void updateSellInDays(Item item) {
|
private void updateSellInDays(Item item) {
|
||||||
|
|||||||
@ -1,24 +1,57 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
class GildedRoseTest {
|
class GildedRoseTest {
|
||||||
@Test
|
|
||||||
void testUpdateSellInDaysForAgedBrie() {
|
private static Stream<Arguments> getTestItemsForSellInDayCheck() {
|
||||||
Item[] items = new Item[] { new Item("Aged Brie", 1, 1) };
|
return Stream.of(
|
||||||
GildedRose app = new GildedRose(items);
|
Arguments.of(new Item[] { new Item("Aged Brie", 1, 1) }, 0),
|
||||||
app.updateQuality();
|
Arguments.of(new Item[] { new Item("Sulfuras, Hand of Ragnaros", 1, 1) }, 1)
|
||||||
assertEquals(0, items[0].sellIn);
|
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@ParameterizedTest(name = "Test sellin days for items - {index}")
|
||||||
void testUpdateSellInDaysForSulfuras() {
|
@MethodSource("getTestItemsForSellInDayCheck")
|
||||||
Item[] items = new Item[] { new Item("Sulfuras, Hand of Ragnaros", 1, 1) };
|
void testUpdateSellInDays(Item[] items, int expectedSellinDays) {
|
||||||
GildedRose app = new GildedRose(items);
|
GildedRose app = new GildedRose(items);
|
||||||
app.updateQuality();
|
app.updateQuality();
|
||||||
assertEquals(1, items[0].sellIn);
|
assertEquals(expectedSellinDays, items[0].sellIn);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> getTestItemsForQualityCheck() {
|
||||||
|
return Stream.of(Arguments.of(new Item[] { new Item("Aged Brie", 1, 1) }, 2),
|
||||||
|
Arguments.of(new Item[] { new Item("Aged Brie", 1, 50) }, 50),
|
||||||
|
|
||||||
|
Arguments.of(new Item[] { new Item("Aged Brie", -1, 1) }, 3),
|
||||||
|
Arguments.of(new Item[] { new Item("Aged Brie", -1, 50) }, 50),
|
||||||
|
|
||||||
|
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 5, 10) }, 13),
|
||||||
|
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 5, 50) }, 50),
|
||||||
|
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 10, 10) }, 12),
|
||||||
|
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 10, 50) }, 50),
|
||||||
|
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 12, 10) }, 11),
|
||||||
|
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", 12, 50) }, 50),
|
||||||
|
|
||||||
|
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", -1, 10) }, 0)
|
||||||
|
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test quality for non expired and expired items - {index}")
|
||||||
|
@MethodSource("getTestItemsForQualityCheck")
|
||||||
|
void testUpdateQuality(Item[] items, int expectedQuality) {
|
||||||
|
GildedRose app = new GildedRose(items);
|
||||||
|
app.updateQuality();
|
||||||
|
assertEquals(expectedQuality, items[0].quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user