This commit is contained in:
Thibaud Gasser 2020-09-01 00:13:03 +02:00
parent 75ac4f6263
commit b0713494a5
3 changed files with 97 additions and 78 deletions

View File

@ -1,90 +1,75 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using ApprovalUtilities.Utilities;
namespace csharpcore namespace csharpcore
{ {
public class GildedRose public class GildedRose
{ {
private const int MaxItemQuality = 50;
private readonly IList<Item> _items; private readonly IList<Item> _items;
public GildedRose(IList<Item> items) public GildedRose(IList<Item> items) => _items = items;
public void UpdateQuality() => _items.ForEach(UpdateItemQuality);
private static void UpdateItemQuality(Item item)
{ {
_items = items; switch (item.Name)
{
case "Sulfuras, Hand of Ragnaros":
return;
case "Aged Brie":
UpdateAgedBrie(item);
break;
case "Backstage passes to a TAFKAL80ETC concert":
UpdateBackstagePasses(item);
break;
case "Conjured Mana Cake":
UpdateConjuredCake(item);
break;
default:
// Decrease quality twice when sell date is passed
var qualityDecrement = item.SellIn > 0 ? 1 : 2;
item.Quality = Math.Max(item.Quality - qualityDecrement, 0);
break;
}
item.SellIn -= 1;
} }
public void UpdateQuality() private static void UpdateConjuredCake(Item item)
{ {
for (var i = 0; i < _items.Count; i++) // TODO
// Decrease quality twice when sell date is passed
var qualityDecrement = item.SellIn > 0 ? 1 : 2;
item.Quality = Math.Max(item.Quality - qualityDecrement, 0);
}
private static void UpdateBackstagePasses(Item item)
{
static int ComputeQualityIncrement(int daysRemaining)
{ {
if (_items[i].Name != "Aged Brie" && _items[i].Name != "Backstage passes to a TAFKAL80ETC concert") // "Infinite" increment when sell date is passed
{ if (daysRemaining <= 0) return int.MinValue;
if (_items[i].Quality > 0) if (daysRemaining <= 5) return 3;
{ if (daysRemaining <= 10) return 2;
if (_items[i].Name != "Sulfuras, Hand of Ragnaros") return 1;
{
_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;
}
}
}
} }
item.Quality = Math.Clamp(item.Quality + ComputeQualityIncrement(item.SellIn), 0, MaxItemQuality);
}
private static void UpdateAgedBrie(Item item)
{
// Increase quality twice when sell date is passed
var qualityIncrement = item.SellIn > 0 ? 1 : 2;
item.Quality = Math.Clamp(item.Quality + qualityIncrement, 0, MaxItemQuality);
} }
} }
} }

View File

@ -26,15 +26,17 @@ namespace csharpcore
new object[]{ -1, 1, 3 }, new object[]{ -1, 1, 3 },
}; };
[Fact] [Theory]
public void ShouldNotIncreaseQualityMoreThan50() [InlineData(1, 0)]
[InlineData(-1, -2)]
public void ShouldNotIncreaseQualityMoreThan50(int startSellInDays, int expectedSellInDays)
{ {
var item = new Item {Name = "Aged Brie", SellIn = 1, Quality = 50}; var item = new Item {Name = "Aged Brie", SellIn = startSellInDays, Quality = 50};
var app = new GildedRose(new List<Item> {item}); var app = new GildedRose(new List<Item> {item});
app.UpdateQuality(); app.UpdateQuality();
Assert.Equal("Aged Brie", item.Name); Assert.Equal("Aged Brie", item.Name);
Assert.Equal(0, item.SellIn); Assert.Equal(expectedSellInDays, item.SellIn);
Assert.Equal(50, item.Quality); Assert.Equal(50, item.Quality);
} }
} }

32
test/ConjuredTest.cs Normal file
View File

@ -0,0 +1,32 @@
using System.Collections.Generic;
using Xunit;
namespace csharpcore
{
// "Conjured" items degrade in Quality twice as fast as normal items
public class ConjuredTest
{
private readonly List<Item> _items;
private GildedRose _rose;
public ConjuredTest()
{
_items = new List<Item>();
_rose =new GildedRose(_items);
}
//[Fact]
public void ShouldDecreaseInQuality()
{
var item = GenerateItem(3, 2);
_items.Add(item);
_rose.UpdateQuality();
Assert.Equal(1, item.Quality);
Assert.Equal(1, item.SellIn);
}
public Item GenerateItem(int quality, int sellInDays)
=> new Item {Name = "Conjured Mana Cake", Quality = quality, SellIn = sellInDays};
}
}