public void SelectProduct() //handles both input and output for product selection, also decrements balance and inventory and writes log { Console.Write("Please select which slot number you would like to purchase: "); string input = Console.ReadLine(); if (myVendingMachine.SlotIndex(input) == 16) //handles improper inputs { Console.WriteLine("That is not a valid selection, Press Enter and try again:"); Console.ReadLine(); } if (ReturnQuantity(input) < 1) //checks to make sure empty slots never get consumed { Console.WriteLine("That item is all sold out, sorry. Press Enter to try again:"); Console.ReadLine(); } else { if (ReturnPrice(input) < myVendingMachine.Balance) {// dispense if (input.ToLower().StartsWith("a")) { Console.WriteLine("Crunch Crunch, Yum!"); Console.ReadLine(); } if (input.ToLower().StartsWith("b")) { Console.WriteLine("Munch Munch, Yum!"); Console.ReadLine(); } if (input.ToLower().StartsWith("c")) { Console.WriteLine("Glug Glug, Yum!"); Console.ReadLine(); } if (input.ToLower().StartsWith("d")) { Console.WriteLine("Chew Chew, Yum!"); Console.ReadLine(); } //decrement inventory myVendingMachine.ReduceInventory(input); // write to log WriteToLog($"{ReturnName(input)} {input}", myVendingMachine.Balance, -ReturnPrice(input)); // and lower balance myVendingMachine.DecreaseBalance(ReturnPrice(input)); } else //when price of item is > balance { Console.WriteLine("Sorry, you need more money. Press Enter and try again."); Console.ReadLine(); } } }
public void SelectProduct() { try { PrintInventory(); Console.Write("Please enter your product selection: "); string selection = Console.ReadLine().ToUpper(); decimal beginningBalance = vendingMachine.Balance; bool containsItem = vendingMachine.Inventory.ContainsKey(selection); bool enoughQuantity = (vendingMachine.Inventory[selection].QuantityRemaining > 0); bool enoughMoney = (beginningBalance >= vendingMachine.Inventory[selection].Price); Console.WriteLine(); if (!enoughMoney) { Console.WriteLine($"The item you selected costs ${vendingMachine.Inventory[selection].Price}\n" + $"Your available balance is ${vendingMachine.Balance}\n" + $"Please add additional money before attempting to make this purchase\n"); } else if (!enoughQuantity) { Console.WriteLine("This item is *****SOLD OUT*****\n"); } else { vendingMachine.DecreaseBalance(vendingMachine.Inventory[selection].Price); vendingMachine.DecreaseQuantity(selection); Console.WriteLine($"You purchased {vendingMachine.Inventory[selection].Name} for ${vendingMachine.Inventory[selection].Price}\n" + $"Your remaining balance is ${vendingMachine.Balance}\n" + $"{vendingMachine.Inventory[selection].Message}\n"); vendingMachine.SelectProductAuditLog(vendingMachine.Inventory[selection].Name, selection, beginningBalance); } } catch (KeyNotFoundException) { Console.WriteLine(); Console.WriteLine("That is not a valid selection\n"); } }