Created
December 16, 2014 13:21
-
-
Save AndyStewart/805b21a7e0b7cf973983 to your computer and use it in GitHub Desktop.
Functional c# Gilded Rose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace GildedRose | |
{ | |
public class GildedRose | |
{ | |
private static readonly Dictionary<string, Action<Item>> Products = new Dictionary<string, Action<Item>> | |
{ | |
{"Aged Brie", Product.Adjust(Adjusters.QualityAdjustedBy(1))}, | |
{"Backstage passes to a TAFKAL80ETC concert", Product.Adjust(Adjusters.Backstage)}, | |
{"Sulfuras, Hand of Ragnaros", Product.NoAdjustment}, | |
{"Conjured Mana Cake", Product.Adjust(Adjusters.QualityAdjustedBy(-2))}, | |
{"NORMAL ITEM", Product.Adjust(Adjusters.QualityAdjustedBy(-1))} | |
}; | |
public static void UpdateQuality(List<Item> items) | |
{ | |
items.ForEach(q => Products[q.Name](q)); | |
} | |
} | |
public class Product | |
{ | |
public static Action<Item> Adjust(Func<Item, int> adjustment) | |
{ | |
return item => { | |
item.SellIn = item.SellIn - 1; | |
item.Quality = item.Quality + adjustment(item); | |
item.Quality = Math.Max(item.Quality, 0); | |
item.Quality = Math.Min(item.Quality, 50); | |
}; | |
} | |
public static Action<Item> NoAdjustment = item => {}; | |
} | |
public class Adjusters | |
{ | |
public static int Backstage(Item item) | |
{ | |
if (item.SellIn < 0) | |
return -item.Quality; | |
if (item.SellIn < 5) | |
return +3; | |
if (item.SellIn < 10) | |
return +2; | |
return +1; | |
} | |
public static Func<Item, int> QualityAdjustedBy(int adjustment) | |
{ | |
return item => item.SellIn < 0 ? adjustment * 2 : adjustment; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment