Created
July 8, 2014 20:11
-
-
Save aevitas/327983c188404a794e65 to your computer and use it in GitHub Desktop.
This file contains 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
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