mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
Refactored hardcoded item names. Now there are consts in Global class which define them.
This commit is contained in:
parent
20538bf65c
commit
e333daef74
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -14,5 +14,22 @@ namespace csharp
|
||||
app.UpdateQuality();
|
||||
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
19
csharp/Global.cs
Normal 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";
|
||||
}
|
||||
}
|
||||
@ -11,30 +11,30 @@ namespace csharp
|
||||
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
|
||||
},
|
||||
new Item {Name = "Conjured Mana Cake", SellIn = 3, Quality = 6}
|
||||
new Item {Name = Global.NAME_ITEM_CONJURED, SellIn = 3, Quality = 6}
|
||||
};
|
||||
|
||||
IGildedRoseApp app = null;
|
||||
|
||||
@ -34,7 +34,7 @@ namespace csharp.StrategyPatternExample
|
||||
{
|
||||
List<ICategoryStrategy> listCategoryStrategies = new List<ICategoryStrategy>();
|
||||
|
||||
if (item.Name == "Aged Brie")
|
||||
if (item.Name == Global.NAME_ITEM_AGED_BRIE)
|
||||
{
|
||||
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>()
|
||||
{
|
||||
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>()
|
||||
{
|
||||
};
|
||||
}
|
||||
else if (item.Name == "Conjured Mana Cake")
|
||||
else if (item.Name == Global.NAME_ITEM_CONJURED)
|
||||
{
|
||||
listCategoryStrategies = new List<ICategoryStrategy>()
|
||||
{
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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--;
|
||||
}
|
||||
|
||||
@ -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++;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,17 +65,16 @@
|
||||
<Compile Include="ApprovalTest.cs" />
|
||||
<Compile Include="StrategyPatternExample\GildedRoseStrategyPatternExample.cs" />
|
||||
<Compile Include="GildedRose.cs" />
|
||||
<Compile Include="ConjuredGildedRoseTest.cs" />
|
||||
<Compile Include="GildedRoseTest.cs" />
|
||||
<Compile Include="IGildedRoseApp.cs" />
|
||||
<Compile Include="Item.cs" />
|
||||
<Compile Include="StrategyPatternExample\CategoryStrategiesFactory.cs" />
|
||||
<Compile Include="StrategyPatternExample\Global.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\NextExpiredImproveQualityStrategy.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\CloseExpiredImproveQualityStrategy.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\NormalDegradeStrategy.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\OlderIsBetterStrategy.cs" />
|
||||
<Compile Include="StrategyPatternExample\Strategies\TwiceFastDegradeQualityStrategy.cs" />
|
||||
|
||||
Loading…
Reference in New Issue
Block a user