/// <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> /// Main method for processing a transaction on the Soda Machine. It takes a customer, their soda choice and their insert coins. It first validats if the machine can process the transactions. If valid, it will accept and add the coins. Dispense a soda to the customer class and return change if required. /// </summary> /// <param name="customer">Customer using the Soda Machine</param> /// <param name="sodaChoice">A reference to the chosen soda.</param> /// <param name="insertedCoins">The list of coins inserted by the customer."</param> /// <returns></returns> public bool Transaction(Customer customer, string sodaChoiceName, List <Coin> insertedCoins) { // Machine calculates how much money is inserted. Machine/method holds inserted coins during validation before depositing in Register or returning to customer. double changeAmount = CoinCalculator.GetValueOfCoins(insertedCoins); if (ValidateTransaction(sodaChoiceName, ref changeAmount)) { // If machine validates coin amount, coins immediatly drop into the register. AcceptPayment(insertedCoins); customer.backpack.AddCan(DispenseSodaCan(sodaChoiceName)); //TODO: Refactor so Customer *Takes soda, and puts in backpack. UserInterface.DisplayCan(sodaChoiceName); UserInterface.ClearGreenScreen(); UserInterface.DisplayMainScreen(); UserInterface.WriteLiteralColor($"ͰGEBLPlease remember to\ntake your change.\nPress Any Key...", 74, 16); Console.ReadKey(true); DispenseCoins(changeAmount, customer); // TODO: Refactor so Customer *Recieves dispensed Change. UserInterface.ClearGreenScreen(); UserInterface.WriteLiteralColor($"ͰGEBLEnjoy your {sodaChoiceName}!\nHave a Great Day!\nPress Any Key...", 74, 16); Console.ReadKey(true); return(true); } // If machine fails to validate, coins inserted are immediately rerouted back to the customer. UserInterface.ClearGreenScreen(); UserInterface.WriteLiteralColor("ͰGEBLPlease remember to\ntake your change.\nPress Any Key...", 74, 16); DispenseCoins(insertedCoins, customer); // TODO: Refactor so Customer *Recieves dispensed Change. Console.ReadKey(); return(false); }
/// <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 }