Пример #1
0
        public void Run(VendingMachine vendingMachine)
        {
            Console.Clear();
            while (true)
            {
                vendingMachine.DisplayItems();
                Console.WriteLine($"1. Add Funds Balance: ${vendingMachine.Balance}");
                Console.WriteLine("2. Enter Product ID");
                Console.WriteLine("3. Finish vending");
                Console.WriteLine("Q  Quit");
                Console.Write("Choice: ");
                string choice = Console.ReadLine();
                Console.WriteLine();

                if (choice == "1")
                {
                    List <int> nums = new List <int>()
                    {
                        1, 2, 5, 10
                    };
                    int enteredBills = 0;

                    while (true)
                    {
                        Console.Write("Please Enter Dollar Amount 1,2,5 or 10 $");
                        try
                        {
                            enteredBills = int.Parse(Console.ReadLine());
                            break;
                        }
                        catch (FormatException)
                        {
                            Console.WriteLine("Choice not valid, please try again");
                        }
                    }

                    if (nums.Contains(enteredBills))
                    {
                        // both logs the activity to add funds and add funds to the balance
                        this.CreateLog(vendingMachine.Balance, vendingMachine.AddFunds(enteredBills), "FEED MONEY");
                        Console.Clear();
                    }
                    else
                    {
                        Console.WriteLine("Invalid dollar amount");
                    }
                }
                else if (choice == "2")
                {
                    Console.Write("Please enter LetterNumber item slot to purchase from: ");
                    string itemSlot = Console.ReadLine().ToUpper();
                    Console.WriteLine();

                    if (!vendingMachine.Stock.ContainsKey(itemSlot))
                    {
                        Console.WriteLine("Selection not valid\n");
                    }
                    else if (vendingMachine.Stock.ContainsKey(itemSlot) && vendingMachine.Stock[itemSlot].Price < vendingMachine.Balance && vendingMachine.Stock[itemSlot].Quantity > 0)
                    {
                        // calling select product will return the update balance to be logged, log transaction
                        this.CreateLog(vendingMachine.Balance, vendingMachine.SelectProduct(itemSlot), vendingMachine.LoggingInfo);
                        Console.WriteLine($"{vendingMachine.Stock[itemSlot].Name} purchased\n");
                    }
                    else if (vendingMachine.Stock[itemSlot].Price > vendingMachine.Balance)
                    {
                        Console.WriteLine("PLEASE ADD MORE FUNDS!\n");
                    }
                    else if (vendingMachine.Stock[itemSlot].Quantity == 0)
                    {
                        Console.WriteLine("ITEM SOLD OUT!\n");
                    }
                }
                else if (choice == "3")
                {
                    decimal beginningBalance = vendingMachine.Balance;
                    decimal endingBalance;

                    // makes change on remaining balance
                    Console.WriteLine($"Here is the remaining balance of ${vendingMachine.Balance}");
                    Console.WriteLine(vendingMachine.MakeChange());

                    endingBalance = vendingMachine.Balance;

                    // create log with starting balance, final balance $0, and message
                    this.CreateLog(beginningBalance, endingBalance, "GIVE CHANGE");

                    Console.WriteLine();
                    Console.WriteLine("Time to mange!");
                    Console.WriteLine(vendingMachine.ConsumeItems());
                    vendingMachine.Cart.Clear();
                }
                else if (choice.ToLower() == "q")
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Choice Not Valid");
                    Console.WriteLine("Please try again");
                    Console.WriteLine();
                }
            }
        }