From 951ca5f313c497d0322c06802db39a42c5e51287 Mon Sep 17 00:00:00 2001 From: Radek Date: Wed, 9 May 2018 20:29:33 +0200 Subject: [PATCH 1/5] Reformat code --- csharp/GildedRose.cs | 78 +++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index c60d97a0..9458a37d 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -4,83 +4,87 @@ namespace csharp { public class GildedRose { - IList Items; - public GildedRose(IList Items) - { - this.Items = Items; - } + private readonly IList _items; + + public GildedRose(IList items) + => _items = items; 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") + if (item.Name != "Aged Brie" + && item.Name != "Backstage passes to a TAFKAL80ETC concert") { - if (Items[i].Quality > 0) + if (item.Quality > 0) { - if (Items[i].Name != "Sulfuras, Hand of Ragnaros") + if (item.Name != "Sulfuras, Hand of Ragnaros") { - Items[i].Quality = Items[i].Quality - 1; + item.Quality = item.Quality - 1; } } } else { - if (Items[i].Quality < 50) + if (item.Quality < 50) { - Items[i].Quality = Items[i].Quality + 1; + item.Quality = item.Quality + 1; - if (Items[i].Name == "Backstage passes to a TAFKAL80ETC concert") + if (item.Name == "Backstage passes to a TAFKAL80ETC concert") { - if (Items[i].SellIn < 11) + if (item.SellIn < 11) { - if (Items[i].Quality < 50) + if (item.Quality < 50) { - Items[i].Quality = Items[i].Quality + 1; + item.Quality = item.Quality + 1; } } - if (Items[i].SellIn < 6) + if (item.SellIn < 6) { - if (Items[i].Quality < 50) + if (item.Quality < 50) { - Items[i].Quality = Items[i].Quality + 1; + item.Quality = item.Quality + 1; } } } } } - if (Items[i].Name != "Sulfuras, Hand of Ragnaros") + if (item.Name != "Sulfuras, Hand of Ragnaros") { - Items[i].SellIn = Items[i].SellIn - 1; + item.SellIn = item.SellIn - 1; } - if (Items[i].SellIn < 0) + if (item.SellIn >= 0) { - if (Items[i].Name != "Aged Brie") + continue; + } + + if (item.Name != "Aged Brie") + { + if (item.Name != "Backstage passes to a TAFKAL80ETC concert") { - if (Items[i].Name != "Backstage passes to a TAFKAL80ETC concert") + if (item.Quality <= 0) { - if (Items[i].Quality > 0) - { - if (Items[i].Name != "Sulfuras, Hand of Ragnaros") - { - Items[i].Quality = Items[i].Quality - 1; - } - } + continue; } - else + + if (item.Name != "Sulfuras, Hand of Ragnaros") { - Items[i].Quality = Items[i].Quality - Items[i].Quality; + item.Quality = item.Quality - 1; } } else { - if (Items[i].Quality < 50) - { - Items[i].Quality = Items[i].Quality + 1; - } + item.Quality = item.Quality - item.Quality; + } + } + else + { + if (item.Quality < 50) + { + item.Quality = item.Quality + 1; } } } From 2d7048efa8040974b114c5038b6c6e85967c8f16 Mon Sep 17 00:00:00 2001 From: Radek Date: Wed, 9 May 2018 21:06:31 +0200 Subject: [PATCH 2/5] Add tests covering basic logic --- csharp/GildedRoseTest.cs | 217 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 212 insertions(+), 5 deletions(-) diff --git a/csharp/GildedRoseTest.cs b/csharp/GildedRoseTest.cs index 911df1be..d8ddd99c 100644 --- a/csharp/GildedRoseTest.cs +++ b/csharp/GildedRoseTest.cs @@ -7,12 +7,219 @@ namespace csharp public class GildedRoseTest { [Test] - public void foo() + public void UpdateQuality_LowersItemQualityBy1() { - IList Items = new List { new Item { Name = "foo", SellIn = 0, Quality = 0 } }; - GildedRose app = new GildedRose(Items); - app.UpdateQuality(); - Assert.AreEqual("fixme", Items[0].Name); + var item = new Item + { + Name = "test", + Quality = 5, + SellIn = 30 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(4)); + } + + [Test] + public void UpdateQuality_LowersItemSellInBy1() + { + var item = new Item + { + Name = "test", + Quality = 5, + SellIn = 30 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.SellIn, Is.EqualTo(29)); + } + + [Test] + public void UpdateQuality_ItemWithZeroSellIn_LowersItemQualityBy2() + { + var item = new Item + { + Name = "test", + Quality = 5, + SellIn = 0 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(3)); + } + + [Test] + public void UpdateQuality_ItemWithZeroQuality_StayAtZeroQuality() + { + var item = new Item + { + Name = "test", + Quality = 0, + SellIn = 30 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(0)); + } + + [Test] + public void UpdateQuality_AgedBrie_IncreaseQualityBy1() + { + var item = new Item + { + Name = "Aged Brie", + Quality = 0, + SellIn = 30 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(1)); + } + + [Test] + public void UpdateQuality_AgedBrieWith50Quality_StayAt50Quality() + { + var item = new Item + { + Name = "Aged Brie", + Quality = 50, + SellIn = 30 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(50)); + } + + [Test] + public void UpdateQuality_Sulfuras_DoesNotDecreaseQuality() + { + var item = new Item + { + Name = "Sulfuras, Hand of Ragnaros", + Quality = 80, + SellIn = 30 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(80)); + } + + [Test] + public void UpdateQuality_Sulfuras_DoesNotDecreaseSellIn() + { + var item = new Item + { + Name = "Sulfuras, Hand of Ragnaros", + Quality = 80, + SellIn = 30 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.SellIn, Is.EqualTo(30)); + } + + [Test] + public void UpdateQuality_BackstagePassesWithMoreThan10DaysLeft_IncreasesQualityBy1() + { + var item = new Item + { + Name = "Backstage passes to a TAFKAL80ETC concert", + Quality = 35, + SellIn = 11 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(36)); + } + + [Test] + public void UpdateQuality_BackstagePassesWith10DaysLeft_IncreasesQualityBy2() + { + var item = new Item + { + Name = "Backstage passes to a TAFKAL80ETC concert", + Quality = 35, + SellIn = 10 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(37)); + } + + [Test] + public void UpdateQuality_BackstagePassesWith5DaysLeft_IncreasesQualityBy3() + { + var item = new Item + { + Name = "Backstage passes to a TAFKAL80ETC concert", + Quality = 35, + SellIn = 5 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(38)); + } + + [Test] + public void UpdateQuality_BackstagePassesWith0DaysLeft_SetQualityToZero() + { + var item = new Item + { + Name = "Backstage passes to a TAFKAL80ETC concert", + Quality = 35, + SellIn = 0 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(0)); } } } From c13dfb967a363c3341394dbde8c911455704c643 Mon Sep 17 00:00:00 2001 From: Radek Date: Wed, 9 May 2018 21:27:54 +0200 Subject: [PATCH 3/5] Implement Conjured items logic --- csharp/GildedRose.cs | 6 ++++-- csharp/GildedRoseTest.cs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index 9458a37d..9812fb75 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -13,6 +13,8 @@ namespace csharp { foreach (var item in _items) { + var qualityDegradationMultiplier = item.Name == "Conjured Mana Cake" ? 2 : 1; + if (item.Name != "Aged Brie" && item.Name != "Backstage passes to a TAFKAL80ETC concert") { @@ -20,7 +22,7 @@ namespace csharp { if (item.Name != "Sulfuras, Hand of Ragnaros") { - item.Quality = item.Quality - 1; + item.Quality = item.Quality - qualityDegradationMultiplier; } } } @@ -72,7 +74,7 @@ namespace csharp if (item.Name != "Sulfuras, Hand of Ragnaros") { - item.Quality = item.Quality - 1; + item.Quality = item.Quality - qualityDegradationMultiplier; } } else diff --git a/csharp/GildedRoseTest.cs b/csharp/GildedRoseTest.cs index d8ddd99c..ae51bfcd 100644 --- a/csharp/GildedRoseTest.cs +++ b/csharp/GildedRoseTest.cs @@ -221,5 +221,41 @@ namespace csharp Assert.That(item.Quality, Is.EqualTo(0)); } + + [Test] + public void UpdateQuality_ConjuredItemWithSellIn4_DecreasesQualityBy2() + { + var item = new Item + { + Name = "Conjured Mana Cake", + Quality = 35, + SellIn = 4 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(33)); + } + + [Test] + public void UpdateQuality_ConjuredItemWithSellIn0_DecreasesQualityBy4() + { + var item = new Item + { + Name = "Conjured Mana Cake", + Quality = 35, + SellIn = 0 + }; + + var items = new List(new[] { item }); + var gRose = new GildedRose(items); + + gRose.UpdateQuality(); + + Assert.That(item.Quality, Is.EqualTo(31)); + } } } From 44999deb68839ae00fce11ce13424aac6db56b7c Mon Sep 17 00:00:00 2001 From: Radek Date: Wed, 9 May 2018 21:29:38 +0200 Subject: [PATCH 4/5] Make implementation more readable --- csharp/GildedRose.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index 9812fb75..c5336e08 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -22,7 +22,7 @@ namespace csharp { if (item.Name != "Sulfuras, Hand of Ragnaros") { - item.Quality = item.Quality - qualityDegradationMultiplier; + item.Quality -= qualityDegradationMultiplier; } } } @@ -30,7 +30,7 @@ namespace csharp { if (item.Quality < 50) { - item.Quality = item.Quality + 1; + item.Quality++; if (item.Name == "Backstage passes to a TAFKAL80ETC concert") { @@ -38,7 +38,7 @@ namespace csharp { if (item.Quality < 50) { - item.Quality = item.Quality + 1; + item.Quality++; } } @@ -46,7 +46,7 @@ namespace csharp { if (item.Quality < 50) { - item.Quality = item.Quality + 1; + item.Quality++; } } } @@ -55,7 +55,7 @@ namespace csharp if (item.Name != "Sulfuras, Hand of Ragnaros") { - item.SellIn = item.SellIn - 1; + item.SellIn--; } if (item.SellIn >= 0) @@ -74,19 +74,19 @@ namespace csharp if (item.Name != "Sulfuras, Hand of Ragnaros") { - item.Quality = item.Quality - qualityDegradationMultiplier; + item.Quality -= qualityDegradationMultiplier; } } else { - item.Quality = item.Quality - item.Quality; + item.Quality = 0; } } else { if (item.Quality < 50) { - item.Quality = item.Quality + 1; + item.Quality++; } } } From 958f757cc402c708848e64f8b167ca0816a80f75 Mon Sep 17 00:00:00 2001 From: Radek Date: Wed, 9 May 2018 22:01:12 +0200 Subject: [PATCH 5/5] Refactor method --- csharp/ApprovalTest.ThirtyDays.received.txt | 28 ++--- csharp/GildedRose.cs | 113 ++++++++------------ 2 files changed, 59 insertions(+), 82 deletions(-) diff --git a/csharp/ApprovalTest.ThirtyDays.received.txt b/csharp/ApprovalTest.ThirtyDays.received.txt index cd66984f..4b5ab41f 100644 --- a/csharp/ApprovalTest.ThirtyDays.received.txt +++ b/csharp/ApprovalTest.ThirtyDays.received.txt @@ -21,7 +21,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 14, 21 Backstage passes to a TAFKAL80ETC concert, 9, 50 Backstage passes to a TAFKAL80ETC concert, 4, 50 -Conjured Mana Cake, 2, 5 +Conjured Mana Cake, 2, 4 -------- day 2 -------- name, sellIn, quality @@ -33,7 +33,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 13, 22 Backstage passes to a TAFKAL80ETC concert, 8, 50 Backstage passes to a TAFKAL80ETC concert, 3, 50 -Conjured Mana Cake, 1, 4 +Conjured Mana Cake, 1, 2 -------- day 3 -------- name, sellIn, quality @@ -45,7 +45,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 12, 23 Backstage passes to a TAFKAL80ETC concert, 7, 50 Backstage passes to a TAFKAL80ETC concert, 2, 50 -Conjured Mana Cake, 0, 3 +Conjured Mana Cake, 0, 0 -------- day 4 -------- name, sellIn, quality @@ -57,7 +57,7 @@ Sulfuras, Hand of Ragnaros, -1, 80 Backstage passes to a TAFKAL80ETC concert, 11, 24 Backstage passes to a TAFKAL80ETC concert, 6, 50 Backstage passes to a TAFKAL80ETC concert, 1, 50 -Conjured Mana Cake, -1, 1 +Conjured Mana Cake, -1, 0 -------- day 5 -------- name, sellIn, quality @@ -66,7 +66,7 @@ Aged Brie, -3, 8 Elixir of the Mongoose, 0, 2 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 10, 25 +Backstage passes to a TAFKAL80ETC concert, 10, 26 Backstage passes to a TAFKAL80ETC concert, 5, 50 Backstage passes to a TAFKAL80ETC concert, 0, 50 Conjured Mana Cake, -2, 0 @@ -78,7 +78,7 @@ Aged Brie, -4, 10 Elixir of the Mongoose, -1, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 9, 27 +Backstage passes to a TAFKAL80ETC concert, 9, 28 Backstage passes to a TAFKAL80ETC concert, 4, 50 Backstage passes to a TAFKAL80ETC concert, -1, 0 Conjured Mana Cake, -3, 0 @@ -90,7 +90,7 @@ Aged Brie, -5, 12 Elixir of the Mongoose, -2, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 8, 29 +Backstage passes to a TAFKAL80ETC concert, 8, 30 Backstage passes to a TAFKAL80ETC concert, 3, 50 Backstage passes to a TAFKAL80ETC concert, -2, 0 Conjured Mana Cake, -4, 0 @@ -102,7 +102,7 @@ Aged Brie, -6, 14 Elixir of the Mongoose, -3, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 7, 31 +Backstage passes to a TAFKAL80ETC concert, 7, 32 Backstage passes to a TAFKAL80ETC concert, 2, 50 Backstage passes to a TAFKAL80ETC concert, -3, 0 Conjured Mana Cake, -5, 0 @@ -114,7 +114,7 @@ Aged Brie, -7, 16 Elixir of the Mongoose, -4, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 6, 33 +Backstage passes to a TAFKAL80ETC concert, 6, 34 Backstage passes to a TAFKAL80ETC concert, 1, 50 Backstage passes to a TAFKAL80ETC concert, -4, 0 Conjured Mana Cake, -6, 0 @@ -126,7 +126,7 @@ Aged Brie, -8, 18 Elixir of the Mongoose, -5, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 5, 35 +Backstage passes to a TAFKAL80ETC concert, 5, 37 Backstage passes to a TAFKAL80ETC concert, 0, 50 Backstage passes to a TAFKAL80ETC concert, -5, 0 Conjured Mana Cake, -7, 0 @@ -138,7 +138,7 @@ Aged Brie, -9, 20 Elixir of the Mongoose, -6, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 4, 38 +Backstage passes to a TAFKAL80ETC concert, 4, 40 Backstage passes to a TAFKAL80ETC concert, -1, 0 Backstage passes to a TAFKAL80ETC concert, -6, 0 Conjured Mana Cake, -8, 0 @@ -150,7 +150,7 @@ Aged Brie, -10, 22 Elixir of the Mongoose, -7, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 3, 41 +Backstage passes to a TAFKAL80ETC concert, 3, 43 Backstage passes to a TAFKAL80ETC concert, -2, 0 Backstage passes to a TAFKAL80ETC concert, -7, 0 Conjured Mana Cake, -9, 0 @@ -162,7 +162,7 @@ Aged Brie, -11, 24 Elixir of the Mongoose, -8, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 2, 44 +Backstage passes to a TAFKAL80ETC concert, 2, 46 Backstage passes to a TAFKAL80ETC concert, -3, 0 Backstage passes to a TAFKAL80ETC concert, -8, 0 Conjured Mana Cake, -10, 0 @@ -174,7 +174,7 @@ Aged Brie, -12, 26 Elixir of the Mongoose, -9, 0 Sulfuras, Hand of Ragnaros, 0, 80 Sulfuras, Hand of Ragnaros, -1, 80 -Backstage passes to a TAFKAL80ETC concert, 1, 47 +Backstage passes to a TAFKAL80ETC concert, 1, 49 Backstage passes to a TAFKAL80ETC concert, -4, 0 Backstage passes to a TAFKAL80ETC concert, -9, 0 Conjured Mana Cake, -11, 0 diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index c5336e08..46ee0006 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -1,94 +1,71 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Runtime.Remoting.Messaging; namespace csharp { public class GildedRose { + private readonly IList _items; public GildedRose(IList items) => _items = items; + private static int GetQualityChange(Item item) + { + var sellIn = item.SellIn; + + switch (item.Name) + { + case "Sulfuras, Hand of Ragnaros": + return 0; + case "Backstage passes to a TAFKAL80ETC concert": + if (sellIn > 9) + { + return 1; + } + else if (sellIn > 4) + { + return 2; + } + else if (sellIn >= 0) + { + return 3; + } + else + { + return -item.Quality; + } + case "Aged Brie": + return sellIn < 0 ? 2 : 1; + case "Conjured Mana Cake": + return sellIn < 0 ? -4 : -2; + default: + return sellIn < 0 ? -2 : -1; + } + } + public void UpdateQuality() { foreach (var item in _items) { - var qualityDegradationMultiplier = item.Name == "Conjured Mana Cake" ? 2 : 1; + item.SellIn--; - if (item.Name != "Aged Brie" - && item.Name != "Backstage passes to a TAFKAL80ETC concert") - { - if (item.Quality > 0) - { - if (item.Name != "Sulfuras, Hand of Ragnaros") - { - item.Quality -= qualityDegradationMultiplier; - } - } - } - else - { - if (item.Quality < 50) - { - item.Quality++; + var qualityChange = GetQualityChange(item); - if (item.Name == "Backstage passes to a TAFKAL80ETC concert") - { - if (item.SellIn < 11) - { - if (item.Quality < 50) - { - item.Quality++; - } - } - - if (item.SellIn < 6) - { - if (item.Quality < 50) - { - item.Quality++; - } - } - } - } - } + item.Quality += qualityChange; if (item.Name != "Sulfuras, Hand of Ragnaros") { - item.SellIn--; - } - - if (item.SellIn >= 0) - { - continue; - } - - if (item.Name != "Aged Brie") - { - if (item.Name != "Backstage passes to a TAFKAL80ETC concert") - { - if (item.Quality <= 0) - { - continue; - } - - if (item.Name != "Sulfuras, Hand of Ragnaros") - { - item.Quality -= qualityDegradationMultiplier; - } - } - else - { - item.Quality = 0; - } + item.Quality = Math.Min(50, item.Quality); } else { - if (item.Quality < 50) - { - item.Quality++; - } + item.SellIn++; } + + item.Quality = Math.Max(0, item.Quality); } } }