refactor
This commit is contained in:
parent
75ac4f6263
commit
b0713494a5
121
GildedRose.cs
121
GildedRose.cs
@ -1,90 +1,75 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ApprovalUtilities.Utilities;
|
||||
|
||||
namespace csharpcore
|
||||
{
|
||||
public class GildedRose
|
||||
{
|
||||
private const int MaxItemQuality = 50;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
item.SellIn -= 1;
|
||||
}
|
||||
|
||||
if (_items[i].SellIn < 6)
|
||||
private static void UpdateConjuredCake(Item item)
|
||||
{
|
||||
if (_items[i].Quality < 50)
|
||||
{
|
||||
_items[i].Quality = _items[i].Quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO
|
||||
// Decrease quality twice when sell date is passed
|
||||
var qualityDecrement = item.SellIn > 0 ? 1 : 2;
|
||||
item.Quality = Math.Max(item.Quality - qualityDecrement, 0);
|
||||
}
|
||||
|
||||
if (_items[i].Name != "Sulfuras, Hand of Ragnaros")
|
||||
private static void UpdateBackstagePasses(Item item)
|
||||
{
|
||||
_items[i].SellIn = _items[i].SellIn - 1;
|
||||
static int ComputeQualityIncrement(int daysRemaining)
|
||||
{
|
||||
// "Infinite" increment when sell date is passed
|
||||
if (daysRemaining <= 0) return int.MinValue;
|
||||
if (daysRemaining <= 5) return 3;
|
||||
if (daysRemaining <= 10) return 2;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (_items[i].SellIn < 0)
|
||||
item.Quality = Math.Clamp(item.Quality + ComputeQualityIncrement(item.SellIn), 0, MaxItemQuality);
|
||||
}
|
||||
|
||||
private static void UpdateAgedBrie(Item item)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Increase quality twice when sell date is passed
|
||||
var qualityIncrement = item.SellIn > 0 ? 1 : 2;
|
||||
item.Quality = Math.Clamp(item.Quality + qualityIncrement, 0, MaxItemQuality);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,15 +26,17 @@ namespace csharpcore
|
||||
new object[]{ -1, 1, 3 },
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void ShouldNotIncreaseQualityMoreThan50()
|
||||
[Theory]
|
||||
[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});
|
||||
|
||||
app.UpdateQuality();
|
||||
Assert.Equal("Aged Brie", item.Name);
|
||||
Assert.Equal(0, item.SellIn);
|
||||
Assert.Equal(expectedSellInDays, item.SellIn);
|
||||
Assert.Equal(50, item.Quality);
|
||||
}
|
||||
}
|
||||
|
32
test/ConjuredTest.cs
Normal file
32
test/ConjuredTest.cs
Normal 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};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user