Refactored hardcoded item names. Now there are consts in Global class which define them.

This commit is contained in:
israel 2017-10-20 12:28:44 +01:00
parent 20538bf65c
commit e333daef74
12 changed files with 153 additions and 100 deletions

View File

@ -1,26 +0,0 @@
using NUnit.Framework;
using System.Collections.Generic;
namespace csharp
{
[TestFixture]
public class ConjuredGildedRoseTest
{
[Test]
public void Quality()
{
IList<Item> Items = new List<Item> { 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);
}
}
}

View File

@ -14,5 +14,22 @@ namespace csharp
app.UpdateQuality(); app.UpdateQuality();
Assert.AreNotEqual("fixme", Items[0].Name); Assert.AreNotEqual("fixme", Items[0].Name);
} }
[Test]
public void ConjuredQuality()
{
IList<Item> Items = new List<Item> { 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);
}
} }
} }

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

View File

@ -11,30 +11,30 @@ namespace csharp
Console.WriteLine("OMGHAI!"); Console.WriteLine("OMGHAI!");
IList<Item> Items = new List<Item>{ IList<Item> Items = new List<Item>{
new Item {Name = "+5 Dexterity Vest", SellIn = 10, Quality = 20}, new Item {Name = Global.NAME_ITEM_PLUS5_DEXTERITY, SellIn = 10, Quality = 20},
new Item {Name = "Aged Brie", SellIn = 2, Quality = 0}, new Item {Name = Global.NAME_ITEM_AGED_BRIE, SellIn = 2, Quality = 0},
new Item {Name = "Elixir of the Mongoose", SellIn = 5, Quality = 7}, new Item {Name = Global.NAME_ITEM_ELIXIR_MONGOOSE, SellIn = 5, Quality = 7},
new Item {Name = "Sulfuras, Hand of Ragnaros", SellIn = 0, Quality = 80}, new Item {Name = Global.NAME_ITEM_SULFURAS, SellIn = 0, Quality = 80},
new Item {Name = "Sulfuras, Hand of Ragnaros", SellIn = -1, Quality = 80}, new Item {Name = Global.NAME_ITEM_SULFURAS, SellIn = -1, Quality = 80},
new Item new Item
{ {
Name = "Backstage passes to a TAFKAL80ETC concert", Name = Global.NAME_ITEM_BACKSTAGE_PASSES,
SellIn = 15, SellIn = 15,
Quality = 20 Quality = 20
}, },
new Item new Item
{ {
Name = "Backstage passes to a TAFKAL80ETC concert", Name = Global.NAME_ITEM_BACKSTAGE_PASSES,
SellIn = 10, SellIn = 10,
Quality = 49 Quality = 49
}, },
new Item new Item
{ {
Name = "Backstage passes to a TAFKAL80ETC concert", Name = Global.NAME_ITEM_BACKSTAGE_PASSES,
SellIn = 5, SellIn = 5,
Quality = 49 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; IGildedRoseApp app = null;

View File

@ -34,7 +34,7 @@ namespace csharp.StrategyPatternExample
{ {
List<ICategoryStrategy> listCategoryStrategies = new List<ICategoryStrategy>(); List<ICategoryStrategy> listCategoryStrategies = new List<ICategoryStrategy>();
if (item.Name == "Aged Brie") if (item.Name == Global.NAME_ITEM_AGED_BRIE)
{ {
listCategoryStrategies = new List<ICategoryStrategy>() listCategoryStrategies = new List<ICategoryStrategy>()
{ {
@ -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<ICategoryStrategy>() listCategoryStrategies = new List<ICategoryStrategy>()
{ {
new NextExpiredImproveQualityStrategy() new CloseExpiredImproveQualityStrategy(new List<CloseExpiredImproveQualityStrategy.NextExpiredCondition>() {
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<ICategoryStrategy>() listCategoryStrategies = new List<ICategoryStrategy>()
{ {
}; };
} }
else if (item.Name == "Conjured Mana Cake") else if (item.Name == Global.NAME_ITEM_CONJURED)
{ {
listCategoryStrategies = new List<ICategoryStrategy>() listCategoryStrategies = new List<ICategoryStrategy>()
{ {

View File

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

View File

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
namespace csharp.StrategyPatternExample.Strategy
{
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

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

View File

@ -2,18 +2,18 @@
namespace csharp.StrategyPatternExample.Strategy namespace csharp.StrategyPatternExample.Strategy
{ {
class NormalDegradeStrategy : ICategoryStrategy internal class NormalDegradeStrategy : ICategoryStrategy
{ {
public void Update(Item item) public void Update(Item item)
{ {
if (item.Quality > Global.MINIMUN_QUALITY) if (item.Quality > Global.MINIMUM_QUALITY)
{ {
item.Quality--; item.Quality--;
} }
item.SellIn--; item.SellIn--;
if (item.SellIn < 0 && item.Quality > Global.MINIMUN_QUALITY) if (item.SellIn < 0 && item.Quality > Global.MINIMUM_QUALITY)
{ {
item.Quality--; item.Quality--;
} }

View File

@ -2,18 +2,18 @@
namespace csharp.StrategyPatternExample.Strategy namespace csharp.StrategyPatternExample.Strategy
{ {
class OlderIsBetterStrategy : ICategoryStrategy internal class OlderIsBetterStrategy : ICategoryStrategy
{ {
public void Update(Item item) public void Update(Item item)
{ {
if (item.Quality < Global.MAXIMUN_QUALITY) if (item.Quality < Global.MAXIMUM_QUALITY)
{ {
item.Quality++; item.Quality++;
} }
item.SellIn--; item.SellIn--;
if (item.SellIn < 0 && item.Quality < Global.MAXIMUN_QUALITY) if (item.SellIn < 0 && item.Quality < Global.MAXIMUM_QUALITY)
{ {
item.Quality++; item.Quality++;
} }

View File

@ -2,7 +2,7 @@
namespace csharp.StrategyPatternExample.Strategy namespace csharp.StrategyPatternExample.Strategy
{ {
class TwiceFastDegradeQualityStrategy : ICategoryStrategy internal class TwiceFastDegradeQualityStrategy : ICategoryStrategy
{ {
public void Update(Item item) public void Update(Item item)
{ {
@ -15,14 +15,14 @@ namespace csharp.StrategyPatternExample.Strategy
degrade = 4; degrade = 4;
} }
if (item.Quality > Global.MINIMUN_QUALITY) if (item.Quality > Global.MINIMUM_QUALITY)
{ {
item.Quality -= degrade; 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;
} }
} }
} }

View File

@ -65,17 +65,16 @@
<Compile Include="ApprovalTest.cs" /> <Compile Include="ApprovalTest.cs" />
<Compile Include="StrategyPatternExample\GildedRoseStrategyPatternExample.cs" /> <Compile Include="StrategyPatternExample\GildedRoseStrategyPatternExample.cs" />
<Compile Include="GildedRose.cs" /> <Compile Include="GildedRose.cs" />
<Compile Include="ConjuredGildedRoseTest.cs" />
<Compile Include="GildedRoseTest.cs" /> <Compile Include="GildedRoseTest.cs" />
<Compile Include="IGildedRoseApp.cs" /> <Compile Include="IGildedRoseApp.cs" />
<Compile Include="Item.cs" /> <Compile Include="Item.cs" />
<Compile Include="StrategyPatternExample\CategoryStrategiesFactory.cs" /> <Compile Include="StrategyPatternExample\CategoryStrategiesFactory.cs" />
<Compile Include="StrategyPatternExample\Global.cs" /> <Compile Include="Global.cs" />
<Compile Include="StrategyPatternExample\ICategoryStrategy.cs" /> <Compile Include="StrategyPatternExample\ICategoryStrategy.cs" />
<Compile Include="StrategyPatternExample\ItemWrapperContext.cs" /> <Compile Include="StrategyPatternExample\ItemWrapperContext.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StrategyPatternExample\Strategies\NextExpiredImproveQualityStrategy.cs" /> <Compile Include="StrategyPatternExample\Strategies\CloseExpiredImproveQualityStrategy.cs" />
<Compile Include="StrategyPatternExample\Strategies\NormalDegradeStrategy.cs" /> <Compile Include="StrategyPatternExample\Strategies\NormalDegradeStrategy.cs" />
<Compile Include="StrategyPatternExample\Strategies\OlderIsBetterStrategy.cs" /> <Compile Include="StrategyPatternExample\Strategies\OlderIsBetterStrategy.cs" />
<Compile Include="StrategyPatternExample\Strategies\TwiceFastDegradeQualityStrategy.cs" /> <Compile Include="StrategyPatternExample\Strategies\TwiceFastDegradeQualityStrategy.cs" />