From 9202855a5d5b5e09868f9442ae93d1981b4e0b6a Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Tue, 27 Apr 2021 23:54:10 +0200 Subject: [PATCH] . . --- csharp/GildedRose.cs | 144 ++++++++++-------- csharp/Program.cs | 2 +- csharpTests/GildedRoseUnitTests.cs | 68 ++++++++- ...arpTests.csproj => csharpUnitTests.csproj} | 0 4 files changed, 144 insertions(+), 70 deletions(-) rename csharpTests/{csharpTests.csproj => csharpUnitTests.csproj} (100%) diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index ee63454b..4e2b6ee6 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -1,8 +1,17 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace csharp { - public class GildedRose + interface IUpdateMethods + { + void CalculateQuality(Item item, int decrementValue, int incrementValue); + void AgedBrie(Item item); + void MinMaxRules(Item item); + void ReduceSellIn(Item item); + } + + public class GildedRose : IUpdateMethods { IList Items; public GildedRose(IList Items) @@ -10,80 +19,89 @@ namespace csharp this.Items = Items; } + enum Limit + { + RegularMin = 0, + RegularMax = 50, + LegendaryMax = 80 + } + public void UpdateQuality() { foreach(var Item in Items) // Changed this to a simpler loop { - if (Item.Name != "Aged Brie" && Item.Name != "Backstage passes to a TAFKAL80ETC concert") + //Console.WriteLine(Item.Name); + if(Item.Name == "Aged Brie") { - if (Item.Quality > 0) - { - if (Item.Name != "Sulfuras, Hand of Ragnaros") - { - Item.Quality = Item.Quality - 1; - } - } + AgedBrie(Item); + MinMaxRules(Item); + //return; } - else + else if (Item.Name == "Sulfuras, Hand of Ragnaros") { - if (Item.Quality < 50) - { - Item.Quality = Item.Quality + 1; - - if (Item.Name == "Backstage passes to a TAFKAL80ETC concert") - { - if (Item.SellIn < 11) - { - if (Item.Quality < 50) - { - Item.Quality = Item.Quality + 1; - } - } - - if (Item.SellIn < 6) - { - if (Item.Quality < 50) - { - Item.Quality = Item.Quality + 1; - } - } - } - } + //CalculateQuality(Item, 0, 0); + //return; } - - if (Item.Name != "Sulfuras, Hand of Ragnaros") + else if(Item.Name == "Conjured Mana Cake") { - Item.SellIn = Item.SellIn - 1; + CalculateQuality(Item, 4, 0); + //return; } - - if (Item.SellIn < 0) + else { - if (Item.Name != "Aged Brie") - { - if (Item.Name != "Backstage passes to a TAFKAL80ETC concert") - { - if (Item.Quality > 0) - { - if (Item.Name != "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; - } - } + CalculateQuality(Item, 2, 0); + //return; } } } + + public void CalculateQuality(Item item, int decrementValue, int IncrementValue) + { + if (item.SellIn <= 50) + { + item.Quality = item.Quality - decrementValue; + } + MinMaxRules(item); + ReduceSellIn(item); + } + public void AgedBrie(Item item) + { + if (item.SellIn == 0) + { + item.Quality = 0; + } + + if (item.SellIn <= 10 && item.SellIn > 5 && item.SellIn > 0) + { + item.Quality = item.Quality + 2; + } + + if (item.SellIn <= 5 && item.SellIn > 0) + { + item.Quality = item.Quality + 3; + } + ReduceSellIn(item); + } + + public void MinMaxRules(Item item) + { + if(item.Quality <= (int)Limit.RegularMin) + { + item.Quality = (int)Limit.RegularMin; + } + if(item.Quality >= (int)Limit.RegularMax) + { + item.Quality = (int)Limit.RegularMax; + } + } + + public void ReduceSellIn(Item item) + { + if (item.SellIn >= 1) + { + item.SellIn = item.SellIn - 1; + } + } + } } diff --git a/csharp/Program.cs b/csharp/Program.cs index d92970a4..70d06322 100644 --- a/csharp/Program.cs +++ b/csharp/Program.cs @@ -40,7 +40,7 @@ namespace csharp var app = new GildedRose(Items); - for (var i = 0; i < 31; i++) + for (var i = 0; i < 11; i++) { Console.WriteLine("-------- day " + i + " --------"); Console.WriteLine("name, sellIn, quality"); diff --git a/csharpTests/GildedRoseUnitTests.cs b/csharpTests/GildedRoseUnitTests.cs index 4b628c3d..6a7c0184 100644 --- a/csharpTests/GildedRoseUnitTests.cs +++ b/csharpTests/GildedRoseUnitTests.cs @@ -24,9 +24,9 @@ namespace csharp.Tests } [TestMethod()] - public void UpdateQualityTestForAgedBrie_SellDatePassed_IncreaseQualityByTwo() + public void UpdateQualityTestForAgedBrie_SellDateIsLessThan10ButGreaterThan5_IncreaseQualityByTwo() { - IList Items = new List { new Item { Name = "Aged Brie", SellIn = 0, Quality = 0 } }; + IList Items = new List { new Item { Name = "Aged Brie", SellIn = 10, Quality = 0 } }; GildedRose app = new GildedRose(Items); app.UpdateQuality(); @@ -34,6 +34,28 @@ namespace csharp.Tests Assert.AreEqual(2, Items[0].Quality); } + [TestMethod()] + public void UpdateQualityTestForAgedBrie_SellDateIsLessThan5_IncreaseQualityByThree() + { + IList Items = new List { new Item { Name = "Aged Brie", SellIn = 5, Quality = 0 } }; + GildedRose app = new GildedRose(Items); + + app.UpdateQuality(); + + Assert.AreEqual(3, Items[0].Quality); + } + + [TestMethod()] + public void UpdateQualityTestForAgedBrie_AfterConcert_DropToZero() + { + IList Items = new List { new Item { Name = "Aged Brie", SellIn = 0, Quality = 10 }}; + GildedRose app = new GildedRose(Items); + + app.UpdateQuality(); + + Assert.AreEqual(0, Items[0].Quality); + } + [TestMethod()] public void UpdateQualityTest_SellDatePassed_DecreaseQualityTwice() { @@ -46,7 +68,7 @@ namespace csharp.Tests } [TestMethod()] - public void UpdateQualityTest_QualityOfItemIsNeverNegative_MinimumValue() + public void UpdateQualityTest_QualityOfItemIsNeverNegative_MinimumValue0() { IList Items = new List { new Item { Name = "+5 Dexterity Vest", SellIn = 0, Quality = 0 } }; GildedRose app = new GildedRose(Items); @@ -56,14 +78,48 @@ namespace csharp.Tests Assert.AreEqual(0, Items[0].Quality); } - public void UpdateQualityTest_QualityOfItemNeverAboveDefinedValue_MaximumValue() + [TestMethod()] + public void UpdateQualityTest_QualityOfItemNeverAboveDefinedValue_MaximumValue50() { - IList Items = new List { new Item { Name = "+5 Dexterity Vest", SellIn = 0, Quality = 0 } }; + IList Items = new List { new Item { Name = "Aged Brie", SellIn = 1, Quality = 49 } }; GildedRose app = new GildedRose(Items); app.UpdateQuality(); - Assert.AreEqual(0, Items[0].Quality); + Assert.AreEqual(50, Items[0].Quality); + } + + [TestMethod()] + public void UpdateQualityTest_LegendaryItems_NeverAltar() + { + IList Items = new List { new Item { Name = "Sulfuras, Hand of Ragnaros", SellIn = 0, Quality = 80 } }; + GildedRose app = new GildedRose(Items); + + app.UpdateQuality(); + + Assert.AreEqual(80, Items[0].Quality); + } + + [TestMethod()] + public void UpdateQualityTest_LegendaryItems_MaximumValue80() + { + IList Items = new List { new Item { Name = "Sulfuras, Hand of Ragnaros", SellIn = 1, Quality = 80} }; + GildedRose app = new GildedRose(Items); + + app.UpdateQuality(); + + Assert.AreEqual(80, Items[0].Quality); + } + + [TestMethod()] + public void UpdateQualityTest_ReduceSellIn_By1Daily() + { + IList Items = new List { new Item { Name = "Aged Brie", SellIn = 1, Quality = 30 } }; + GildedRose app = new GildedRose(Items); + + app.UpdateQuality(); + + Assert.AreEqual(0, Items[0].SellIn); } } } \ No newline at end of file diff --git a/csharpTests/csharpTests.csproj b/csharpTests/csharpUnitTests.csproj similarity index 100% rename from csharpTests/csharpTests.csproj rename to csharpTests/csharpUnitTests.csproj