This commit is contained in:
IsraelHernandezGonzalez 2017-10-20 12:52:38 +00:00 committed by GitHub
commit cb397f3cb9
19 changed files with 862 additions and 23 deletions

View File

@ -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

View File

@ -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

View File

@ -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);
}
}
}

View File

@ -2,7 +2,7 @@
namespace csharp
{
public class GildedRose
public class GildedRose : IGildedRoseApp
{
IList<Item> Items;
public GildedRose(IList<Item> 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;
}
}
}
}
}

View File

@ -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<Item> Items = new List<Item> { new Item { Name = "foo", SellIn = 0, Quality = 0 } };
IList<Item> Items = new List<Item> { 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<Item> Items = new List<Item> { 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);
}
}
}

19
csharp/Global.cs Normal file
View File

@ -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";
}
}

9
csharp/IGildedRoseApp.cs Normal file
View File

@ -0,0 +1,9 @@
using System;
namespace csharp
{
public interface IGildedRoseApp
{
void UpdateQuality();
}
}

View File

@ -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<Item> Items = new List<Item>{
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++)
{

View File

@ -0,0 +1,48 @@
using csharp.StrategyPatternExample.Strategy;
using System;
using System.Collections.Generic;
namespace csharp.StrategyPatternExample
{
/// <summary>
/// This class is responsible for providing a category strategy to a given item.
/// </summary>
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<CloseExpiredImproveQualityStrategy.NextExpiredCondition>() {
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
}
}

View File

@ -0,0 +1,39 @@
using System.Linq;
using System.Collections.Generic;
using csharp.StrategyPatternExample;
namespace csharp.StrategyPatternExample
{
/// <summary>
/// GildedRose class refactored to adapt the strategy pattern without making a great refactor of main method of Program.cs.
/// </summary>
public class GildedRoseStrategyPatternExample : IGildedRoseApp
{
#region Variables
private IList<ItemWrapperContext> Items;
#endregion
#region Constructor
public GildedRoseStrategyPatternExample(IList<Item> Items)
{
this.Items = Items.Select(i => new ItemWrapperContext(i)).ToList<ItemWrapperContext>();
}
#endregion
#region Methods
public void UpdateQuality()
{
for (var i = 0; i < Items.Count; i++)
{
Items[i].UpdateQuality();
}
}
#endregion
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace csharp.StrategyPatternExample
{
/// <summary>
/// Interface that every category strategy will have to implement.
/// </summary>
interface ICategoryStrategy
{
void Update(Item item);
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
namespace csharp.StrategyPatternExample
{
/// <summary>
/// Wrapper class to allow to create a context for every defined item and thus to be able to bind a strategy.
/// </summary>
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
}
}

View File

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
namespace csharp.StrategyPatternExample.Strategy
{
/// <summary>
/// Implements the strategy; Increases in Quality as its SellIn value approaches.
/// </summary>
internal class CloseExpiredImproveQualityStrategy : ICategoryStrategy
{
#region subclasses
public class NextExpiredCondition
{
/// <summary>
/// SellIn to check.
/// </summary>
public int SellInLimit { get; set; }
/// <summary>
/// Incremento to apply.
/// </summary>
public int Increment { get; set; }
}
#endregion
#region Variables
private IList<NextExpiredCondition> listConditions;
#endregion
#region Constructor
public CloseExpiredImproveQualityStrategy(IList<NextExpiredCondition> 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
}
}

View File

@ -0,0 +1,18 @@
using System;
namespace csharp.StrategyPatternExample.Strategy
{
/// <summary>
/// Implements the strategy; Do nothing. No action is implemented.
/// </summary>
internal class DoNothingStrategy : ICategoryStrategy
{
#region Methods
public void Update(Item item)
{
}
#endregion
}
}

View File

@ -0,0 +1,30 @@
using System;
namespace csharp.StrategyPatternExample.Strategy
{
/// <summary>
/// Implements the strategy; SellIn and Quality values are lowered.
/// Main strategy for the most items.
/// </summary>
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
}
}

View File

@ -0,0 +1,29 @@
using System;
namespace csharp.StrategyPatternExample.Strategy
{
/// <summary>
/// Implements the strategy; Increase in Quality the older it gets.
/// </summary>
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
}
}

View File

@ -0,0 +1,36 @@
using System;
namespace csharp.StrategyPatternExample.Strategy
{
/// <summary>
/// Implements the strategy; Items degrade in Quality twice as fast as normal items.
/// </summary>
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
}
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\NUnit3TestAdapter.3.8.0\build\net35\NUnit3TestAdapter.props" Condition="Exists('packages\NUnit3TestAdapter.3.8.0\build\net35\NUnit3TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -62,11 +63,22 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApprovalTest.cs" />
<Compile Include="StrategyPatternExample\GildedRoseStrategyPatternExample.cs" />
<Compile Include="GildedRose.cs" />
<Compile Include="GildedRoseTest.cs" />
<Compile Include="IGildedRoseApp.cs" />
<Compile Include="Item.cs" />
<Compile Include="StrategyPatternExample\CategoryStrategiesFactory.cs" />
<Compile Include="Global.cs" />
<Compile Include="StrategyPatternExample\ICategoryStrategy.cs" />
<Compile Include="StrategyPatternExample\ItemWrapperContext.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StrategyPatternExample\Strategies\CloseExpiredImproveQualityStrategy.cs" />
<Compile Include="StrategyPatternExample\Strategies\NormalDegradeStrategy.cs" />
<Compile Include="StrategyPatternExample\Strategies\DoNothingStrategy.cs" />
<Compile Include="StrategyPatternExample\Strategies\OlderIsBetterStrategy.cs" />
<Compile Include="StrategyPatternExample\Strategies\TwiceFastDegradeQualityStrategy.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
@ -76,6 +88,12 @@
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\NUnit3TestAdapter.3.8.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\NUnit3TestAdapter.3.8.0\build\net35\NUnit3TestAdapter.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@ -3,4 +3,5 @@
<package id="ApprovalTests" version="3.0.13" targetFramework="net452" />
<package id="ApprovalUtilities" version="3.0.13" targetFramework="net452" />
<package id="NUnit" version="3.8.1" targetFramework="net452" />
<package id="NUnit3TestAdapter" version="3.8.0" targetFramework="net452" />
</packages>