Code Refactoring :Separated quality check based on item type to improve readability

This commit is contained in:
lekshmysasidhar 2023-03-31 10:29:01 +02:00
parent 816efb008e
commit e5625734e5
2 changed files with 21 additions and 9 deletions

View File

@ -13,7 +13,7 @@ class GildedRose {
&& !item.name.equals("Backstage passes to a TAFKAL80ETC concert")) { && !item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.quality > 0) { if (item.quality > 0) {
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) { if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
item.quality = item.quality - 1; updateQualityForNormalItem(item);
} }
} }
} else { } else {
@ -21,13 +21,7 @@ class GildedRose {
item.quality = item.quality + 1; item.quality = item.quality + 1;
if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) { if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.sellIn < 11) { updateQualityForBackstagePasses(item);
addQualityWhenWithInLimit(item);
}
if (item.sellIn < 6) {
addQualityWhenWithInLimit(item);
}
} }
} }
} }
@ -40,6 +34,20 @@ class GildedRose {
} }
} }
private void updateQualityForBackstagePasses(Item item) {
if (item.sellIn < 11) {
addQualityWhenWithInLimit(item);
}
if (item.sellIn < 6) {
addQualityWhenWithInLimit(item);
}
}
private void updateQualityForNormalItem(Item item) {
item.quality--;
}
private void addQualityWhenWithInLimit(Item item) { private void addQualityWhenWithInLimit(Item item) {
if (item.quality < 50) { if (item.quality < 50) {
item.quality = item.quality + 1; item.quality = item.quality + 1;

View File

@ -41,7 +41,11 @@ class GildedRoseTest {
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, 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", 12, 50) }, 50),
Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", -1, 10) }, 0) Arguments.of(new Item[] { new Item("Backstage passes to a TAFKAL80ETC concert", -1, 10) }, 0),
Arguments.of(new Item[] { new Item("Coffee Day", 1, 1) }, 0),
Arguments.of(new Item[] { new Item("Coffee Day", -1, 1) }, 0),
Arguments.of(new Item[] { new Item("Coffee Day", -1, 2) }, 0)
); );
} }