gilded-rose-dotnet-core/GildedRose.cs

75 lines
2.3 KiB
C#
Raw Normal View History

2020-08-31 22:13:03 +00:00
using System;
using System.Collections.Generic;
using ApprovalUtilities.Utilities;
2020-08-31 17:32:41 +00:00
namespace csharpcore
{
public class GildedRose
{
2020-08-31 22:13:03 +00:00
private const int MaxItemQuality = 50;
2020-08-31 19:38:16 +00:00
private readonly IList<Item> _items;
2020-08-31 22:13:03 +00:00
public GildedRose(IList<Item> items) => _items = items;
public void UpdateQuality() => _items.ForEach(UpdateItemQuality);
2020-08-31 17:32:41 +00:00
2020-08-31 22:13:03 +00:00
private static void UpdateItemQuality(Item item)
2020-08-31 17:32:41 +00:00
{
2020-08-31 22:13:03 +00:00
switch (item.Name)
2020-08-31 17:32:41 +00:00
{
2020-08-31 22:13:03 +00:00
case "Sulfuras, Hand of Ragnaros":
return;
2020-08-31 17:32:41 +00:00
2020-08-31 22:13:03 +00:00
case "Aged Brie":
UpdateAgedBrie(item);
break;
2020-08-31 17:32:41 +00:00
2020-08-31 22:13:03 +00:00
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;
}
2020-08-31 17:32:41 +00:00
2020-08-31 22:13:03 +00:00
private static void UpdateConjuredCake(Item item)
{
// Decrease quality twice as fast as normal items
var qualityDecrement = item.SellIn > 0 ? 2 : 4;
2020-08-31 22:13:03 +00:00
item.Quality = Math.Max(item.Quality - qualityDecrement, 0);
}
2020-08-31 17:32:41 +00:00
2020-08-31 22:13:03 +00:00
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;
2020-08-31 17:32:41 +00:00
}
2020-08-31 22:13:03 +00:00
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);
2020-08-31 17:32:41 +00:00
}
}
}