Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PurchaseMenu"/> class.
 /// constructor for a purchase menu
 /// </summary>
 /// <param name="vm">vending machine using this menu</param>
 /// <param name="mm">the main menu creating this purchase menu</param>
 public PurchaseMenu(VendingMachine vm, MainMenu mm)
 {
     this.VM = vm;
     this.MM = mm;
 }
        public void Purchase(string slot, VendingMachine vendingMachine, List <VendingMachineItem> customer, MainMenu mainmenu)
        {
            VendingMachineItem vmi = vendingMachine.GetItemAtSlot(slot);

            // check if item is in stock - if not, return "out of stock"
            if (vendingMachine.GetQuantityRemaining(slot) == 0 || vmi == null)
            {
                Console.WriteLine();
                Console.Write("SOLD OUT!");
                mainmenu.ErrorBuzz();
                Console.WriteLine();
            }
            // check if user has enough money - if not return "insufficient funds"
            if (Balance < GetItemAtSlot(slot).Price)
            {
                Console.Clear();
                Console.WriteLine();
                Console.Write("INSUFFICIENT FUNDS!");
                mainmenu.ErrorBuzz();
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine();
                // return what item has been purchased
                Console.WriteLine($"Purchased {vendingMachine.GetItemAtSlot(slot).ItemName}");
                // subtract price of item from balance
                Balance -= (GetItemAtSlot(slot).Price);
                // add item to customer bin ( list )
                customer.Add(vendingMachine.GetItemAtSlot(slot));
                // remove purchased item from inventory
                Inventory[slot].RemoveAt(0);
            }
        }