/// <summary> /// Dispenses coints directly to customer and removes from register. /// </summary> /// <param name="amountToDispense">Value of change to dispense.</param> /// <param name="customer">Customer refernce to dispense coins unto.</param> public void DispenseCoins(double amountToDispense, Customer customer) { if (amountToDispense == 0) { return; } List <Coin> change = new List <Coin>(); // Set highest coins first in register CoinCalculator.OrderByValue(ref register); double changeValue = 0; foreach (Coin coin in register) { if (Math.Round(changeValue + coin.Value, 2) == amountToDispense) { changeValue += coin.Value; change.Add(coin); break; } else if (changeValue + coin.Value < amountToDispense) { changeValue += coin.Value; change.Add(coin); } } foreach (Coin coin in change) { int index = register.FindIndex(x => x.name == coin.name); register.RemoveAt(index); } customer.RecieveChange(ref change); }
/// <summary> /// Soda Machine construct that takes a list of coins and cans. Stores them in Soda Machine register and inventory respectively. Constructor calls to Order the coins by descending value. Initialize "available" soda via InitializeSodaSelection() method. /// </summary> /// <param name="coins">List of coins to start in Register.</param> /// <param name="cans">List of cans of soda to start in Inventory.</param> public SodaMachine(List <Coin> coins, List <Can> cans) { register = coins; inventory = cans; InitializeSodaSelection(); // Anytime an unknown amount of coins are inserted, the machine should auto order/sort the coins into their respective slots, first slot holding highest value of coin. CoinCalculator.OrderByValue(ref register); }
/// <summary> /// Used to initialize the number of coins in the Wallet class. Easy define number of each coin. /// </summary> /// <param name="numberOfQuarters">Number of Quarters to put in wallet.</param> /// <param name="numberOfDimes">Number of Dimes to put in wallet.</param> /// <param name="numberOfNickels">Number of Nickels to put in wallet.</param> /// <param name="numberOfPennies">Number of Pennies to put in wallet.</param> public void InitializeWalletCoins(int numberOfQuarters, int numberOfDimes, int numberOfNickels, int numberOfPennies) { for (int i = 0; i < numberOfQuarters; i++) { coins.Add(new Quarter()); } for (int i = 0; i < numberOfDimes; i++) { coins.Add(new Dime()); } for (int i = 0; i < numberOfNickels; i++) { coins.Add(new Nickel()); } for (int i = 0; i < numberOfPennies; i++) { coins.Add(new Penny()); } CoinCalculator.OrderByValue(ref coins); }
/// <summary> /// Accepts coins into the register, should be called after validation processes. /// </summary> /// <param name="insertedCoins">Coins inserted by customer.</param> public void AcceptPayment(List <Coin> insertedCoins) { register.InsertRange(0, insertedCoins); CoinCalculator.OrderByValue(ref register); // Reorder register with highest value coins first }