diff --git a/csharpcore/GildedRose.cs b/csharpcore/GildedRose.cs index 0d019901..d005a302 100644 --- a/csharpcore/GildedRose.cs +++ b/csharpcore/GildedRose.cs @@ -4,86 +4,16 @@ namespace csharpcore { public class GildedRose { - IList Items; - public GildedRose(IList Items) + public GildedRose(List items) { - this.Items = Items; + Items = items; } + public List Items { get; } + public void UpdateQuality() { - for (var i = 0; i < Items.Count; i++) - { - if (Items[i].Name != "Aged Brie" && Items[i].Name != "Backstage passes to a TAFKAL80ETC concert") - { - if (Items[i].Quality > 0) - { - if (Items[i].Name != "Sulfuras, Hand of Ragnaros") - { - Items[i].Quality = Items[i].Quality - 1; - } - } - } - else - { - if (Items[i].Quality < 50) - { - Items[i].Quality = Items[i].Quality + 1; - - if (Items[i].Name == "Backstage passes to a TAFKAL80ETC concert") - { - if (Items[i].SellIn < 11) - { - if (Items[i].Quality < 50) - { - Items[i].Quality = Items[i].Quality + 1; - } - } - - if (Items[i].SellIn < 6) - { - if (Items[i].Quality < 50) - { - Items[i].Quality = Items[i].Quality + 1; - } - } - } - } - } - - if (Items[i].Name != "Sulfuras, Hand of Ragnaros") - { - Items[i].SellIn = Items[i].SellIn - 1; - } - - if (Items[i].SellIn < 0) - { - if (Items[i].Name != "Aged Brie") - { - if (Items[i].Name != "Backstage passes to a TAFKAL80ETC concert") - { - if (Items[i].Quality > 0) - { - if (Items[i].Name != "Sulfuras, Hand of Ragnaros") - { - Items[i].Quality = Items[i].Quality - 1; - } - } - } - else - { - Items[i].Quality = Items[i].Quality - Items[i].Quality; - } - } - else - { - if (Items[i].Quality < 50) - { - Items[i].Quality = Items[i].Quality + 1; - } - } - } - } + Items.ForEach(x => x.UpdateQuality()); } } } diff --git a/csharpcore/GildedRoseTest.cs b/csharpcore/GildedRoseTest.cs index aa64b0b5..e8733898 100644 --- a/csharpcore/GildedRoseTest.cs +++ b/csharpcore/GildedRoseTest.cs @@ -1,4 +1,5 @@ using Xunit; +using System.Linq; using System.Collections.Generic; namespace csharpcore @@ -6,12 +7,163 @@ namespace csharpcore public class GildedRoseTest { [Fact] - public void foo() + public void Once_the_sell_in_date_has_passed_quality_degrades_twice_as_fast() { - IList Items = new List { new Item { Name = "foo", SellIn = 0, Quality = 0 } }; - GildedRose app = new GildedRose(Items); - app.UpdateQuality(); - Assert.Equal("fixme", Items[0].Name); + var sut = new GildedRose(new List + { + new Item { Name = "Foo", SellIn = 0, Quality = 10 } + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x.Name == "Foo"); + + Assert.Equal(8, item.Quality); + } + + [Fact] + public void While_the_sell_in_date_has_not_passed_Quality_degrades_normally() + { + var sut = new GildedRose(new List + { + new Item { Name = "Foo", SellIn = 1, Quality = 10 } + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x.Name == "Foo"); + + Assert.Equal(9, item.Quality); + } + + [Fact] + public void The_quality_of_an_item_is_never_negative() + { + var sut = new GildedRose(new List + { + new Item { Name = "Foo", SellIn = 10, Quality = 0 } + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x.Name == "Foo"); + + Assert.Equal(0, item.Quality); + } + + [Fact] + public void Aged_Brie_actually_increases_in_quality_the_older_it_gets() + { + var sut = new GildedRose(new List + { + new AgedBrieItem { SellIn = 10, Quality = 5 }, + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x.Name == "Aged Brie"); + + Assert.Equal(6, item.Quality); + } + + [Fact] + public void The_quality_of_an_item_is_never_more_than_50() + { + var sut = new GildedRose(new List + { + new AgedBrieItem { SellIn = 10, Quality = 50 }, + new BackstagePassesItem { SellIn = 10, Quality = 50 }, + }); + + sut.UpdateQuality(); + var item1 = sut.Items.First(x => x is AgedBrieItem); + var item2 = sut.Items.First(x => x is BackstagePassesItem); + + Assert.Equal(50, item1.Quality); + Assert.Equal(50, item2.Quality); + } + + [Fact] + public void Sulfuras_never_has_to_be_sold() + { + var sut = new GildedRose(new List + { + new SulfurasItem { SellIn = 10, Quality = 20 }, + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x is SulfurasItem); + + Assert.Equal(10, item.SellIn); + } + + [Fact] + public void Sulfuras_never_decreases_in_quality() + { + var sut = new GildedRose(new List + { + new SulfurasItem { SellIn = 10, Quality = 20 }, + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x is SulfurasItem); + + Assert.Equal(20, item.Quality); + } + + [Fact] + public void Backstage_passes_increases_in_quality_by_2_when_SellIn_is_10_days_or_less() + { + var sut = new GildedRose(new List + { + new BackstagePassesItem { SellIn = 10, Quality = 20 }, + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x is BackstagePassesItem); + + Assert.Equal(22, item.Quality); + } + + [Fact] + public void Backstage_passes_increases_in_Quality_by_3_when_SellIn_is_5_days_or_less() + { + var sut = new GildedRose(new List + { + new BackstagePassesItem { SellIn = 5, Quality = 20 }, + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x is BackstagePassesItem); + + Assert.Equal(23, item.Quality); + } + + [Fact] + public void Backstage_passes_quality_drops_to_0_after_the_concert() + { + var sut = new GildedRose(new List + { + new BackstagePassesItem { SellIn = 0, Quality = 20 }, + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x is BackstagePassesItem); + + Assert.Equal(0, item.Quality); + } + + [Fact] + public void Conjured_items_degrade_twice_as_fast_as_normal_items() + { + var sut = new GildedRose(new List + { + new ConjuredItem { Name = "Foo", SellIn = 10, Quality = 20 }, + new ConjuredItem { Name = "Bar", SellIn = 0, Quality = 20 }, + }); + + sut.UpdateQuality(); + var item = sut.Items.First(x => x.Name == "Foo"); + var item2 = sut.Items.First(x => x.Name == "Bar"); + + Assert.Equal(18, item.Quality); + Assert.Equal(16, item2.Quality); } } } \ No newline at end of file diff --git a/csharpcore/Item.cs b/csharpcore/Item.cs index 7940eb84..70bdd8a1 100644 --- a/csharpcore/Item.cs +++ b/csharpcore/Item.cs @@ -1,9 +1,118 @@ -namespace csharpcore +using System; + +namespace csharpcore { public class Item { public string Name { get; set; } public int SellIn { get; set; } public int Quality { get; set; } + + public virtual void UpdateQuality() + { + Quality -= 1; + + SellIn -= 1; + + if (SellIn < 0) + { + Quality -= 1; + } + + if (Quality < 0) + { + Quality = 0; + } + } + } + + public class AgedBrieItem : Item + { + public AgedBrieItem() + { + Name = "Aged Brie"; + } + + public override void UpdateQuality() + { + Quality += 1; + + SellIn -= 1; + + if (SellIn < 0) + { + Quality += 1; + } + + if (Quality > 50) + { + Quality = 50; + } + } + } + + public class BackstagePassesItem : Item + { + public BackstagePassesItem() + { + Name = "Backstage passes to a TAFKAL80ETC concert"; + } + + public override void UpdateQuality() + { + Quality += 1; + + if (SellIn < 11) + { + Quality += 1; + } + + if (SellIn < 6) + { + Quality += 1; + } + + if (Quality > 50) + { + Quality = 50; + } + + SellIn -= 1; + + if (SellIn < 0) + { + Quality = 0; + } + } + } + + public class SulfurasItem : Item + { + public SulfurasItem() + { + Name = "Sulfuras, Hand of Ragnaros"; + } + + public override void UpdateQuality() { } + } + + public class ConjuredItem : Item + { + public override void UpdateQuality() + { + Quality -= 2; + + SellIn -= 1; + + if (SellIn < 0) + { + Quality -= 2; + } + + if (Quality < 0) + { + Quality = 0; + } + } } } diff --git a/csharpcore/Program.cs b/csharpcore/Program.cs index ebe4da4a..fe28d582 100644 --- a/csharpcore/Program.cs +++ b/csharpcore/Program.cs @@ -9,32 +9,32 @@ namespace csharpcore { Console.WriteLine("OMGHAI!"); - IList Items = new List{ + var Items = new List{ new Item {Name = "+5 Dexterity Vest", SellIn = 10, Quality = 20}, - new Item {Name = "Aged Brie", SellIn = 2, Quality = 0}, + new AgedBrieItem {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 + new SulfurasItem {Name = "Sulfuras, Hand of Ragnaros", SellIn = 0, Quality = 80}, + new SulfurasItem {Name = "Sulfuras, Hand of Ragnaros", SellIn = -1, Quality = 80}, + new BackstagePassesItem { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 15, Quality = 20 }, - new Item + new BackstagePassesItem { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 10, Quality = 49 }, - new Item + new BackstagePassesItem { Name = "Backstage passes to a TAFKAL80ETC concert", SellIn = 5, Quality = 49 }, // this conjured item does not work properly yet - new Item {Name = "Conjured Mana Cake", SellIn = 3, Quality = 6} + new ConjuredItem {Name = "Conjured Mana Cake", SellIn = 3, Quality = 6} }; var app = new GildedRose(Items);