Ejemplo n.º 1
0
        /// <summary>
        /// Takes user input to add money to the vending machine
        /// </summary>
        public void AddMoney()
        {
            Console.Write("What do you want to add? In dollars ");
            double amount = -2;

            // string strAmnt = Console.ReadLine();
            while (true)
            {
                string strAmnt = Console.ReadLine();
                if (strAmnt.Contains('$'))
                {
                    strAmnt = strAmnt.Replace('$', '0');
                }
                if (!double.TryParse(strAmnt, out amount))
                {
                    Console.WriteLine("Please enter dollar amount");
                    continue;
                }
                if (amount < 0)
                {
                    Console.WriteLine(" Did you ever hear the tragedy of Darth Plagueis The Wise? I thought not. It’s not a story the Jedi would tell you. It’s a Sith legend. Darth Plagueis was a Dark Lord of the Sith, so powerful and so wise he could use the Force to influence the midichlorians to create life… He had such a knowledge of the dark side that he could even keep the ones he cared about from dying. The dark side of the Force is a pathway to many abilities some consider to be unnatural. He became so powerful… the only thing he was afraid of was losing his power, which eventually, of course, he did. Unfortunately, he taught his apprentice everything he knew, then his apprentice killed him in his sleep. Ironic. He could save others from death, but not himself.");
                    Console.WriteLine("Please enter dollar amount");
                }
                else
                {
                    _vm.AddMoneyToBalance(amount);
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public void RunVendingMachine()
        {
            Vendomatic500.VendomaticAscii();

            int  selectedChoice   = 0;
            bool validAnswer      = false;
            bool returnToMainMenu = true;

            while (returnToMainMenu)
            {
                Console.WriteLine("MAIN MENU");
                Console.WriteLine("Please select from the following:");
                Console.WriteLine("1. Display Vending Machine Items \n2. Purchase \n3. Turn Off Vending Machine");
                Console.WriteLine();

                while (!validAnswer)
                {
                    try
                    {
                        selectedChoice = int.Parse(Console.ReadLine());

                        if (selectedChoice == 1 || selectedChoice == 2 || selectedChoice == 3)
                        {
                            validAnswer = true;;
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }

                    catch (Exception)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Please make a valid selection: 1, 2 or 3.");
                        Console.WriteLine("1. Display Vending Machine Items \n2. Purchase \n3. Turn Off Vending Machine");
                        Console.WriteLine();
                    }
                }

                if (selectedChoice == 1)
                {
                    Console.WriteLine();
                    Console.WriteLine(vm.DisplayItems());
                    Console.WriteLine("Press any button to return to the Main Menu.");
                    Console.ReadLine();
                    Console.WriteLine();
                    validAnswer = false;
                }

                else if (selectedChoice == 2)
                {
                    returnToMainMenu = false;
                    bool returnToPurchaseMenu = true;

                    while (returnToPurchaseMenu)
                    {
                        Console.WriteLine();
                        Console.WriteLine("PURCHASE MENU");
                        Console.WriteLine("Please select from the following:");
                        Console.WriteLine("1. Feed Money \n2. Select Product \n3. Complete Transaction");
                        Console.WriteLine("Your current available balance is " + vm.Balance + ".");
                        Console.WriteLine();
                        validAnswer = false;

                        while (!validAnswer)
                        {
                            try
                            {
                                selectedChoice = int.Parse(Console.ReadLine());

                                if (selectedChoice == 1 || selectedChoice == 2 || selectedChoice == 3)
                                {
                                    validAnswer          = true;
                                    returnToPurchaseMenu = false;
                                }
                                else
                                {
                                    throw new Exception();
                                }
                            }

                            catch (Exception)
                            {
                                Console.WriteLine();
                                Console.WriteLine("Please make a valid selection: 1, 2 or 3.");
                                Console.WriteLine("1. Feed Money \n2. Select Product \n3. Complete Transaction");
                                Console.WriteLine();
                            }
                        }

                        if (selectedChoice == 1)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Please insert a $1, $2, $5, or $10 bill.");
                            Console.WriteLine("To add $1: select 1. \nTo add $2: select 2. \nTo add $5: select 5. \nTo add $10: select 10.");
                            Console.WriteLine();

                            int paymentAmount = 0;
                            validAnswer = false;

                            while (!validAnswer)
                            {
                                try
                                {
                                    paymentAmount = int.Parse(Console.ReadLine());

                                    if (paymentAmount == 1 || paymentAmount == 2 || paymentAmount == 5 || paymentAmount == 10)
                                    {
                                        validAnswer          = true;
                                        returnToPurchaseMenu = false;
                                    }
                                    else
                                    {
                                        throw new Exception();
                                    }
                                }

                                catch (Exception)
                                {
                                    Console.WriteLine();
                                    Console.WriteLine("Please make a valid selection: 1, 2, 5, or 10.");
                                    Console.WriteLine();
                                }
                            }

                            vm.AddMoneyToBalance(paymentAmount);
                            vm.FeedMoneyToVMLog(paymentAmount);
                            returnToPurchaseMenu = true;
                        }

                        else if (selectedChoice == 2)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Please enter product code.");
                            Console.WriteLine();
                            string slotNumber = " ";

                            validAnswer = false;

                            while (!validAnswer)
                            {
                                try
                                {
                                    slotNumber = Console.ReadLine().ToUpper();

                                    if (vm.VMContents.ContainsKey(slotNumber))
                                    {
                                        validAnswer          = true;
                                        returnToPurchaseMenu = false;
                                    }

                                    else
                                    {
                                        throw new Exception();
                                    }
                                }

                                catch (Exception)
                                {
                                    Console.WriteLine();
                                    Console.WriteLine("You have entered a non-existant product code. Returning to Purchase Menu.");
                                    Console.WriteLine("To view item list, complete transaction and return to Main Menu.");
                                    validAnswer          = true;
                                    returnToPurchaseMenu = true;
                                }
                            }

                            while (!returnToPurchaseMenu)
                            {
                                if (vm.VMContents[slotNumber].Quantity > 0)
                                {
                                    vm.DispenseItem(slotNumber);

                                    if (vm.Balance.IsNegative)
                                    {
                                        vm.Balance = vm.Balance.Plus(vm.VMContents[slotNumber].Price);
                                        vm.VMContents[slotNumber].Quantity += 1;
                                        Console.WriteLine();
                                        Console.WriteLine("Your current balance is too low to make this purchase. Please add more money or select a different product.");
                                        returnToPurchaseMenu = true;
                                    }

                                    else
                                    {
                                        Console.WriteLine();
                                        Console.WriteLine(vm.VMContents[slotNumber].ToString());
                                        returnToPurchaseMenu = true;
                                        vm.ItemDispensedToLog(slotNumber);
                                    }
                                }

                                else
                                {
                                    Console.WriteLine();
                                    Console.WriteLine($"{vm.DispenseItem(slotNumber)}");
                                    Console.WriteLine("Returning to Purchase Menu.");
                                    returnToPurchaseMenu = true;
                                }
                            }
                        }

                        else if (selectedChoice == 3)
                        {
                            Console.WriteLine();
                            vm.ChangeDispensedToLog();
                            Console.WriteLine(vm.DispenseChange());
                            Console.WriteLine();
                            Console.WriteLine("Returning to main menu.");
                            Console.WriteLine();
                            returnToMainMenu = true;
                            validAnswer      = false;
                        }
                    }
                }

                else if (selectedChoice == 3)
                {
                    vm.SalesReport();
                    Environment.Exit(0);
                }
            }
        }