using System.Collections.Generic;
public static void Main()
List<Fruit> fruits = new List<Fruit>();
fruits.Add(new Fruit(FRUIT_TYPE.APPLE, 0.79));
fruits.Add(new Fruit(FRUIT_TYPE.APPLE, 1));
fruits.Add(new Fruit(FRUIT_TYPE.MANGO, 3.5));
ITransaction fruitStand = new FruitStand();
fruitStand.printSalesData();
fruitStand.reset(fruits);
fruitStand.buy(FRUIT_TYPE.APPLE);
fruitStand.buy(FRUIT_TYPE.MANGO);
fruitStand.printSalesData();
enum FRUIT_TYPE {APPLE, BANANA, PEAR, MANGO, ORANGE, GUAVA, PLUM};
public Fruit(FRUIT_TYPE type, double price) {
public FRUIT_TYPE getType() { return type; }
public double getPrice() { return price; }
void reset(List<Fruit> fruits);
bool buy(FRUIT_TYPE fruitType);
class FruitStand : ITransaction {
private double totalSales;
private Dictionary<FRUIT_TYPE, List<Fruit>> fruitTracker;
public void reset(List<Fruit> fruits) {
fruitTracker = new Dictionary<FRUIT_TYPE, List<Fruit>>();
initializeFruitInventory(fruits);
public bool buy(FRUIT_TYPE fruitType) {
List<Fruit> existingFruits = getFruitsByType(fruitType);
if(existingFruits != null && existingFruits.Count > 0) {
Fruit purchasedFruit = existingFruits[0];
existingFruits.RemoveAt(0);
totalSales += purchasedFruit.getPrice();
public void printSalesData() {
Console.WriteLine("================================");
Console.WriteLine(String.Format("Total Sales: {0:C}", totalSales));
Console.WriteLine("Quantity on Hand:");
foreach(FRUIT_TYPE type in FRUIT_TYPE.GetValues(typeof(FRUIT_TYPE))) {
int quantity = findQuantityOnHand(type);
Console.WriteLine(String.Format("{0}: {1}", type.ToString(), quantity));
Console.WriteLine("================================\n");
private int findQuantityOnHand(FRUIT_TYPE type) {
List<Fruit> specifiedFruits = getFruitsByType(type);
return specifiedFruits == null ? 0 : specifiedFruits.Count;
private void initializeFruitInventory(List<Fruit> startingFruits) {
if(startingFruits != null && startingFruits.Count > 0) {
foreach(Fruit fruit in startingFruits) {
List<Fruit> fruitInventory = getFruitsByType(fruit.getType());
if(fruitInventory == null) {
fruitInventory = new List<Fruit>();
fruitTracker.Add(fruit.getType(), fruitInventory);
fruitInventory.Add(fruit);
private List<Fruit> getFruitsByType(FRUIT_TYPE type) {
List<Fruit> specifiedFruits = null;
if(fruitTracker != null && fruitTracker.Count > 0) {
fruitTracker.TryGetValue(type, out specifiedFruits);