From 58205b574097443e0bd634438065ea791393967b Mon Sep 17 00:00:00 2001 From: Kadir Sirimsi Date: Mon, 10 Feb 2025 13:02:53 +0100 Subject: [PATCH] refactor: improved readability and extract mutations --- .../main/java/com/gildedrose/GildedRose.java | 73 ++++++++++--------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 9b6d4307..e17f2733 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -9,54 +9,61 @@ class GildedRose { public void updateQuality() { for (Item item : items) { - if (!item.getName().equals("Aged Brie") - && !item.getName().equals("Backstage passes to a TAFKAL80ETC concert")) { - if (item.quality > 0) { - if (!item.getName().equals("Sulfuras, Hand of Ragnaros")) { - item.quality = item.quality - 1; - } + boolean isAgedBrie = item.getName().equals("Aged Brie"); + boolean isSulfuras = item.getName().equals("Sulfuras, Hand of Ragnaros"); + boolean passesToTafkalConcert = item.getName().equals("Backstage passes to a TAFKAL80ETC concert"); + + if (!isAgedBrie && !passesToTafkalConcert) { + if (item.quality > 0 && !isSulfuras) { + deductOneFromQuality(item); } - } else { - if (item.quality < 50) { - item.quality = item.quality + 1; + } else if(item.quality < 50) { + addOneToQuality(item); - if (item.getName().equals("Backstage passes to a TAFKAL80ETC concert")) { - if (item.sellIn < 11) { - if (item.quality < 50) { - item.quality = item.quality + 1; - } - } + if (passesToTafkalConcert) { + if (item.sellIn < 11 && item.quality < 50) { + addOneToQuality(item); + } - if (item.sellIn < 6) { - if (item.quality < 50) { - item.quality = item.quality + 1; - } - } + if (item.sellIn < 6 && item.quality < 50) { + addOneToQuality(item); } } } - if (!item.getName().equals("Sulfuras, Hand of Ragnaros")) { - item.sellIn = item.sellIn - 1; + if (!isSulfuras) { + deductSellIn(item); } if (item.sellIn < 0) { - if (!item.getName().equals("Aged Brie")) { - if (!item.getName().equals("Backstage passes to a TAFKAL80ETC concert")) { - if (item.quality > 0) { - if (!item.getName().equals("Sulfuras, Hand of Ragnaros")) { - item.quality = item.quality - 1; - } + if (!isAgedBrie) { + if (!passesToTafkalConcert) { + if (item.quality > 0 && !isSulfuras) { + deductOneFromQuality(item); } } else { - item.quality = 0; - } - } else { - if (item.quality < 50) { - item.quality = item.quality + 1; + setQualityToZero(item); } + } else if ((item.quality < 50)) { + addOneToQuality(item); } } } } + + public void setQualityToZero(Item item) { + item.quality = 0; + } + + public void deductSellIn(Item item) { + item.sellIn--; + } + + public void addOneToQuality(Item item) { + item.quality++; + } + + public void deductOneFromQuality(Item item) { + item.quality--; + } }