Skip to content

Instantly share code, notes, and snippets.

@aevitas
Created July 8, 2014 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aevitas/327983c188404a794e65 to your computer and use it in GitHub Desktop.
Save aevitas/327983c188404a794e65 to your computer and use it in GitHub Desktop.
public class Inventory
{
public Dictionary<GarmentColor, int> Stock { get; set; }
public Inventory()
{
Stock = new Dictionary<GarmentColor, int>();
}
public void Add(GarmentColor color, int amount)
{
if (Stock.ContainsKey(color))
{
Stock[color] = amount;
return;
}
Stock.Add(color, amount);
}
public void Sell(GarmentColor color, int amount)
{
if (!Stock.ContainsKey(color))
throw new Exception("There are no garments of color " + color + " in stock!");
var stockCount = Stock[color];
if (amount > stockCount)
throw new Exception("Insufficient stock to complete sale!");
}
public void TradeIn(GarmentColor oldGarment, GarmentColor newGarment)
{
if (!Stock.ContainsKey(newGarment) || Stock[newGarment] <= 0)
throw new Exception("Can't trade garments, garment trading in for isn't in stock!");
Stock[oldGarment]++;
Stock[newGarment]--;
}
}
public enum GarmentColor
{
Black,
Blue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment