diff --git a/csharp/ApprovalTest.ThirtyDays.received.txt b/csharp/ApprovalTest.ThirtyDays.approved.txt similarity index 99% rename from csharp/ApprovalTest.ThirtyDays.received.txt rename to csharp/ApprovalTest.ThirtyDays.approved.txt index cd66984f..f80af4e4 100644 --- a/csharp/ApprovalTest.ThirtyDays.received.txt +++ b/csharp/ApprovalTest.ThirtyDays.approved.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 diff --git a/csharp/ApprovalTest.ThirtyDays_StrategyPatternExample.approved.txt b/csharp/ApprovalTest.ThirtyDays_StrategyPatternExample.approved.txt new file mode 100644 index 00000000..f80af4e4 --- /dev/null +++ b/csharp/ApprovalTest.ThirtyDays_StrategyPatternExample.approved.txt @@ -0,0 +1,373 @@ +OMGHAI! +-------- day 0 -------- +name, sellIn, quality ++5 Dexterity Vest, 10, 20 +Aged Brie, 2, 0 +Elixir of the Mongoose, 5, 7 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, 15, 20 +Backstage passes to a TAFKAL80ETC concert, 10, 49 +Backstage passes to a TAFKAL80ETC concert, 5, 49 +Conjured Mana Cake, 3, 6 + +-------- day 1 -------- +name, sellIn, quality ++5 Dexterity Vest, 9, 19 +Aged Brie, 1, 1 +Elixir of the Mongoose, 4, 6 +Sulfuras, Hand of Ragnaros, 0, 80 +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, 4 + +-------- day 2 -------- +name, sellIn, quality ++5 Dexterity Vest, 8, 18 +Aged Brie, 0, 2 +Elixir of the Mongoose, 3, 5 +Sulfuras, Hand of Ragnaros, 0, 80 +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, 2 + +-------- day 3 -------- +name, sellIn, quality ++5 Dexterity Vest, 7, 17 +Aged Brie, -1, 4 +Elixir of the Mongoose, 2, 4 +Sulfuras, Hand of Ragnaros, 0, 80 +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, 0 + +-------- day 4 -------- +name, sellIn, quality ++5 Dexterity Vest, 6, 16 +Aged Brie, -2, 6 +Elixir of the Mongoose, 1, 3 +Sulfuras, Hand of Ragnaros, 0, 80 +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, 0 + +-------- day 5 -------- +name, sellIn, quality ++5 Dexterity Vest, 5, 15 +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, 5, 50 +Backstage passes to a TAFKAL80ETC concert, 0, 50 +Conjured Mana Cake, -2, 0 + +-------- day 6 -------- +name, sellIn, quality ++5 Dexterity Vest, 4, 14 +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, 4, 50 +Backstage passes to a TAFKAL80ETC concert, -1, 0 +Conjured Mana Cake, -3, 0 + +-------- day 7 -------- +name, sellIn, quality ++5 Dexterity Vest, 3, 13 +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, 3, 50 +Backstage passes to a TAFKAL80ETC concert, -2, 0 +Conjured Mana Cake, -4, 0 + +-------- day 8 -------- +name, sellIn, quality ++5 Dexterity Vest, 2, 12 +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, 2, 50 +Backstage passes to a TAFKAL80ETC concert, -3, 0 +Conjured Mana Cake, -5, 0 + +-------- day 9 -------- +name, sellIn, quality ++5 Dexterity Vest, 1, 11 +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, 1, 50 +Backstage passes to a TAFKAL80ETC concert, -4, 0 +Conjured Mana Cake, -6, 0 + +-------- day 10 -------- +name, sellIn, quality ++5 Dexterity Vest, 0, 10 +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, 0, 50 +Backstage passes to a TAFKAL80ETC concert, -5, 0 +Conjured Mana Cake, -7, 0 + +-------- day 11 -------- +name, sellIn, quality ++5 Dexterity Vest, -1, 8 +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, -1, 0 +Backstage passes to a TAFKAL80ETC concert, -6, 0 +Conjured Mana Cake, -8, 0 + +-------- day 12 -------- +name, sellIn, quality ++5 Dexterity Vest, -2, 6 +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, -2, 0 +Backstage passes to a TAFKAL80ETC concert, -7, 0 +Conjured Mana Cake, -9, 0 + +-------- day 13 -------- +name, sellIn, quality ++5 Dexterity Vest, -3, 4 +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, -3, 0 +Backstage passes to a TAFKAL80ETC concert, -8, 0 +Conjured Mana Cake, -10, 0 + +-------- day 14 -------- +name, sellIn, quality ++5 Dexterity Vest, -4, 2 +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, -4, 0 +Backstage passes to a TAFKAL80ETC concert, -9, 0 +Conjured Mana Cake, -11, 0 + +-------- day 15 -------- +name, sellIn, quality ++5 Dexterity Vest, -5, 0 +Aged Brie, -13, 28 +Elixir of the Mongoose, -10, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, 0, 50 +Backstage passes to a TAFKAL80ETC concert, -5, 0 +Backstage passes to a TAFKAL80ETC concert, -10, 0 +Conjured Mana Cake, -12, 0 + +-------- day 16 -------- +name, sellIn, quality ++5 Dexterity Vest, -6, 0 +Aged Brie, -14, 30 +Elixir of the Mongoose, -11, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -1, 0 +Backstage passes to a TAFKAL80ETC concert, -6, 0 +Backstage passes to a TAFKAL80ETC concert, -11, 0 +Conjured Mana Cake, -13, 0 + +-------- day 17 -------- +name, sellIn, quality ++5 Dexterity Vest, -7, 0 +Aged Brie, -15, 32 +Elixir of the Mongoose, -12, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -2, 0 +Backstage passes to a TAFKAL80ETC concert, -7, 0 +Backstage passes to a TAFKAL80ETC concert, -12, 0 +Conjured Mana Cake, -14, 0 + +-------- day 18 -------- +name, sellIn, quality ++5 Dexterity Vest, -8, 0 +Aged Brie, -16, 34 +Elixir of the Mongoose, -13, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -3, 0 +Backstage passes to a TAFKAL80ETC concert, -8, 0 +Backstage passes to a TAFKAL80ETC concert, -13, 0 +Conjured Mana Cake, -15, 0 + +-------- day 19 -------- +name, sellIn, quality ++5 Dexterity Vest, -9, 0 +Aged Brie, -17, 36 +Elixir of the Mongoose, -14, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -4, 0 +Backstage passes to a TAFKAL80ETC concert, -9, 0 +Backstage passes to a TAFKAL80ETC concert, -14, 0 +Conjured Mana Cake, -16, 0 + +-------- day 20 -------- +name, sellIn, quality ++5 Dexterity Vest, -10, 0 +Aged Brie, -18, 38 +Elixir of the Mongoose, -15, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -5, 0 +Backstage passes to a TAFKAL80ETC concert, -10, 0 +Backstage passes to a TAFKAL80ETC concert, -15, 0 +Conjured Mana Cake, -17, 0 + +-------- day 21 -------- +name, sellIn, quality ++5 Dexterity Vest, -11, 0 +Aged Brie, -19, 40 +Elixir of the Mongoose, -16, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -6, 0 +Backstage passes to a TAFKAL80ETC concert, -11, 0 +Backstage passes to a TAFKAL80ETC concert, -16, 0 +Conjured Mana Cake, -18, 0 + +-------- day 22 -------- +name, sellIn, quality ++5 Dexterity Vest, -12, 0 +Aged Brie, -20, 42 +Elixir of the Mongoose, -17, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -7, 0 +Backstage passes to a TAFKAL80ETC concert, -12, 0 +Backstage passes to a TAFKAL80ETC concert, -17, 0 +Conjured Mana Cake, -19, 0 + +-------- day 23 -------- +name, sellIn, quality ++5 Dexterity Vest, -13, 0 +Aged Brie, -21, 44 +Elixir of the Mongoose, -18, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -8, 0 +Backstage passes to a TAFKAL80ETC concert, -13, 0 +Backstage passes to a TAFKAL80ETC concert, -18, 0 +Conjured Mana Cake, -20, 0 + +-------- day 24 -------- +name, sellIn, quality ++5 Dexterity Vest, -14, 0 +Aged Brie, -22, 46 +Elixir of the Mongoose, -19, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -9, 0 +Backstage passes to a TAFKAL80ETC concert, -14, 0 +Backstage passes to a TAFKAL80ETC concert, -19, 0 +Conjured Mana Cake, -21, 0 + +-------- day 25 -------- +name, sellIn, quality ++5 Dexterity Vest, -15, 0 +Aged Brie, -23, 48 +Elixir of the Mongoose, -20, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -10, 0 +Backstage passes to a TAFKAL80ETC concert, -15, 0 +Backstage passes to a TAFKAL80ETC concert, -20, 0 +Conjured Mana Cake, -22, 0 + +-------- day 26 -------- +name, sellIn, quality ++5 Dexterity Vest, -16, 0 +Aged Brie, -24, 50 +Elixir of the Mongoose, -21, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -11, 0 +Backstage passes to a TAFKAL80ETC concert, -16, 0 +Backstage passes to a TAFKAL80ETC concert, -21, 0 +Conjured Mana Cake, -23, 0 + +-------- day 27 -------- +name, sellIn, quality ++5 Dexterity Vest, -17, 0 +Aged Brie, -25, 50 +Elixir of the Mongoose, -22, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -12, 0 +Backstage passes to a TAFKAL80ETC concert, -17, 0 +Backstage passes to a TAFKAL80ETC concert, -22, 0 +Conjured Mana Cake, -24, 0 + +-------- day 28 -------- +name, sellIn, quality ++5 Dexterity Vest, -18, 0 +Aged Brie, -26, 50 +Elixir of the Mongoose, -23, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -13, 0 +Backstage passes to a TAFKAL80ETC concert, -18, 0 +Backstage passes to a TAFKAL80ETC concert, -23, 0 +Conjured Mana Cake, -25, 0 + +-------- day 29 -------- +name, sellIn, quality ++5 Dexterity Vest, -19, 0 +Aged Brie, -27, 50 +Elixir of the Mongoose, -24, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -14, 0 +Backstage passes to a TAFKAL80ETC concert, -19, 0 +Backstage passes to a TAFKAL80ETC concert, -24, 0 +Conjured Mana Cake, -26, 0 + +-------- day 30 -------- +name, sellIn, quality ++5 Dexterity Vest, -20, 0 +Aged Brie, -28, 50 +Elixir of the Mongoose, -25, 0 +Sulfuras, Hand of Ragnaros, 0, 80 +Sulfuras, Hand of Ragnaros, -1, 80 +Backstage passes to a TAFKAL80ETC concert, -15, 0 +Backstage passes to a TAFKAL80ETC concert, -20, 0 +Backstage passes to a TAFKAL80ETC concert, -25, 0 +Conjured Mana Cake, -27, 0 + diff --git a/csharp/ApprovalTest.cs b/csharp/ApprovalTest.cs index 5931f934..8021ffba 100644 --- a/csharp/ApprovalTest.cs +++ b/csharp/ApprovalTest.cs @@ -1,5 +1,6 @@ using ApprovalTests; using ApprovalTests.Reporters; +using csharp.StrategyPatternExample; using NUnit.Framework; using System; using System.IO; @@ -22,5 +23,17 @@ namespace csharp String output = fakeoutput.ToString(); Approvals.Verify(output); } + + [Test] + public void ThirtyDays_StrategyPatternExample() + { + StringBuilder fakeoutput = new StringBuilder(); + Console.SetOut(new StringWriter(fakeoutput)); + Console.SetIn(new StringReader("a\n")); + + Program.Main(new string[] { typeof(GildedRoseStrategyPatternExample).Name }); + String output = fakeoutput.ToString(); + Approvals.Verify(output); + } } } diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index c60d97a0..e7a814fd 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -2,7 +2,7 @@ namespace csharp { - public class GildedRose + public class GildedRose : IGildedRoseApp { IList Items; public GildedRose(IList Items) @@ -21,6 +21,14 @@ namespace csharp if (Items[i].Name != "Sulfuras, Hand of Ragnaros") { Items[i].Quality = Items[i].Quality - 1; + + if (Items[i].Name == "Conjured Mana Cake") + { + if (Items[i].Quality > 0) + { + Items[i].Quality = Items[i].Quality - 1; + } + } } } } @@ -67,6 +75,14 @@ namespace csharp if (Items[i].Name != "Sulfuras, Hand of Ragnaros") { Items[i].Quality = Items[i].Quality - 1; + + if (Items[i].Name == "Conjured Mana Cake") + { + if (Items[i].Quality > 0) + { + Items[i].Quality = Items[i].Quality - 1; + } + } } } } diff --git a/csharp/GildedRoseTest.cs b/csharp/GildedRoseTest.cs index 911df1be..1025bede 100644 --- a/csharp/GildedRoseTest.cs +++ b/csharp/GildedRoseTest.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using csharp.StrategyPatternExample; +using NUnit.Framework; using System.Collections.Generic; namespace csharp @@ -7,12 +8,39 @@ namespace csharp public class GildedRoseTest { [Test] - public void foo() + public void ConjuredQuality() { - IList Items = new List { new Item { Name = "foo", SellIn = 0, Quality = 0 } }; + IList Items = new List { new Item { Name = Global.NAME_ITEM_CONJURED, 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("fixme", Items[0].Name); + Assert.AreEqual(6, Items[0].Quality); + + app.UpdateQuality(); + Assert.AreEqual(2, Items[0].Quality); } + + [Test] + public void ConjuredQuality_StrategyPatternExample() + { + IList Items = new List { new Item { Name = Global.NAME_ITEM_CONJURED, SellIn = 1, Quality = 8 } }; + GildedRoseStrategyPatternExample app = new GildedRoseStrategyPatternExample(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/IGildedRoseApp.cs b/csharp/IGildedRoseApp.cs new file mode 100644 index 00000000..35dd4d2a --- /dev/null +++ b/csharp/IGildedRoseApp.cs @@ -0,0 +1,9 @@ +using System; + +namespace csharp +{ + public interface IGildedRoseApp + { + void UpdateQuality(); + } +} diff --git a/csharp/Program.cs b/csharp/Program.cs index 4ae66100..885ee404 100644 --- a/csharp/Program.cs +++ b/csharp/Program.cs @@ -1,4 +1,5 @@ -using System; +using csharp.StrategyPatternExample; +using System; using System.Collections.Generic; namespace csharp @@ -8,37 +9,44 @@ namespace csharp public static void Main(string[] args) { 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 }, - // this conjured item does not work properly yet - new Item {Name = "Conjured Mana Cake", SellIn = 3, Quality = 6} + new Item {Name = Global.NAME_ITEM_CONJURED, SellIn = 3, Quality = 6} }; + + IGildedRoseApp app = null; - var app = new GildedRose(Items); - + if ((args.Length > 0) && (args[0] == typeof(GildedRoseStrategyPatternExample).Name)) + { + app = new GildedRoseStrategyPatternExample(Items); + } + else + { + app = new GildedRose(Items); + } for (var i = 0; i < 31; i++) { diff --git a/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs b/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs new file mode 100644 index 00000000..d917511d --- /dev/null +++ b/csharp/StrategyPatternExample/CategoryStrategiesFactory.cs @@ -0,0 +1,48 @@ +using csharp.StrategyPatternExample.Strategy; +using System; +using System.Collections.Generic; + +namespace csharp.StrategyPatternExample +{ + /// + /// This class is responsible for providing a category strategy to a given item. + /// + static internal class CategoryStrategiesFactory + { + #region Methods + + static public ICategoryStrategy GetCategoryStrategies(Item item) + { + switch (item.Name) + { + case Global.NAME_ITEM_AGED_BRIE: + return new OlderIsBetterStrategy(); + + case Global.NAME_ITEM_BACKSTAGE_PASSES: + return new CloseExpiredImproveQualityStrategy(new List() { + new CloseExpiredImproveQualityStrategy.NextExpiredCondition() + { + SellInLimit = 5, + Increment = 3 + }, + new CloseExpiredImproveQualityStrategy.NextExpiredCondition() + { + SellInLimit = 10, + Increment = 2 + } + }); + + case Global.NAME_ITEM_SULFURAS: + return new DoNothingStrategy(); + + case Global.NAME_ITEM_CONJURED: + return new TwiceFastDegradeQualityStrategy(); + + default: + return new NormalDegradeStrategy(); + } + } + + #endregion + } +} diff --git a/csharp/StrategyPatternExample/GildedRoseStrategyPatternExample.cs b/csharp/StrategyPatternExample/GildedRoseStrategyPatternExample.cs new file mode 100644 index 00000000..5f9204cc --- /dev/null +++ b/csharp/StrategyPatternExample/GildedRoseStrategyPatternExample.cs @@ -0,0 +1,39 @@ +using System.Linq; +using System.Collections.Generic; +using csharp.StrategyPatternExample; + +namespace csharp.StrategyPatternExample +{ + /// + /// GildedRose class refactored to adapt the strategy pattern without making a great refactor of main method of Program.cs. + /// + public class GildedRoseStrategyPatternExample : IGildedRoseApp + { + #region Variables + + private IList Items; + + #endregion + + #region Constructor + + public GildedRoseStrategyPatternExample(IList Items) + { + this.Items = Items.Select(i => new ItemWrapperContext(i)).ToList(); + } + + #endregion + + #region Methods + + public void UpdateQuality() + { + for (var i = 0; i < Items.Count; i++) + { + Items[i].UpdateQuality(); + } + } + + #endregion + } +} diff --git a/csharp/StrategyPatternExample/ICategoryStrategy.cs b/csharp/StrategyPatternExample/ICategoryStrategy.cs new file mode 100644 index 00000000..464ee381 --- /dev/null +++ b/csharp/StrategyPatternExample/ICategoryStrategy.cs @@ -0,0 +1,12 @@ +using System; + +namespace csharp.StrategyPatternExample +{ + /// + /// Interface that every category strategy will have to implement. + /// + interface ICategoryStrategy + { + void Update(Item item); + } +} diff --git a/csharp/StrategyPatternExample/ItemWrapperContext.cs b/csharp/StrategyPatternExample/ItemWrapperContext.cs new file mode 100644 index 00000000..08322a7b --- /dev/null +++ b/csharp/StrategyPatternExample/ItemWrapperContext.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; + +namespace csharp.StrategyPatternExample +{ + /// + /// Wrapper class to allow to create a context for every defined item and thus to be able to bind a strategy. + /// + public class ItemWrapperContext + { + #region Variables + + private Item _item; + private ICategoryStrategy strategy; + + #endregion + + #region Properties + + 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; } + } + + #endregion + + #region Constructor + + public ItemWrapperContext(Item item) + { + this._item = item; + this.strategy = CategoryStrategiesFactory.GetCategoryStrategies(item); + } + + #endregion + + #region Methods + + public void UpdateQuality() + { + strategy.Update(this._item); + } + + #endregion + } +} diff --git a/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs b/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs new file mode 100644 index 00000000..04d05877 --- /dev/null +++ b/csharp/StrategyPatternExample/Strategies/CloseExpiredImproveQualityStrategy.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; + +namespace csharp.StrategyPatternExample.Strategy +{ + /// + /// Implements the strategy; Increases in Quality as its SellIn value approaches. + /// + 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/DoNothingStrategy.cs b/csharp/StrategyPatternExample/Strategies/DoNothingStrategy.cs new file mode 100644 index 00000000..b6c813ac --- /dev/null +++ b/csharp/StrategyPatternExample/Strategies/DoNothingStrategy.cs @@ -0,0 +1,18 @@ +using System; + +namespace csharp.StrategyPatternExample.Strategy +{ + /// + /// Implements the strategy; Do nothing. No action is implemented. + /// + internal class DoNothingStrategy : ICategoryStrategy + { + #region Methods + + public void Update(Item item) + { + } + + #endregion + } +} diff --git a/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs b/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs new file mode 100644 index 00000000..1b505363 --- /dev/null +++ b/csharp/StrategyPatternExample/Strategies/NormalDegradeStrategy.cs @@ -0,0 +1,30 @@ +using System; + +namespace csharp.StrategyPatternExample.Strategy +{ + /// + /// Implements the strategy; SellIn and Quality values are lowered. + /// Main strategy for the most items. + /// + internal class NormalDegradeStrategy : ICategoryStrategy + { + #region Methods + + public void Update(Item item) + { + if (item.Quality > Global.MINIMUM_QUALITY) + { + item.Quality--; + } + + item.SellIn--; + + if (item.SellIn < 0 && item.Quality > Global.MINIMUM_QUALITY) + { + item.Quality--; + } + } + + #endregion + } +} diff --git a/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs b/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs new file mode 100644 index 00000000..bc55a0cf --- /dev/null +++ b/csharp/StrategyPatternExample/Strategies/OlderIsBetterStrategy.cs @@ -0,0 +1,29 @@ +using System; + +namespace csharp.StrategyPatternExample.Strategy +{ + /// + /// Implements the strategy; Increase in Quality the older it gets. + /// + internal class OlderIsBetterStrategy : ICategoryStrategy + { + #region Methods + + public void Update(Item item) + { + if (item.Quality < Global.MAXIMUM_QUALITY) + { + item.Quality++; + } + + item.SellIn--; + + if (item.SellIn < 0 && item.Quality < Global.MAXIMUM_QUALITY) + { + item.Quality++; + } + } + + #endregion + } +} diff --git a/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs b/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs new file mode 100644 index 00000000..2082f2ce --- /dev/null +++ b/csharp/StrategyPatternExample/Strategies/TwiceFastDegradeQualityStrategy.cs @@ -0,0 +1,36 @@ +using System; + +namespace csharp.StrategyPatternExample.Strategy +{ + /// + /// Implements the strategy; Items degrade in Quality twice as fast as normal items. + /// + internal class TwiceFastDegradeQualityStrategy : ICategoryStrategy + { + #region Methods + + public void Update(Item item) + { + int degrade = 2; + + item.SellIn--; + + if (item.SellIn < 0) + { + degrade = 4; + } + + if (item.Quality > Global.MINIMUM_QUALITY) + { + item.Quality -= degrade; + } + + if (item.Quality < Global.MINIMUM_QUALITY) + { + item.Quality = Global.MINIMUM_QUALITY; + } + } + + #endregion + } +} diff --git a/csharp/csharp.csproj b/csharp/csharp.csproj index 37813560..adf3ae16 100644 --- a/csharp/csharp.csproj +++ b/csharp/csharp.csproj @@ -1,5 +1,6 @@  + Debug @@ -62,11 +63,22 @@ + + + + + + + + + + + @@ -76,6 +88,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}. + + +