/// <summary> /// If the selection is valid, in stock, and costs less than the current balance, /// reduce the inventory count by 1 and reduce the balance by the cost. /// </summary> /// <param name="slot">The item code, or "slot" in the vending machine.</param> /// <returns></returns> public VendableItems Purchase(string slot) { VendableItems item = null; if (!Inventory.ContainsKey(slot)) { throw new InvalidSlotException(); } if (Inventory[slot].AmountRemaining == 0) { throw new OutOfStockException(); } if (Balance < Inventory[slot].Cost) { throw new InsufficientFundsException(); } item = Inventory[slot]; item.SellOne(); Balance -= item.Cost; Log.RecordPurchase(slot, item.Name, Balance + item.Cost, Balance); return item; }
/// <summary> /// Displays vending machine inventory as a list. Items with no remaining /// stock are displayed as "SOLD OUT!" /// </summary> private void DisplayVendingItemsList() { Console.Clear(); string[] slots = VendoMatic500.Slots; foreach (string slot in slots) { VendableItems item = VendoMatic500.GetItemAtSlot(slot); if (item.AmountRemaining > 0) { Console.WriteLine($"{slot,-4} {item.Name,-19} ${item.Cost}"); } else { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine($"{slot,-4} {item.Name,-19} SOLD OUT!"); Console.ForegroundColor = ConsoleColor.White; } } Console.WriteLine(); }
/// <summary> /// Purchases the selected product, if valid. /// </summary> /// <param name="selection">The user input product code / "slot".</param> private void ProcessPurchaseOfSelectedProduct(string selection) { try { VendableItems item = VendoMatic500.Purchase(selection); if (item != null) { ResponsiveYumText += item.ConsumeMessage + "\n"; Console.WriteLine(); Console.WriteLine($"Dispensing {item.Name}..."); Console.WriteLine(); PauseOperation(); } } catch (VendingMachineException e) { Console.WriteLine(e.Message); Console.WriteLine(); Console.WriteLine("Press ENTER to continue"); Console.ReadLine(); } }
/// <summary> /// Displays vending machine inventory as a list. Items with no remaining /// stock are displayed as "SOLD OUT!" /// </summary> private void DisplayVendingItemsGrid() { Console.Clear(); string[] slots = VendoMatic500.Slots; Console.WriteLine("----------------------------------------------------------------------------------------"); foreach (char c in "ABCD".ToCharArray()) { for (int s = 1; s < 5; s++) { string address = c.ToString() + s.ToString(); VendableItems item = VendoMatic500.GetItemAtSlot(address); if (item.AmountRemaining > 0) { Console.Write($"| {address,-17}|"); } else { Console.Write("|"); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write($" {address,-17}"); Console.ForegroundColor = ConsoleColor.White; Console.Write("|"); } } Console.WriteLine(); for (int s = 1; s < 5; s++) { string address = c.ToString() + s.ToString(); VendableItems item = VendoMatic500.GetItemAtSlot(address); if (item.AmountRemaining > 0) { Console.Write($"| {item.Name,-19}|"); } else { Console.Write("|"); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write($" {item.Name,-19}"); Console.ForegroundColor = ConsoleColor.White; Console.Write("|"); } } Console.WriteLine(); for (int s = 1; s < 5; s++) { string address = c.ToString() + s.ToString(); VendableItems item = VendoMatic500.GetItemAtSlot(address); if (item.AmountRemaining > 0) { Console.Write($"| {item.Cost.ToString("C"),-18}|"); } else { Console.Write("|"); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write($" SOLD OUT! "); Console.ForegroundColor = ConsoleColor.White; Console.Write("|"); } } Console.WriteLine(); Console.WriteLine("----------------------------------------------------------------------------------------"); } Console.WriteLine(); }