From e333daef74359d08e634deb80ee1e5c91a0464ad Mon Sep 17 00:00:00 2001 From: israel Date: Fri, 20 Oct 2017 12:28:44 +0100 Subject: [PATCH] Refactored hardcoded item names. Now there are consts in Global class which define them. --- csharp/ConjuredGildedRoseTest.cs | 26 ------ csharp/GildedRoseTest.cs | 17 ++++ csharp/Global.cs | 19 +++++ csharp/Program.cs | 18 ++--- .../CategoryStrategiesFactory.cs | 21 +++-- csharp/StrategyPatternExample/Global.cs | 10 --- .../CloseExpiredImproveQualityStrategy.cs | 80 +++++++++++++++++++ .../NextExpiredImproveQualityStrategy.cs | 37 --------- .../Strategies/NormalDegradeStrategy.cs | 6 +- .../Strategies/OlderIsBetterStrategy.cs | 6 +- .../TwiceFastDegradeQualityStrategy.cs | 8 +- csharp/csharp.csproj | 5 +- 12 files changed, 153 insertions(+), 100 deletions(-) delete mode 100644 csharp/ConjuredGildedRoseTest.cs create mode 100644 csharp/Global.cs delete mode 100644 csharp/StrategyPatternExample/Global.cs create mode 100644 csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs delete mode 100644 csharp/StrategyPatternExample/Strategies/NextExpiredImproveQualityStrategy.cs diff --git a/csharp/ConjuredGildedRoseTest.cs b/csharp/ConjuredGildedRoseTest.cs deleted file mode 100644 index b8feb2f6..00000000 --- a/csharp/ConjuredGildedRoseTest.cs +++ /dev/null @@ -1,26 +0,0 @@ -using NUnit.Framework; -using System.Collections.Generic; - -namespace csharp -{ - [TestFixture] - public class ConjuredGildedRoseTest - { - [Test] - public void Quality() - { - IList Items = new List { new Item { Name = "Conjured Mana Cake", SellIn = 1, Quality = 8 } }; - GildedRose app = new GildedRose(Items); - - // "Conjured" items degrade in Quality twice as fast as normal items. - // So, - // SellIn >= 0 => degrade = -2 - // SellIn < 0 => degrade = -4 - app.UpdateQuality(); - Assert.AreEqual(6, Items[0].Quality); - - app.UpdateQuality(); - Assert.AreEqual(2, Items[0].Quality); - } - } -} diff --git a/csharp/GildedRoseTest.cs b/csharp/GildedRoseTest.cs index b7729968..07dbf477 100644 --- a/csharp/GildedRoseTest.cs +++ b/csharp/GildedRoseTest.cs @@ -14,5 +14,22 @@ namespace csharp app.UpdateQuality(); Assert.AreNotEqual("fixme", Items[0].Name); } + + [Test] + public void ConjuredQuality() + { + IList Items = new List { new Item { Name = "Conjured Mana Cake", SellIn = 1, Quality = 8 } }; + GildedRose app = new GildedRose(Items); + + // "Conjured" items degrade in Quality twice as fast as normal items. + // So, + // SellIn >= 0 => degrade = -2 + // SellIn < 0 => degrade = -4 + app.UpdateQuality(); + Assert.AreEqual(6, Items[0].Quality); + + app.UpdateQuality(); + Assert.AreEqual(2, Items[0].Quality); + } } } diff --git a/csharp/Global.cs b/csharp/Global.cs new file mode 100644 index 00000000..9bf79daf --- /dev/null +++ b/csharp/Global.cs @@ -0,0 +1,19 @@ +using System; + +namespace csharp +{ + static public class Global + { + // Limits of the quality field. + public const int MAXIMUM_QUALITY = 50; + public const int MINIMUM_QUALITY = 0; + + // Item names. + public const string NAME_ITEM_PLUS5_DEXTERITY = "+5 Dexterity Vest"; + public const string NAME_ITEM_AGED_BRIE = "Aged Brie"; + public const string NAME_ITEM_ELIXIR_MONGOOSE = "Elixir of the Mongoose"; + public const string NAME_ITEM_SULFURAS = "Sulfuras, Hand of Ragnaros"; + public const string NAME_ITEM_BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert"; + public const string NAME_ITEM_CONJURED = "Conjured Mana Cake"; + } +} diff --git a/csharp/Program.cs b/csharp/Program.cs index f5958fa4..885ee404 100644 --- a/csharp/Program.cs +++ b/csharp/Program.cs @@ -11,30 +11,30 @@ namespace csharp Console.WriteLine("OMGHAI!"); IList Items = new List{ - new Item {Name = "+5 Dexterity Vest", SellIn = 10, Quality = 20}, - new Item {Name = "Aged Brie", SellIn = 2, Quality = 0}, - new Item {Name = "Elixir of the Mongoose", SellIn = 5, Quality = 7}, - new Item {Name = "Sulfuras, Hand of Ragnaros", SellIn = 0, Quality = 80}, - new Item {Name = "Sulfuras, Hand of Ragnaros", SellIn = -1, Quality = 80}, + new Item {Name = Global.NAME_ITEM_PLUS5_DEXTERITY, SellIn = 10, Quality = 20}, + new Item {Name = Global.NAME_ITEM_AGED_BRIE, SellIn = 2, Quality = 0}, + new Item {Name = Global.NAME_ITEM_ELIXIR_MONGOOSE, SellIn = 5, Quality = 7}, + new Item {Name = Global.NAME_ITEM_SULFURAS, SellIn = 0, Quality = 80}, + new Item {Name = Global.NAME_ITEM_SULFURAS, SellIn = -1, Quality = 80}, new Item { - Name = "Backstage passes to a TAFKAL80ETC concert", + Name = Global.NAME_ITEM_BACKSTAGE_PASSES, SellIn = 15, Quality = 20 }, new Item { - Name = "Backstage passes to a TAFKAL80ETC concert", + Name = Global.NAME_ITEM_BACKSTAGE_PASSES, SellIn = 10, Quality = 49 }, new Item { - Name = "Backstage passes to a TAFKAL80ETC concert", + Name = Global.NAME_ITEM_BACKSTAGE_PASSES, SellIn = 5, Quality = 49 }, - new Item {Name = "Conjured Mana Cake", SellIn = 3, Quality = 6} + new Item {Name = Global.NAME_ITEM_CONJURED, SellIn = 3, Quality = 6} }; IGildedRoseApp app = null; diff --git a/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs b/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs index 130271b2..a0fb1342 100644 --- a/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs +++ b/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs @@ -34,7 +34,7 @@ namespace csharp.StrategyPatternExample { List listCategoryStrategies = new List(); - if (item.Name == "Aged Brie") + if (item.Name == Global.NAME_ITEM_AGED_BRIE) { listCategoryStrategies = new List() { @@ -42,20 +42,31 @@ namespace csharp.StrategyPatternExample }; } - else if (item.Name == "Backstage passes to a TAFKAL80ETC concert") + else if (item.Name == Global.NAME_ITEM_BACKSTAGE_PASSES) { listCategoryStrategies = new List() { - new NextExpiredImproveQualityStrategy() + new CloseExpiredImproveQualityStrategy(new List() { + new CloseExpiredImproveQualityStrategy.NextExpiredCondition() + { + SellInLimit = 5, + Increment = 3 + }, + new CloseExpiredImproveQualityStrategy.NextExpiredCondition() + { + SellInLimit = 10, + Increment = 2 + } + }) }; } - else if (item.Name == "Sulfuras, Hand of Ragnaros") + else if (item.Name == Global.NAME_ITEM_SULFURAS) { listCategoryStrategies = new List() { }; } - else if (item.Name == "Conjured Mana Cake") + else if (item.Name == Global.NAME_ITEM_CONJURED) { listCategoryStrategies = new List() { diff --git a/csharp/StrategyPatternExample/Global.cs b/csharp/StrategyPatternExample/Global.cs deleted file mode 100644 index 38beae85..00000000 --- a/csharp/StrategyPatternExample/Global.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace csharp.StrategyPatternExample -{ - static public class Global - { - public const int MAXIMUN_QUALITY = 50; - public const int MINIMUN_QUALITY = 0; - } -} diff --git a/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs b/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs new file mode 100644 index 00000000..53605b26 --- /dev/null +++ b/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; + +namespace csharp.StrategyPatternExample.Strategy +{ + internal class CloseExpiredImproveQualityStrategy : ICategoryStrategy + { + #region subclasses + + public class NextExpiredCondition + { + /// + /// SellIn to check. + /// + public int SellInLimit { get; set; } + + /// + /// Incremento to apply. + /// + public int Increment { get; set; } + } + + #endregion + + #region Variables + + private IList listConditions; + + #endregion + + #region Constructor + + public CloseExpiredImproveQualityStrategy(IList conditions) + { + if (conditions == null && conditions.Count == 0) + { + // INFO : A good candidate to Globalization. + throw new ArgumentException("Param conditions list is empty."); + } + + this.listConditions = conditions; + } + + #endregion + + #region Methods + + public void Update(Item item) + { + item.SellIn--; + + if (item.SellIn < 0) + { + item.Quality = Global.MINIMUM_QUALITY; + } + else + { + int inc = 1; + + foreach (NextExpiredCondition condition in listConditions) + { + if (item.SellIn < condition.SellInLimit) + { + inc = condition.Increment; + break; + } + } + + item.Quality += inc; + + if (item.Quality > Global.MAXIMUM_QUALITY) + { + item.Quality = Global.MAXIMUM_QUALITY; + } + } + } + + #endregion + } +} diff --git a/csharp/StrategyPatternExample/Strategies/NextExpiredImproveQualityStrategy.cs b/csharp/StrategyPatternExample/Strategies/NextExpiredImproveQualityStrategy.cs deleted file mode 100644 index 46b30a26..00000000 --- a/csharp/StrategyPatternExample/Strategies/NextExpiredImproveQualityStrategy.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; - -namespace csharp.StrategyPatternExample.Strategy -{ - class NextExpiredImproveQualityStrategy : ICategoryStrategy - { - public void Update(Item item) - { - item.SellIn--; - - if (item.SellIn < 0) - { - item.Quality = Global.MINIMUN_QUALITY; - } - else - { - int inc = 1; - - if (item.SellIn < 5) - { - inc = 3; - } - else if (item.SellIn < 10) - { - inc = 2; - } - - item.Quality += inc; - - if (item.Quality > Global.MAXIMUN_QUALITY) - { - item.Quality = Global.MAXIMUN_QUALITY; - } - } - } - } -} diff --git a/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs b/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs index e52bb555..2e1d823b 100644 --- a/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs +++ b/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs @@ -2,18 +2,18 @@ namespace csharp.StrategyPatternExample.Strategy { - class NormalDegradeStrategy : ICategoryStrategy + internal class NormalDegradeStrategy : ICategoryStrategy { public void Update(Item item) { - if (item.Quality > Global.MINIMUN_QUALITY) + if (item.Quality > Global.MINIMUM_QUALITY) { item.Quality--; } item.SellIn--; - if (item.SellIn < 0 && item.Quality > Global.MINIMUN_QUALITY) + if (item.SellIn < 0 && item.Quality > Global.MINIMUM_QUALITY) { item.Quality--; } diff --git a/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs b/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs index 7414c008..688d7e07 100644 --- a/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs +++ b/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs @@ -2,18 +2,18 @@ namespace csharp.StrategyPatternExample.Strategy { - class OlderIsBetterStrategy : ICategoryStrategy + internal class OlderIsBetterStrategy : ICategoryStrategy { public void Update(Item item) { - if (item.Quality < Global.MAXIMUN_QUALITY) + if (item.Quality < Global.MAXIMUM_QUALITY) { item.Quality++; } item.SellIn--; - if (item.SellIn < 0 && item.Quality < Global.MAXIMUN_QUALITY) + if (item.SellIn < 0 && item.Quality < Global.MAXIMUM_QUALITY) { item.Quality++; } diff --git a/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs b/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs index 5b45df35..4f71d653 100644 --- a/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs +++ b/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs @@ -2,7 +2,7 @@ namespace csharp.StrategyPatternExample.Strategy { - class TwiceFastDegradeQualityStrategy : ICategoryStrategy + internal class TwiceFastDegradeQualityStrategy : ICategoryStrategy { public void Update(Item item) { @@ -15,14 +15,14 @@ namespace csharp.StrategyPatternExample.Strategy degrade = 4; } - if (item.Quality > Global.MINIMUN_QUALITY) + if (item.Quality > Global.MINIMUM_QUALITY) { item.Quality -= degrade; } - if (item.Quality < Global.MINIMUN_QUALITY) + if (item.Quality < Global.MINIMUM_QUALITY) { - item.Quality = Global.MINIMUN_QUALITY; + item.Quality = Global.MINIMUM_QUALITY; } } } diff --git a/csharp/csharp.csproj b/csharp/csharp.csproj index 34b7076f..e6ca86ce 100644 --- a/csharp/csharp.csproj +++ b/csharp/csharp.csproj @@ -65,17 +65,16 @@ - - + - +