From 32b9ad32e15ee9ab7a9735aa9762b2639ec286be Mon Sep 17 00:00:00 2001 From: israel Date: Thu, 19 Oct 2017 12:34:55 +0100 Subject: [PATCH 01/10] Golden master added for ThirtyDays test. --- ...irtyDays.received.txt => ApprovalTest.ThirtyDays.approved.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename csharp/{ApprovalTest.ThirtyDays.received.txt => ApprovalTest.ThirtyDays.approved.txt} (100%) diff --git a/csharp/ApprovalTest.ThirtyDays.received.txt b/csharp/ApprovalTest.ThirtyDays.approved.txt similarity index 100% rename from csharp/ApprovalTest.ThirtyDays.received.txt rename to csharp/ApprovalTest.ThirtyDays.approved.txt From f9d54a73cde1faa92a447a05bbd5bbd5ef973e3c Mon Sep 17 00:00:00 2001 From: israel Date: Thu, 19 Oct 2017 13:25:03 +0100 Subject: [PATCH 02/10] Create ItemWrapperContext class. --- csharp/GildedRose.cs | 10 +++-- csharp/Strategy/ItemWrapperContext.cs | 56 +++++++++++++++++++++++++++ csharp/csharp.csproj | 1 + 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 csharp/Strategy/ItemWrapperContext.cs diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index c60d97a0..b8bd7d20 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -1,13 +1,15 @@ -using System.Collections.Generic; +using System.Linq; +using System.Collections.Generic; +using csharp.Strategy; namespace csharp { public class GildedRose { - IList Items; + IList Items; public GildedRose(IList Items) - { - this.Items = Items; + { + this.Items = Items.Select(i => new ItemWrapperContext(i)).ToList(); } public void UpdateQuality() diff --git a/csharp/Strategy/ItemWrapperContext.cs b/csharp/Strategy/ItemWrapperContext.cs new file mode 100644 index 00000000..82f53489 --- /dev/null +++ b/csharp/Strategy/ItemWrapperContext.cs @@ -0,0 +1,56 @@ +using System; + +namespace csharp.Strategy +{ + public class ItemWrapperContext + { + #region Variables + + public string Name + { + get { return this._item.Name; } + set { this._item.Name = value; } + } + + public int Quality + { + get { return this._item.Quality; } + set { this._item.Quality = value; } + } + + public int SellIn + { + get { return this._item.SellIn; } + set { this._item.SellIn = value; } + } + + //private List listCategoryStrategies; + private Item _item; + + #endregion + + #region Constructor + + public ItemWrapperContext(Item item) + { + this._item = item; + + //listCategoryStrategies = StrategiesFactory.GetInstance().GetCategoryStrategies(item); + } + + #endregion + + #region Methods + + //public void UpdateQuality() + //{ + // foreach (ICategoryStrategy categoryStrategyItem in this.listCategoryStrategies) + // { + // categoryStrategyItem.UpdateQuality(this); + // } + //} + + #endregion + + } +} diff --git a/csharp/csharp.csproj b/csharp/csharp.csproj index 37813560..d213e84d 100644 --- a/csharp/csharp.csproj +++ b/csharp/csharp.csproj @@ -65,6 +65,7 @@ + From 649e42d2568afb5470f964a9cff198954b47ecc7 Mon Sep 17 00:00:00 2001 From: israel Date: Fri, 20 Oct 2017 10:18:26 +0100 Subject: [PATCH 03/10] Included NUnit Testing Adapter. --- csharp/packages.config | 1 + 1 file changed, 1 insertion(+) diff --git a/csharp/packages.config b/csharp/packages.config index 77fb974a..2cd38b71 100644 --- a/csharp/packages.config +++ b/csharp/packages.config @@ -3,4 +3,5 @@ + \ No newline at end of file From 75d8793a8222a55547fea503f3037801aa469986 Mon Sep 17 00:00:00 2001 From: israel Date: Fri, 20 Oct 2017 10:36:32 +0100 Subject: [PATCH 04/10] Code refactored to adapt it to Strategy Pattern. --- csharp/GildedRose.cs | 9 +++ csharp/Strategy/CategoryStrategiesFactory.cs | 77 +++++++++++++++++++ csharp/Strategy/ICategoryStrategy.cs | 9 +++ csharp/Strategy/ItemWrapperContext.cs | 28 ++++--- .../NextExpiredImproveQualityStrategy.cs | 37 +++++++++ csharp/Strategy/NormalDegradeStrategy.cs | 22 ++++++ csharp/Strategy/OlderIsBetterStrategy.cs | 26 +++++++ .../TwiceFastDegradeQualityStrategy.cs | 29 +++++++ csharp/csharp.csproj | 13 ++++ 9 files changed, 238 insertions(+), 12 deletions(-) create mode 100644 csharp/Strategy/CategoryStrategiesFactory.cs create mode 100644 csharp/Strategy/ICategoryStrategy.cs create mode 100644 csharp/Strategy/NextExpiredImproveQualityStrategy.cs create mode 100644 csharp/Strategy/NormalDegradeStrategy.cs create mode 100644 csharp/Strategy/OlderIsBetterStrategy.cs create mode 100644 csharp/Strategy/TwiceFastDegradeQualityStrategy.cs diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index b8bd7d20..a73bd729 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -12,7 +12,16 @@ namespace csharp this.Items = Items.Select(i => new ItemWrapperContext(i)).ToList(); } + public void UpdateQuality() + { + for (var i = 0; i < Items.Count; i++) + { + Items[i].UpdateQuality(); + } + } + + public void UpdateQuality_old() { for (var i = 0; i < Items.Count; i++) { diff --git a/csharp/Strategy/CategoryStrategiesFactory.cs b/csharp/Strategy/CategoryStrategiesFactory.cs new file mode 100644 index 00000000..59f7913d --- /dev/null +++ b/csharp/Strategy/CategoryStrategiesFactory.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; + +namespace csharp.Strategy +{ + class CategoryStrategiesFactory + { + #region Variables + + static private CategoryStrategiesFactory _strategiesFactory = null; + + #endregion + + #region Constructor + + private CategoryStrategiesFactory() { } + + #endregion + + #region Methods + + public static CategoryStrategiesFactory GetInstance() + { + if (_strategiesFactory == null) + { + _strategiesFactory = new CategoryStrategiesFactory(); + } + + return _strategiesFactory; + } + + #endregion + + public List GetCategoryStrategies(Item item) + { + List listCategoryStrategies = new List(); + + if (item.Name == "Aged Brie") + { + listCategoryStrategies = new List() + { + new OlderIsBetterStrategy() + }; + + } + else if (item.Name == "Backstage passes to a TAFKAL80ETC concert") + { + listCategoryStrategies = new List() + { + new NextExpiredImproveQualityStrategy() + }; + } + else if (item.Name == "Sulfuras, Hand of Ragnaros") + { + listCategoryStrategies = new List() + { + }; + } + else if (item.Name == "Conjured Mana Cake") + { + listCategoryStrategies = new List() + { + new TwiceFastDegradeQualityStrategy() + }; + } + else + { + listCategoryStrategies = new List() + { + new NormalDegradeStrategy() + }; + } + + return listCategoryStrategies; + } + } +} diff --git a/csharp/Strategy/ICategoryStrategy.cs b/csharp/Strategy/ICategoryStrategy.cs new file mode 100644 index 00000000..b6a7557e --- /dev/null +++ b/csharp/Strategy/ICategoryStrategy.cs @@ -0,0 +1,9 @@ +using System; + +namespace csharp.Strategy +{ + interface ICategoryStrategy + { + void Update(Item item); + } +} diff --git a/csharp/Strategy/ItemWrapperContext.cs b/csharp/Strategy/ItemWrapperContext.cs index 82f53489..335ce2e4 100644 --- a/csharp/Strategy/ItemWrapperContext.cs +++ b/csharp/Strategy/ItemWrapperContext.cs @@ -1,10 +1,18 @@ using System; +using System.Collections.Generic; namespace csharp.Strategy { public class ItemWrapperContext { #region Variables + + private Item _item; + private List listCategoryStrategies; + + #endregion + + #region Properties public string Name { @@ -24,9 +32,6 @@ namespace csharp.Strategy set { this._item.SellIn = value; } } - //private List listCategoryStrategies; - private Item _item; - #endregion #region Constructor @@ -35,22 +40,21 @@ namespace csharp.Strategy { this._item = item; - //listCategoryStrategies = StrategiesFactory.GetInstance().GetCategoryStrategies(item); + listCategoryStrategies = CategoryStrategiesFactory.GetInstance().GetCategoryStrategies(item); } #endregion #region Methods - //public void UpdateQuality() - //{ - // foreach (ICategoryStrategy categoryStrategyItem in this.listCategoryStrategies) - // { - // categoryStrategyItem.UpdateQuality(this); - // } - //} + public void UpdateQuality() + { + foreach (ICategoryStrategy categoryStrategyItem in this.listCategoryStrategies) + { + categoryStrategyItem.Update(this._item); + } + } #endregion - } } diff --git a/csharp/Strategy/NextExpiredImproveQualityStrategy.cs b/csharp/Strategy/NextExpiredImproveQualityStrategy.cs new file mode 100644 index 00000000..aeeaf259 --- /dev/null +++ b/csharp/Strategy/NextExpiredImproveQualityStrategy.cs @@ -0,0 +1,37 @@ +using System; + +namespace csharp.Strategy +{ + class NextExpiredImproveQualityStrategy : ICategoryStrategy + { + public void Update(Item item) + { + item.SellIn--; + + if (item.SellIn < 0) + { + item.Quality = 0; + } + else + { + int inc = 1; + + if (item.SellIn < 5) + { + inc = 3; + } + else if (item.SellIn < 10) + { + inc = 2; + } + + item.Quality += inc; + + if (item.Quality > 50) + { + item.Quality = 50; + } + } + } + } +} diff --git a/csharp/Strategy/NormalDegradeStrategy.cs b/csharp/Strategy/NormalDegradeStrategy.cs new file mode 100644 index 00000000..36f43b85 --- /dev/null +++ b/csharp/Strategy/NormalDegradeStrategy.cs @@ -0,0 +1,22 @@ +using System; + +namespace csharp.Strategy +{ + class NormalDegradeStrategy : ICategoryStrategy + { + public void Update(Item item) + { + if (item.Quality > 0) + { + item.Quality--; + } + + item.SellIn--; + + if (item.SellIn < 0 && item.Quality > 0) + { + item.Quality--; + } + } + } +} diff --git a/csharp/Strategy/OlderIsBetterStrategy.cs b/csharp/Strategy/OlderIsBetterStrategy.cs new file mode 100644 index 00000000..e42a0b24 --- /dev/null +++ b/csharp/Strategy/OlderIsBetterStrategy.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace csharp.Strategy +{ + class OlderIsBetterStrategy : ICategoryStrategy + { + public void Update(Item item) + { + if (item.Quality < 50) + { + item.Quality++; + } + + item.SellIn--; + + if (item.SellIn < 0 && item.Quality < 50) + { + item.Quality++; + } + } + } +} diff --git a/csharp/Strategy/TwiceFastDegradeQualityStrategy.cs b/csharp/Strategy/TwiceFastDegradeQualityStrategy.cs new file mode 100644 index 00000000..b64b18af --- /dev/null +++ b/csharp/Strategy/TwiceFastDegradeQualityStrategy.cs @@ -0,0 +1,29 @@ +using System; + +namespace csharp.Strategy +{ + class TwiceFastDegradeQualityStrategy : ICategoryStrategy + { + public void Update(Item item) + { + int degrade = 1; + + item.SellIn--; + + if (item.SellIn < 0) + { + degrade = 2; + } + + if (item.Quality > 0) + { + item.Quality -= degrade; + } + + if (item.Quality < 0) + { + item.Quality = 0; + } + } + } +} diff --git a/csharp/csharp.csproj b/csharp/csharp.csproj index d213e84d..ce85d440 100644 --- a/csharp/csharp.csproj +++ b/csharp/csharp.csproj @@ -1,5 +1,6 @@  + Debug @@ -65,9 +66,15 @@ + + + + + + @@ -77,6 +84,12 @@ + + + Este proyecto hace referencia a los paquetes NuGet que faltan en este equipo. Use la restauración de paquetes NuGet para descargarlos. Para obtener más información, consulte http://go.microsoft.com/fwlink/?LinkID=322105. El archivo que falta es {0}. + + +