From 4a424925366f1b158a4aff8d5b85a60f7c915d81 Mon Sep 17 00:00:00 2001 From: agentic-dev agent Date: Mon, 2 Nov 2026 15:57:47 +0000 Subject: [PATCH] Refactor UpdateQualityService.UpdateQuality into per-item-kind methods Replace nested ifs with a switch on item.Name dispatching to UpdateNormalItem/AgedBrie/Sulfuras/BackstagePasses, with IncreaseQuality/DecreaseQuality helpers and const names. Behaviour unchanged. --- src/Core/OriginalCode/UpdateQualityService.cs | 146 ++++++++++-------- 1 file changed, 82 insertions(+), 64 deletions(-) diff --git a/src/Core/OriginalCode/UpdateQualityService.cs b/src/Core/OriginalCode/UpdateQualityService.cs index eb9eb97..16b17e5 100644 --- a/src/Core/OriginalCode/UpdateQualityService.cs +++ b/src/Core/OriginalCode/UpdateQualityService.cs @@ -9,79 +9,97 @@ namespace GildedRoseKata.OriginalCode; public class UpdateQualityService(IList items) { #pragma warning disable + private const string AgedBrieName = "Aged Brie"; + private const string SulfurasName = "Sulfuras, Hand of Ragnaros"; + private const string BackstagePassesName = "Backstage passes to a TAFKAL80ETC concert"; + private const int MaxQuality = 50; + private const int MinQuality = 0; + public void UpdateQuality() { - for (var i = 0; i < items.Count; i++) + foreach (var item in items) { - if (items[i].Name != "Aged Brie" && items[i].Name != "Backstage passes to a TAFKAL80ETC concert") + switch (item.Name) { - if (items[i].Quality > 0) - { - if (items[i].Name != "Sulfuras, Hand of Ragnaros") - { - items[i].Quality = items[i].Quality - 1; - } - } + case AgedBrieName: + UpdateAgedBrie(item); + break; + case SulfurasName: + UpdateSulfuras(item); + break; + case BackstagePassesName: + UpdateBackstagePasses(item); + break; + default: + UpdateNormalItem(item); + break; } - else - { - if (items[i].Quality < 50) - { - items[i].Quality = items[i].Quality + 1; + } + } - if (items[i].Name == "Backstage passes to a TAFKAL80ETC concert") - { - if (items[i].SellIn < 11) - { - if (items[i].Quality < 50) - { - items[i].Quality = items[i].Quality + 1; - } - } + private static void UpdateNormalItem(Item item) + { + DecreaseQuality(item); + item.SellIn--; - if (items[i].SellIn < 6) - { - if (items[i].Quality < 50) - { - items[i].Quality = items[i].Quality + 1; - } - } - } - } - } + if (item.SellIn < 0) + { + DecreaseQuality(item); + } + } - if (items[i].Name != "Sulfuras, Hand of Ragnaros") - { - items[i].SellIn = items[i].SellIn - 1; - } + private static void UpdateAgedBrie(Item item) + { + IncreaseQuality(item); + item.SellIn--; - if (items[i].SellIn < 0) - { - if (items[i].Name != "Aged Brie") - { - if (items[i].Name != "Backstage passes to a TAFKAL80ETC concert") - { - if (items[i].Quality > 0) - { - if (items[i].Name != "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; - } - } - } + if (item.SellIn < 0) + { + IncreaseQuality(item); + } + } + + private static void UpdateSulfuras(Item item) + { + // Sulfuras never has to be sold and never decreases in quality: nothing changes. + } + + private static void UpdateBackstagePasses(Item item) + { + // The tier checks use SellIn as it was before today's decrement. + IncreaseQuality(item); + + if (item.SellIn < 11) + { + IncreaseQuality(item); + } + + if (item.SellIn < 6) + { + IncreaseQuality(item); + } + + item.SellIn--; + + if (item.SellIn < 0) + { + item.Quality = 0; + } + } + + private static void IncreaseQuality(Item item) + { + if (item.Quality < MaxQuality) + { + item.Quality++; + } + } + + private static void DecreaseQuality(Item item) + { + if (item.Quality > MinQuality) + { + item.Quality--; } } #pragma warning restore