//Cancle coin transaction public void CancelTransaction(Customer customer) { //soda machine returns coins from hopper in foreach (Coin coin in hopperIn.ToList()) { customer.wallet.coins.Add(coin); hopperIn.Remove(coin); } Console.WriteLine($"The soda machine register now contains {Verification.CountMoney(register),2:C2}"); customer.DisplayContents(customer.backpack); customer.DisplayContents(customer.wallet); }
//Display "state" of simulation. (how objects change throughout transaction process public void DisplayAllStats() { Console.WriteLine("\nPress enter to display all current simulation statistics: "); Console.ReadLine(); Console.Clear(); Console.WriteLine("Soda machine:" + $"\n{sodaMachine.inventory.Count} cans in inventory" + $"\n{sodaMachine.register.Count} coins in register totalling {Verification.CountMoney(sodaMachine.register):C2}" + $"\n{sodaMachine.CardPaymentBalance:C2} in card credits.\n"); Console.WriteLine("Customer:" + $"\nCard balance of {customer.wallet.card.AvailableFunds:C2}" + $"\nWallet contains {Verification.CountMoney(customer.wallet.coins):C2}"); customer.DisplayContents(customer.backpack); }
// member methods public void RunSimulation() { // Choose card or coin string paymentMethod = UserInterface.ChoosePaymentMethod(); string userSelection = "0"; if (paymentMethod == "1") { Console.Clear(); //display available funds customer.InsertPayment(customer.wallet.card); //pick soda userSelection = UserInterface.MakeSelection(sodaMachine, paymentMethod); //check funds are available bool canCompleteTransaction = sodaMachine.ProcessTransaction(userSelection, customer); if (canCompleteTransaction) { double sodaCost = Math.Round(SodaMachine.GetSodaCost(userSelection), 2); sodaMachine.CompleteTransaction(customer, sodaCost, userSelection); } else // Funds not available { sodaMachine.CancelTransaction(); } } else { //User can enter more money while (userSelection == "0") { //Insert coin into hopper customer.InsertPayment(sodaMachine); double moneyInHopper = Math.Round(Verification.CountMoney(sodaMachine.hopperIn), 2); // Count money in hopper Console.Clear(); Console.WriteLine($"Money inserted: {moneyInHopper:C2}\n"); // Display money in hopper //Choose soda OR enter more money userSelection = UserInterface.MakeSelection(sodaMachine, paymentMethod); } // Break once user chooses soda bool canCompleteTransaction = sodaMachine.ProcessTransaction(userSelection); if (canCompleteTransaction) // If adequate amount of money passed in { sodaMachine.CompleteTransaction(customer, userSelection); } else // If inadequate amount of money passed in { sodaMachine.CancelTransaction(customer); } } }
//Complete coin transaction public void CompleteTransaction(Customer customer, string userSelection) { //soda machine takes coins from hopper foreach (Coin coin in hopperIn.ToList()) { register.Add(coin); hopperIn.Remove(coin); } Console.WriteLine($"The soda machine register now contains {Verification.CountMoney(register),2:C2}"); //Soda machine puts soda in backpack customer.backpack.cans.Add(DispenseSoda(userSelection)); customer.DisplayContents(customer.backpack); //soda machine gives change foreach (Coin coin in hopperOut.ToList()) { customer.wallet.coins.Add(coin); hopperOut.Remove(coin); } customer.DisplayContents(customer.wallet); }
// member methods //Need to display customer contents (wallet) public void DisplayContents(Wallet wallet) { double amountInWallet = Math.Round(Verification.CountMoney(wallet.coins), 2); Console.WriteLine($"You have {amountInWallet:C2} in coins in your wallet."); int numberOfQuarters = 0; int numberOfDimes = 0; int numberOfNickels = 0; int numberOfPennies = 0; if (wallet.coins.Count == 0) { Console.WriteLine("You have no coins in your wallet."); } else { foreach (Coin coin in wallet.coins) { switch (coin.name) { case "Quarter": numberOfQuarters++; break; case "Dime": numberOfDimes++; break; case "Nickel": numberOfNickels++; break; case "Penny": numberOfPennies++; break; } } Console.WriteLine($"Your wallet contains {numberOfQuarters} quarters, {numberOfDimes} dimes, {numberOfNickels} nickels, and {numberOfPennies} pennies."); } }
// COIN TRANSACTIONS // //Process coin transaction public bool ProcessTransaction(string userInput) { bool canCompleteTransaction; //tally coins in hopper double moneyInHopper = Math.Round(Verification.CountMoney(hopperIn), 2); //get soda cost double sodaCost = Math.Round(GetSodaCost(userInput), 2); if (sodaCost > moneyInHopper) { Console.WriteLine("\nTransaction cannot be completed - inadequate funds."); canCompleteTransaction = false; } else if (sodaCost == moneyInHopper) { Console.WriteLine("\nTransaction complete, no change due!"); canCompleteTransaction = true; } else { //Calculate change for user double changeDue = Math.Round(moneyInHopper - sodaCost, 2); //Checks if exact change can be given to user bool canGiveExactChange = DispenseChange(changeDue); if (canGiveExactChange == true) { Console.WriteLine("\nTransaction complete, pleace collect your change!"); canCompleteTransaction = true; } else { Console.WriteLine("\nTransaction cannot be completed - exact change unavailable."); canCompleteTransaction = false; } } return(canCompleteTransaction); }