public VendingItem BuyItem(string userSelection) // buys the item, reducing balance by the item's cost and { // reducing the relevant inventory item by one. VendingItem product = inventory[userSelection][0]; decimal productCost = product.Cost; if (balance >= productCost) { VendingWriter vr = new VendingWriter(); // writing to/formatting log string recordTransaction = (inventory[userSelection][0].Name + " "); recordTransaction += userSelection + " $" + balance + " "; balance -= productCost; recordTransaction += "$" + balance; // more writing to the log vr.WritingAFile(recordTransaction); inventory[userSelection].RemoveAt(0); return(product); } return(null); }
/// <summary> /// Vends the chosen item, if there are sufficient funds /// </summary> /// <param name="item">The item to vend</param> /// <returns>Whether the item was vended</returns> public bool Vend(VendingItem item) { // Initialize output variable bool vending = false; // If there are sufficient funds, // if (item.Price <= this.DepositedFunds) { this.DepositedFunds -= item.Price; this.ItemsInTray.Add(item); vending = true; LogTransaction($"{item.Name} {item.Slot}", this.DepositedFunds + item.Price); UpdateAudit(item.Price, item.Name); this.Stock[item.Slot].Remove(item); } // Return whether the item was vended return(vending); }
public void Display() { while (true) { Console.WriteLine(); Console.WriteLine("(1) Feed Money"); Console.WriteLine("(2) Select Product"); Console.WriteLine("(3) Finish Transaction"); Console.WriteLine("Current Money Provided: $" + vm.Balance); string userInput = Console.ReadLine(); if (userInput == "1") { Console.WriteLine("Please enter $1, $2, $5, or $10: "); //How to handle if entered with $? Decimal.TryParse(Console.ReadLine(), out decimal cash); if (cash == 1.00M || cash == 2.00M || cash == 5.00M || cash == 10.00M) { Console.WriteLine("Money Provided: $" + cash); vm.AddMoney(cash); Console.WriteLine("Your new balance is: $" + vm.Balance); } else { Console.WriteLine("Sorry, that's an invalid amount."); } } else if (userInput == "2") { Console.WriteLine("Please enter your selection: "); string input = (Console.ReadLine()).ToUpper(); if (!vm.IsValidSlot(input)) { Console.WriteLine("Invalid product code. Please try again."); } else if (!vm.IsInStock(input)) { Console.WriteLine("Sorry, item is sold out"); } else { VendingItem userDesiredProduct = vm.GetItemAtSlot(input); decimal cost = userDesiredProduct.Cost; if (vm.Balance < cost) { Console.WriteLine("Please provide more cash"); } else { boughtItems.Add(vm.BuyItem(input)); Console.WriteLine("Dispensing..."); Console.WriteLine("Current Balance: $" + vm.Balance); } } } else if (userInput == "3") { Console.WriteLine("Current Balance: $" + vm.Balance); Console.WriteLine("Your change is " + vm.ReturnChange()); foreach (VendingItem item in boughtItems) { Console.WriteLine("Consuming item(s): " + item.MakeEatNoise()); } boughtItems.Clear(); } else { Console.WriteLine("Please try again."); } } }
/// <summary> /// /// </summary> /// <param name="itemName"></param> /// <param name="quantity"></param> public InventoryItem(VendingItem itemName, int quantity) { ItemName = itemName; Quantity = quantity; }