Пример #1
0
        /// <summary>
        /// Change must be inserted before being able to select an item.  Machine will check if there is enough change to make the purchase.
        /// </summary>
        public static void VendMode()
        {
            Items availableItems = new Items();

            availableItems = itemSelector.AvailableItems();

            Console.WriteLine("Please insert change. Q for quarter, D for dime, N for nickel, P for penny. Press enter when done");
            string        changeInput  = Console.ReadLine();
            List <Change> change       = new List <Change>();
            ChangeHolder  changeHolder = new ChangeHolder();
            decimal       inserted     = 0M;

            foreach (var character in changeInput)
            {
                switch (character)
                {
                case 'Q':
                    changeHolder.Quarter.quantity++;
                    inserted += changeHolder.Quarter.value;
                    break;

                case 'D':
                    changeHolder.Dime.quantity++;
                    inserted += changeHolder.Dime.value;
                    break;

                case 'N':
                    changeHolder.Nickel.quantity++;
                    inserted += changeHolder.Nickel.value;
                    break;

                case 'P':
                    changeHolder.Penny.quantity++;
                    inserted += changeHolder.Penny.value;
                    break;

                default:
                    break;
                }
            }

            int index = 0;

            foreach (Item item in availableItems.items)
            {
                if (item.quantity > 0)
                {
                    Console.WriteLine(index + " - " + item.Name + " - " + item.Price);
                }
                index++;
            }
            Console.WriteLine("X to shutdown or A to enter admin panel.");
            int selectedIndex = -1;

            while (selectedIndex == -1)
            {
                string input = Console.ReadLine();
                if (input == "X")
                {
                    return;
                }
                if (input == "A")
                {
                    adminPanelSelection();
                }
                else
                {
                    Int32.TryParse(input, out selectedIndex);
                    if (selectedIndex == -1)
                    {
                        Console.WriteLine("invalid selection, please try again");
                    }
                }
            }
            Item selectedItem = new Item();

            selectedItem = availableItems.items[selectedIndex];
            try
            {
                if (changeMaker.EnoughChange(inserted, selectedItem.Price))
                {
                    itemSelector.VendItem(selectedItem);
                    ChangeHolder changeGiven = new ChangeHolder();
                    changeGiven = changeMaker.MakeChange(inserted, selectedItem.Price);
                    Console.WriteLine("Change given");
                    Console.WriteLine("Quarters: " + changeGiven.Quarter.quantity.ToString());
                    Console.WriteLine("Dimes: " + changeGiven.Dime.quantity.ToString());
                    Console.WriteLine("Nickels: " + changeGiven.Nickel.quantity.ToString());
                    Console.WriteLine("Pennies: " + changeGiven.Penny.quantity.ToString());
                    changeMaker.LoadChange(changeHolder); //load the coins inserted into the change maker
                    Console.WriteLine("Item vended");
                }

                else
                {
                    Console.WriteLine("Not enough change.  Returning inserted change.");
                    changeHolder = new ChangeHolder();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            VendMode();
        }