Skip to content

Instantly share code, notes, and snippets.

@mattflo
Created September 29, 2011 00:34
Show Gist options
  • Save mattflo/1249699 to your computer and use it in GitHub Desktop.
Save mattflo/1249699 to your computer and use it in GitHub Desktop.
Supermarket Kata
using System.Linq;
using NSpec;
namespace SuperMarketKata
{
class describe_Checkout : nspec
{
void given_a_an_empty_sku_list()
{
specify = () => Checkout("").Is(0);
}
void given_one_sku()
{
specify = () => Checkout("A").Is(50);
specify = () => Checkout("B").Is(30);
specify = () => Checkout("C").Is(20);
specify = () => Checkout("D").Is(15);
}
void given_multiple_skus()
{
specify = () => Checkout("CC").Is(40);
context["discounts"] = () =>
{
specify = () => Checkout("AAA").Is(130);
specify = () => Checkout("BB").Is(45);
specify = () => Checkout("AAAA").Is(180);
context["given discounted skus aren't scanned consecutively"] = () =>
{
specify = () => Checkout("ABAA").Is(160);
};
context["other crazy stuff"] = () =>
{
specify = () => Checkout("AAABB").Is(175);
specify = () => Checkout("AAAAAA").Is(260);
};
};
}
int Checkout(string skus)
{
skus = new string(skus.ToCharArray().OrderBy(i => i).ToArray());
skus = skus.Replace("AAA", "Z");
skus = skus.Replace("BB", "Y");
return skus.Sum(sku => GetSkuPrice(sku));
}
private int GetSkuPrice(char sku)
{
if (sku.Equals('A')) return 50;
if (sku.Equals('B')) return 30;
if (sku.Equals('C')) return 20;
if (sku.Equals('D')) return 15;
if (sku.Equals('Z')) return 130;
if (sku.Equals('Y')) return 45;
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment