gilded-rose-dotnet-core/GildedRose.cs
Thibaud 023cf716b1 feat: implement conjured items
"Conjured" items degrade in Quality twice as fast as normal items
2020-09-01 00:26:25 +02:00

75 lines
2.3 KiB
C#

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) => _items = items;
public void UpdateQuality() => _items.ForEach(UpdateItemQuality);
private static void UpdateItemQuality(Item item)
{
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;
}
private static void UpdateConjuredCake(Item item)
{
// Decrease quality twice as fast as normal items
var qualityDecrement = item.SellIn > 0 ? 2 : 4;
item.Quality = Math.Max(item.Quality - qualityDecrement, 0);
}
private static void UpdateBackstagePasses(Item item)
{
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;
}
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);
}
}
}