From 7f109fe12fe1a86f2ae8a006c1c023b34bc00c0a Mon Sep 17 00:00:00 2001 From: Ziemowit Kustra Date: Tue, 18 Oct 2022 23:15:15 +0200 Subject: [PATCH] reconstruction of Quality modification and units update --- csharp/Aged.cs | 28 +++++++++++ csharp/BackstagePass.cs | 49 +++++++++++++++++++ csharp/Conjured.cs | 22 +++++++++ csharp/GildedRose.cs | 102 +++------------------------------------ csharp/GildedRoseTest.cs | 38 ++++++--------- csharp/IItem.cs | 14 ++++++ csharp/Item.cs | 43 +++++++++++++++-- csharp/ItemType.cs | 17 +++++++ csharp/Legendary.cs | 22 +++++++++ csharp/Program.cs | 50 +++++++++---------- csharp/csharp.csproj | 12 +++++ csharp/packages.config | 2 + 12 files changed, 254 insertions(+), 145 deletions(-) create mode 100644 csharp/Aged.cs create mode 100644 csharp/BackstagePass.cs create mode 100644 csharp/Conjured.cs create mode 100644 csharp/IItem.cs create mode 100644 csharp/ItemType.cs create mode 100644 csharp/Legendary.cs diff --git a/csharp/Aged.cs b/csharp/Aged.cs new file mode 100644 index 00000000..d1d04683 --- /dev/null +++ b/csharp/Aged.cs @@ -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; + } + } +} diff --git a/csharp/BackstagePass.cs b/csharp/BackstagePass.cs new file mode 100644 index 00000000..e43590ed --- /dev/null +++ b/csharp/BackstagePass.cs @@ -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; + } + + } +} diff --git a/csharp/Conjured.cs b/csharp/Conjured.cs new file mode 100644 index 00000000..8e19c01e --- /dev/null +++ b/csharp/Conjured.cs @@ -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); + } + } +} diff --git a/csharp/GildedRose.cs b/csharp/GildedRose.cs index 3028c545..f8b7d0a2 100644 --- a/csharp/GildedRose.cs +++ b/csharp/GildedRose.cs @@ -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 Items; - public GildedRose(IList Items) + private List Items; + public GildedRose(List 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(); } } } diff --git a/csharp/GildedRoseTest.cs b/csharp/GildedRoseTest.cs index 56711e66..28a6458f 100644 --- a/csharp/GildedRoseTest.cs +++ b/csharp/GildedRoseTest.cs @@ -1,37 +1,35 @@ using NUnit.Framework; using System.Collections.Generic; +using Moq; namespace csharp { [TestFixture] public class GildedRoseTest { - private IList Items = new List { new Item { Name = "", SellIn = 0, Quality = 0 } }; - - [Test] public void foo() { - IList Items = new List { new Item { Name = "foo", SellIn = 0, Quality = 0 } }; + List Items = new List { 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 Items = new List { 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 Items = new List { 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 Items = new List { 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(); } } } diff --git a/csharp/IItem.cs b/csharp/IItem.cs new file mode 100644 index 00000000..fd6d3a7b --- /dev/null +++ b/csharp/IItem.cs @@ -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(); + } +} diff --git a/csharp/Item.cs b/csharp/Item.cs index c0174285..c15eb8c3 100644 --- a/csharp/Item.cs +++ b/csharp/Item.cs @@ -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; + } } } diff --git a/csharp/ItemType.cs b/csharp/ItemType.cs new file mode 100644 index 00000000..40b224b0 --- /dev/null +++ b/csharp/ItemType.cs @@ -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 + } +} diff --git a/csharp/Legendary.cs b/csharp/Legendary.cs new file mode 100644 index 00000000..5aca3320 --- /dev/null +++ b/csharp/Legendary.cs @@ -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++; + } + } +} diff --git a/csharp/Program.cs b/csharp/Program.cs index 36313e27..c8d8ede6 100644 --- a/csharp/Program.cs +++ b/csharp/Program.cs @@ -9,32 +9,32 @@ namespace csharp { 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 = "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 Items = new List{ + 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); diff --git a/csharp/csharp.csproj b/csharp/csharp.csproj index c5fec89b..5948b771 100644 --- a/csharp/csharp.csproj +++ b/csharp/csharp.csproj @@ -48,6 +48,12 @@ packages\ApprovalUtilities.3.0.13\lib\net45\ApprovalUtilities.Net45.dll True + + packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll + + + packages\Moq.4.5.30\lib\net45\Moq.dll + packages\NUnit.3.9.0\lib\net45\nunit.framework.dll @@ -61,10 +67,16 @@ + + + + + + diff --git a/csharp/packages.config b/csharp/packages.config index 63895ed1..970289c0 100644 --- a/csharp/packages.config +++ b/csharp/packages.config @@ -2,6 +2,8 @@ + + \ No newline at end of file