public VendingMachineItem BuyItem(string code, TransactionSession session) { VendingMachineItem wantedItem = null; foreach (VendingMachineItem item in items) { if (item.ItemCode == code) { decimal price = item.Price; session.Deduct(price); item.NumberInStock--; wantedItem = item; } } return(wantedItem); }
public bool HaveEnoughToBuy(string code, TransactionSession session) { bool hasEnough = false; foreach (VendingMachineItem item in items) { if (item.ItemCode == code) { if (session.Balance >= item.Price) { hasEnough = true; } } } return(hasEnough); }
public void TransactionMenu() { TransactionSession session = new TransactionSession(); bool validTransInput = false; while (!validTransInput) { Console.WriteLine("(1) Feed Money"); Console.WriteLine("(2) Select Product"); Console.WriteLine("(3) Finish Transaction"); Console.WriteLine($"Current Money Provided: ${session.Balance}"); Console.WriteLine(); Console.Write("Selection: "); string transInput = Console.ReadLine(); switch (transInput) { case "1": // Feed Money Console.Clear(); Console.Write("Please enter an accepted dollar amount (1, 2, 5, 10): "); decimal billValue = 0.0M; string inputFeed = Console.ReadLine(); while (!decimal.TryParse(inputFeed, out billValue)) { Console.Clear(); Console.WriteLine($"\'{inputFeed}\' is not an accepted dollar amount! Try again!"); Console.Write("Please enter an accepted dollar amount (1, 2, 5, 10): "); inputFeed = Console.ReadLine(); } while (!session.Deposit(billValue)) { Console.Clear(); Console.WriteLine($"\'{billValue}\' is not an accepted dollar amount! Try again!"); Console.Write("Please enter an accepted dollar amount (1, 2, 5, 10): "); billValue = decimal.Parse(Console.ReadLine()); } Console.Clear(); vendingMachine.WriteLineToLog("FEED MONEY:", billValue, session.Balance); // 10/01/2018 12:00:00 PM FEED MONEY: $5.00 $5.00 // Date Time "FEED MONEY:" amount_added balance break; case "2": // Select Product Console.Clear(); Display(); Console.Write("Select an item code: "); string input = Console.ReadLine(); string inputCode = input.ToUpper(); if (!vendingMachine.IsValidCode(inputCode)) { Console.Clear(); Console.WriteLine($"\'{input}\' is not a valid item code"); Console.WriteLine(); } else { if (!vendingMachine.IsInStock(inputCode)) { Console.Clear(); Console.WriteLine($"\'{inputCode}\' is out of stock"); Console.WriteLine(); } else { // buy the item if (vendingMachine.HaveEnoughToBuy(inputCode, session)) { decimal startingBalance = session.Balance; VendingMachineItem theItem = vendingMachine.BuyItem(inputCode, session); Console.Clear(); Console.WriteLine($"User obtained one (1) {theItem.Name}"); Console.WriteLine(); Console.WriteLine(theItem.Text); Console.WriteLine(); vendingMachine.WriteLineToLog($"{theItem.Name} {theItem.ItemCode}", startingBalance, session.Balance); } else { Console.Clear(); Console.WriteLine("Funds not sufficient ;("); Console.WriteLine(); } } } // 10/01/2018 12:00:20 PM Crunchie B4 $10.00 $8.50 // Date Time itemName itemCode balanceBefore balanceAfter break; case "3": // Finish Transaction Console.Clear(); if (session.Balance > 0) { decimal changeBack = session.Balance; string change = session.FinishTransaction(); Console.WriteLine("Change returned:"); Console.WriteLine(); Console.WriteLine(change); Console.WriteLine(); vendingMachine.WriteLineToLog("GIVE CHANGE:", changeBack, session.Balance); } validTransInput = true; // 10/01/2018 12:01:35 PM GIVE CHANGE: $7.50 $0.00 // Date Time "GIVE CHANGE:" balanceBefore balanceAfter break; default: // If input is not 1, 2, or 3 Console.Clear(); Console.WriteLine($"\'{transInput}\' is not a valid selection dammit! Try again!"); Console.WriteLine(); Console.WriteLine("(1) Feed Money"); Console.WriteLine("(2) Select Product"); Console.WriteLine("(3) Finish Transaction"); Console.WriteLine($"Current Money Provided: ${session.Balance}"); transInput = Console.ReadLine(); break; } } }