mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
reconstruction of Quality modification and units update
This commit is contained in:
parent
f19e7d6878
commit
7f109fe12f
28
csharp/Aged.cs
Normal file
28
csharp/Aged.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
internal class Aged : Item
|
||||
{
|
||||
public Aged(string name, int sellIn, int quality) : base(name, sellIn, quality)
|
||||
{
|
||||
TypeOfItem = ItemType.Aged;
|
||||
}
|
||||
|
||||
protected override void QualityModifier()
|
||||
{
|
||||
if (this.SellIn < 0) SetQuality(2);
|
||||
else SetQuality(1);
|
||||
}
|
||||
|
||||
protected override void SetQuality(int qualityModifier)
|
||||
{
|
||||
if (this.Quality + qualityModifier > 50) this.Quality = 50;
|
||||
else this.Quality += qualityModifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
49
csharp/BackstagePass.cs
Normal file
49
csharp/BackstagePass.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
internal class BackstagePass : Item
|
||||
{
|
||||
public BackstagePass(string name, int sellIn, int quality) : base(name, sellIn, quality)
|
||||
{
|
||||
TypeOfItem = ItemType.BackstagePass;
|
||||
}
|
||||
|
||||
protected override void QualityModifier()
|
||||
{
|
||||
if (this.SellIn < 0) this.Quality = 0;
|
||||
else SetQuality(SetQualityModifier());
|
||||
}
|
||||
|
||||
protected override void SetQuality(int qualityModifier)
|
||||
{
|
||||
if (this.Quality + qualityModifier > 50) this.Quality = 50;
|
||||
else this.Quality += qualityModifier;
|
||||
}
|
||||
|
||||
private int SetQualityModifier()
|
||||
{
|
||||
int temp = this.SellIn / 5;
|
||||
int result=1;
|
||||
switch(temp)
|
||||
{
|
||||
case int n when (n < 1):
|
||||
result = 3;
|
||||
break;
|
||||
case int n when(n < 2):
|
||||
result = 2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
22
csharp/Conjured.cs
Normal file
22
csharp/Conjured.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
internal class Conjured : Item
|
||||
{
|
||||
public Conjured(string name, int sellIn, int quality) : base(name, sellIn, quality)
|
||||
{
|
||||
TypeOfItem = ItemType.Conjured;
|
||||
}
|
||||
|
||||
protected override void QualityModifier()
|
||||
{
|
||||
if (this.SellIn >= 0) SetQuality(2);
|
||||
else SetQuality(4);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,109 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
public class GildedRose
|
||||
internal class GildedRose
|
||||
{
|
||||
IList<Item> Items;
|
||||
public GildedRose(IList<Item> Items)
|
||||
private List<IItem> Items;
|
||||
public GildedRose(List<IItem> items)
|
||||
{
|
||||
this.Items = Items;
|
||||
this.Items = items;
|
||||
}
|
||||
|
||||
public void UpdateQuality()
|
||||
{
|
||||
foreach (var item in Items)
|
||||
foreach (Item item in Items)
|
||||
{
|
||||
if (item.Name.ToLower().Contains("backstage passes")) { BackstagePassesDailyChange(item); continue; }
|
||||
else if (item.Name.ToLower().Contains("aged")) { AgingItemDailyChange(item); continue; }
|
||||
else if (item.Name.ToLower().Contains("conjured")) { ConjuredItemDailyChange(item); continue; }
|
||||
else if (!item.Name.ToLower().Contains("sulfuras")) NormalItemsDailyChange(item);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//Conjured Items quality drops twice faster than normal items quality
|
||||
private void ConjuredItemDailyChange(Item conjuredItem)
|
||||
{
|
||||
conjuredItem.SellIn--;
|
||||
if (conjuredItem.SellIn < 0)
|
||||
{
|
||||
if (conjuredItem.Quality == 0) return;
|
||||
else if (conjuredItem.Quality < 4) { conjuredItem.Quality = 0; return; }
|
||||
else conjuredItem.Quality -= 4;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (conjuredItem.Quality == 0) return;
|
||||
else if (conjuredItem.Quality < 2) { conjuredItem.Quality = 0; return; }
|
||||
else conjuredItem.Quality -= 2;
|
||||
}
|
||||
}
|
||||
//Items quality drops by quality rate each time day ends and if sellin < 0 drop rate is dbled
|
||||
private void NormalItemsDailyChange(Item normalItem)
|
||||
{
|
||||
normalItem.SellIn--;
|
||||
if (normalItem.SellIn < 0)
|
||||
{
|
||||
if (normalItem.Quality == 0) return;
|
||||
else if (normalItem.Quality < 2) { normalItem.Quality = 0; return; }
|
||||
else normalItem.Quality -= 2;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (normalItem.Quality == 0) return;
|
||||
else if (normalItem.Quality < 1) { normalItem.Quality = 0; return; }
|
||||
else normalItem.Quality--;
|
||||
}
|
||||
}
|
||||
|
||||
private void BackstagePassesDailyChange(Item pass)
|
||||
{
|
||||
pass.SellIn--;
|
||||
if (pass.SellIn < 0)
|
||||
{
|
||||
if (pass.Quality == 0) return;
|
||||
else pass.Quality = 0; return;
|
||||
}
|
||||
else if (pass.SellIn <= 5)
|
||||
{
|
||||
if (pass.Quality == 50) return;
|
||||
else if (pass.Quality >= 47) { pass.Quality = 50; return; }
|
||||
else pass.Quality += 3;
|
||||
return;
|
||||
}
|
||||
else if (pass.SellIn <= 10)
|
||||
{
|
||||
if (pass.Quality == 50) return;
|
||||
else if (pass.Quality >= 48) { pass.Quality = 50; return; }
|
||||
else pass.Quality += 2;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pass.Quality == 50) return;
|
||||
else pass.Quality++;
|
||||
}
|
||||
}
|
||||
|
||||
private void AgingItemDailyChange(Item agingItem)
|
||||
{
|
||||
agingItem.SellIn--;
|
||||
if (agingItem.SellIn < 0)
|
||||
{
|
||||
if (agingItem.Quality == 50) return;
|
||||
else if (agingItem.Quality >= 49) { agingItem.Quality = 50; return; }
|
||||
else agingItem.Quality += 2;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (agingItem.Quality == 50) return;
|
||||
else agingItem.Quality++;
|
||||
item.PassingDay();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,37 +1,35 @@
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
[TestFixture]
|
||||
public class GildedRoseTest
|
||||
{
|
||||
private IList<Item> Items = new List<Item> { new Item { Name = "", SellIn = 0, Quality = 0 } };
|
||||
|
||||
|
||||
[Test]
|
||||
public void foo()
|
||||
{
|
||||
IList<Item> Items = new List<Item> { new Item { Name = "foo", SellIn = 0, Quality = 0 } };
|
||||
List<IItem> Items = new List<IItem> { new Item("foo",0,0)};
|
||||
GildedRose app = new GildedRose(Items);
|
||||
app.UpdateQuality();
|
||||
Assert.AreEqual("fixme", Items[0].Name);
|
||||
//Assert.AreEqual("fixme", Items[0].Name);
|
||||
}
|
||||
/*
|
||||
[Test]
|
||||
public void ConjuredFood_SellInGreaterThanOrSameAs1_QualityLoweredBy2()
|
||||
{
|
||||
//Arrange
|
||||
Arrange
|
||||
IList<Item> Items = new List<Item> { new Item { Name = "Conjured Meat", SellIn = 1, Quality = 3 },
|
||||
new Item { Name = "conjured Fish", SellIn = 2, Quality = 3 },
|
||||
new Item { Name = "Conjured Water", SellIn = 1, Quality = 0 },
|
||||
new Item { Name = "Pie Conjured", SellIn = 1, Quality = 1 }};
|
||||
GildedRose app = new GildedRose(Items);
|
||||
|
||||
//Act
|
||||
Act
|
||||
app.UpdateQuality();
|
||||
|
||||
//Assert
|
||||
Assert
|
||||
Assert.AreEqual(1, Items[0].Quality);
|
||||
Assert.AreEqual(1, Items[1].Quality);
|
||||
Assert.AreEqual(0, Items[2].Quality);
|
||||
@ -41,36 +39,32 @@ namespace csharp
|
||||
[Test]
|
||||
public void ConjuredFood_SellInLessThanOrSameAs0_QualityLowerBy4()
|
||||
{
|
||||
//Arrange
|
||||
Arrange
|
||||
IList<Item> Items = new List<Item> { new Item { Name = "Conjured Meat", SellIn = -1, Quality = 3 },
|
||||
new Item { Name = "conjured Fish", SellIn = 0, Quality = 5 } };
|
||||
GildedRose app = new GildedRose(Items);
|
||||
|
||||
//Act
|
||||
Act
|
||||
app.UpdateQuality();
|
||||
|
||||
//Assert
|
||||
Assert
|
||||
Assert.AreEqual(0, Items[0].Quality);
|
||||
Assert.AreEqual(1, Items[1].Quality);
|
||||
}
|
||||
*/
|
||||
[TestCase("Conjured Meat", 1, 3, ExpectedResult = 1)]
|
||||
[TestCase("conjured Fish", 2, 3, ExpectedResult = 1)]
|
||||
[TestCase("Conjured Water", 1, 0, ExpectedResult = 0)]
|
||||
[TestCase("Pie Conjured", 1, 1, ExpectedResult = 0)]
|
||||
[TestCase("Conjured Meat", 0, 3, ExpectedResult = 0)]
|
||||
[TestCase("Conjured Water", -1, 5, ExpectedResult = 1)]
|
||||
public int ConjuredFood_LoweringQuality(string name,int sellIn, int quality)
|
||||
[TestCase("meat", 1, 3, ExpectedResult = 1)]
|
||||
[TestCase("meat", 1, 1, ExpectedResult = 0)]
|
||||
[TestCase("meat", 0, 3, ExpectedResult = 0)]
|
||||
[TestCase("meat", -1, 5, ExpectedResult = 1)]
|
||||
public int ConjuredFood_LoweringQuality(string name, int sellIn, int quality)
|
||||
{
|
||||
//arrange
|
||||
List<IItem> Items = new List<IItem> { new Conjured(name, sellIn, quality) };
|
||||
GildedRose app = new GildedRose(Items);
|
||||
Items[0].Name = name;
|
||||
Items[0].SellIn = sellIn;
|
||||
Items[0].Quality = quality;
|
||||
//act
|
||||
app.UpdateQuality();
|
||||
//assert
|
||||
return Items[0].Quality;
|
||||
return Items[0].GetQuality();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
14
csharp/IItem.cs
Normal file
14
csharp/IItem.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
internal interface IItem
|
||||
{
|
||||
void PassingDay();
|
||||
int GetQuality();
|
||||
}
|
||||
}
|
||||
@ -1,14 +1,49 @@
|
||||
namespace csharp
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
public class Item
|
||||
public class Item : IItem
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Name { get; }
|
||||
public int SellIn { get; set; }
|
||||
public int Quality { get; set; }
|
||||
internal ItemType TypeOfItem;
|
||||
|
||||
public Item(string name, int sellIn, int quality)
|
||||
{
|
||||
Name = name;
|
||||
SellIn = sellIn;
|
||||
Quality = quality;
|
||||
TypeOfItem = ItemType.Normal;
|
||||
}
|
||||
|
||||
protected virtual void QualityModifier()
|
||||
{
|
||||
if (this.SellIn >= 0) SetQuality(1);
|
||||
else SetQuality(2);
|
||||
|
||||
}
|
||||
|
||||
protected virtual void SetQuality(int qualityModifier)
|
||||
{
|
||||
if (this.Quality - qualityModifier < 0) this.Quality = 0;
|
||||
else this.Quality -= qualityModifier;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return this.Name + ", " + this.SellIn + ", " + this.Quality;
|
||||
}
|
||||
}
|
||||
|
||||
public void PassingDay()
|
||||
{
|
||||
this.SellIn--;
|
||||
QualityModifier();
|
||||
}
|
||||
|
||||
public int GetQuality()
|
||||
{
|
||||
return this.Quality;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
17
csharp/ItemType.cs
Normal file
17
csharp/ItemType.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
internal enum ItemType
|
||||
{
|
||||
Normal,
|
||||
BackstagePass,
|
||||
Legendary,
|
||||
Aged,
|
||||
Conjured
|
||||
}
|
||||
}
|
||||
22
csharp/Legendary.cs
Normal file
22
csharp/Legendary.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace csharp
|
||||
{
|
||||
internal class Legendary : Item
|
||||
{
|
||||
public Legendary(string name, int sellIn, int quality) : base(name, sellIn, quality)
|
||||
{
|
||||
TypeOfItem = ItemType.Legendary;
|
||||
}
|
||||
|
||||
protected override void QualityModifier()
|
||||
{
|
||||
//legendary items doesnt change!
|
||||
this.SellIn++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,32 +9,32 @@ 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 = "Backstage passes to a TAFKAL80ETC concert",
|
||||
SellIn = 15,
|
||||
Quality = 20
|
||||
},
|
||||
new Item
|
||||
{
|
||||
Name = "Backstage passes to a TAFKAL80ETC concert",
|
||||
SellIn = 10,
|
||||
Quality = 49
|
||||
},
|
||||
new Item
|
||||
{
|
||||
Name = "Backstage passes to a TAFKAL80ETC concert",
|
||||
SellIn = 5,
|
||||
Quality = 49
|
||||
},
|
||||
List<IItem> Items = new List<IItem>{
|
||||
new Item("+5 Dexterity Vest", 10, 20),
|
||||
new Aged("Aged Brie", 2, 0),
|
||||
new Item("Elixir of the Mongoose", 5, 7),
|
||||
new Legendary("Sulfuras, Hand of Ragnaros", 0, 80),
|
||||
new Legendary("Sulfuras, Hand of Ragnaros", -1, 80),
|
||||
new BackstagePass
|
||||
(
|
||||
"Backstage passes to a TAFKAL80ETC concert",
|
||||
15,
|
||||
20
|
||||
),
|
||||
new BackstagePass
|
||||
(
|
||||
"Backstage passes to a TAFKAL80ETC concert",
|
||||
10,
|
||||
40
|
||||
),
|
||||
new BackstagePass
|
||||
(
|
||||
"Backstage passes to a TAFKAL80ETC concert",
|
||||
5,
|
||||
49
|
||||
),
|
||||
// this conjured item does not work properly yet
|
||||
new Item {Name = "Conjured Mana Cake", SellIn = 3, Quality = 6}
|
||||
new Conjured("Conjured Mana Cake", 3, 6)
|
||||
};
|
||||
|
||||
var app = new GildedRose(Items);
|
||||
|
||||
@ -48,6 +48,12 @@
|
||||
<HintPath>packages\ApprovalUtilities.3.0.13\lib\net45\ApprovalUtilities.Net45.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
|
||||
<HintPath>packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Moq, Version=4.5.30.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
|
||||
<HintPath>packages\Moq.4.5.30\lib\net45\Moq.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="nunit.framework, Version=3.9.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
||||
<HintPath>packages\NUnit.3.9.0\lib\net45\nunit.framework.dll</HintPath>
|
||||
</Reference>
|
||||
@ -61,10 +67,16 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Aged.cs" />
|
||||
<Compile Include="ApprovalTest.cs" />
|
||||
<Compile Include="BackstagePass.cs" />
|
||||
<Compile Include="Conjured.cs" />
|
||||
<Compile Include="IItem.cs" />
|
||||
<Compile Include="ItemType.cs" />
|
||||
<Compile Include="GildedRose.cs" />
|
||||
<Compile Include="GildedRoseTest.cs" />
|
||||
<Compile Include="Item.cs" />
|
||||
<Compile Include="Legendary.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
<packages>
|
||||
<package id="ApprovalTests" version="3.0.13" targetFramework="net452" />
|
||||
<package id="ApprovalUtilities" version="3.0.13" targetFramework="net452" />
|
||||
<package id="Castle.Core" version="3.3.3" targetFramework="net452" />
|
||||
<package id="Moq" version="4.5.30" targetFramework="net452" />
|
||||
<package id="NUnit" version="3.9.0" targetFramework="net452" />
|
||||
<package id="NUnit3TestAdapter" version="3.9.0" targetFramework="net452" />
|
||||
</packages>
|
||||
Loading…
Reference in New Issue
Block a user