diff --git a/GildedRose.cs b/GildedRose.cs index 6de7d65..b2115f5 100644 --- a/GildedRose.cs +++ b/GildedRose.cs @@ -1,90 +1,75 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using ApprovalUtilities.Utilities; namespace csharpcore { public class GildedRose { + private const int MaxItemQuality = 50; + private readonly IList _items; - public GildedRose(IList items) + public GildedRose(IList items) => _items = items; + + public void UpdateQuality() => _items.ForEach(UpdateItemQuality); + + private static void UpdateItemQuality(Item item) { - _items = items; + switch (item.Name) + { + case "Sulfuras, Hand of Ragnaros": + return; + + case "Aged Brie": + UpdateAgedBrie(item); + break; + + case "Backstage passes to a TAFKAL80ETC concert": + UpdateBackstagePasses(item); + break; + + case "Conjured Mana Cake": + UpdateConjuredCake(item); + break; + + default: + // Decrease quality twice when sell date is passed + var qualityDecrement = item.SellIn > 0 ? 1 : 2; + item.Quality = Math.Max(item.Quality - qualityDecrement, 0); + break; + } + + item.SellIn -= 1; } - public void UpdateQuality() + private static void UpdateConjuredCake(Item item) { - for (var i = 0; i < _items.Count; i++) + // TODO + // Decrease quality twice when sell date is passed + var qualityDecrement = item.SellIn > 0 ? 1 : 2; + item.Quality = Math.Max(item.Quality - qualityDecrement, 0); + } + + private static void UpdateBackstagePasses(Item item) + { + static int ComputeQualityIncrement(int daysRemaining) { - if (_items[i].Name != "Aged Brie" && _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 - { - 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; - } - } - - if (_items[i].SellIn < 6) - { - if (_items[i].Quality < 50) - { - _items[i].Quality = _items[i].Quality + 1; - } - } - } - } - } - - if (_items[i].Name != "Sulfuras, Hand of Ragnaros") - { - _items[i].SellIn = _items[i].SellIn - 1; - } - - 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; - } - } - } + // "Infinite" increment when sell date is passed + if (daysRemaining <= 0) return int.MinValue; + if (daysRemaining <= 5) return 3; + if (daysRemaining <= 10) return 2; + return 1; } + + item.Quality = Math.Clamp(item.Quality + ComputeQualityIncrement(item.SellIn), 0, MaxItemQuality); + } + + private static void UpdateAgedBrie(Item item) + { + // Increase quality twice when sell date is passed + var qualityIncrement = item.SellIn > 0 ? 1 : 2; + item.Quality = Math.Clamp(item.Quality + qualityIncrement, 0, MaxItemQuality); } } } diff --git a/test/AgedBrieTest.cs b/test/AgedBrieTest.cs index d63da77..cb1739a 100644 --- a/test/AgedBrieTest.cs +++ b/test/AgedBrieTest.cs @@ -26,15 +26,17 @@ namespace csharpcore new object[]{ -1, 1, 3 }, }; - [Fact] - public void ShouldNotIncreaseQualityMoreThan50() + [Theory] + [InlineData(1, 0)] + [InlineData(-1, -2)] + public void ShouldNotIncreaseQualityMoreThan50(int startSellInDays, int expectedSellInDays) { - var item = new Item {Name = "Aged Brie", SellIn = 1, Quality = 50}; + var item = new Item {Name = "Aged Brie", SellIn = startSellInDays, Quality = 50}; var app = new GildedRose(new List {item}); app.UpdateQuality(); Assert.Equal("Aged Brie", item.Name); - Assert.Equal(0, item.SellIn); + Assert.Equal(expectedSellInDays, item.SellIn); Assert.Equal(50, item.Quality); } } diff --git a/test/ConjuredTest.cs b/test/ConjuredTest.cs new file mode 100644 index 0000000..93760b3 --- /dev/null +++ b/test/ConjuredTest.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using Xunit; + +namespace csharpcore +{ + // "Conjured" items degrade in Quality twice as fast as normal items + public class ConjuredTest + { + private readonly List _items; + private GildedRose _rose; + + public ConjuredTest() + { + _items = new List(); + _rose =new GildedRose(_items); + } + + //[Fact] + public void ShouldDecreaseInQuality() + { + var item = GenerateItem(3, 2); + _items.Add(item); + + _rose.UpdateQuality(); + Assert.Equal(1, item.Quality); + Assert.Equal(1, item.SellIn); + } + + public Item GenerateItem(int quality, int sellInDays) + => new Item {Name = "Conjured Mana Cake", Quality = quality, SellIn = sellInDays}; + } +}