public VendingMachineItem GetItemAtSlot(string slotId)
        {
            List <VendingMachineItem> itemWantedList = new List <VendingMachineItem>();
            List <VendingMachineItem> ourSlotDevice  = null;

            try
            {
                ourSlotDevice = Inventory[slotId];
                string   price     = ourSlotDevice[0].Price.ToString();
                string   name      = ourSlotDevice[0].Name;
                string[] nameArray = new string[] { name, price };
                this.slots = nameArray;


                itemWantedList = Inventory[slotId];

                if (itemWantedList[0] == null)
                {
                    throw new Exception();
                }

                else if (itemWantedList[0].Price <= curerntBalance && itemWantedList.Count > 0)
                {
                    VendingMachineItem item = itemWantedList[0];


                    curerntBalance -= itemWantedList[0].Price;
                    itemWantedList.RemoveAt(0);

                    return(item);
                }
                else if (itemWantedList[0].Price > curerntBalance)
                {
                    throw new Exception();
                }

                else
                {
                    return(null);
                }
            }

            catch (Exception ex)
            {
                if (ourSlotDevice == null)
                {
                    InvalidSlotIdException x = new InvalidSlotIdException("This slot does not exist", ex);
                    {
                        Console.Beep();

                        Tools.ColorfulWriteLine("This slot does not exist", ConsoleColor.Red);
                    }
                }

                else if (itemWantedList.Count == 0)
                {
                    Console.Beep();
                    OutOfStockException x = new OutOfStockException("This item is out of stock", ex);
                    Tools.ColorfulWriteLine("This item is out of stock", ConsoleColor.Red);
                }

                else if (itemWantedList[0].Price > curerntBalance)
                {
                    Console.Beep();
                    InsuffiecientFundsException x = new InsuffiecientFundsException("You do not have enough money", ex);
                    Tools.ColorfulWriteLine("You do not have enough money", ConsoleColor.Red);
                }


                return(null);
            }
        }
Example #2
0
            public void Display()
            {
                while (true)
                {
                    Console.WriteLine();
                    Console.WriteLine("(1) Feed Money");
                    Console.WriteLine("(2) Select Product");
                    Console.WriteLine("(3) Finish Transaction");
                    Console.WriteLine();
                    string input = Console.ReadLine();

                    if (input == "1")
                    {
                        try
                        {
                            Console.WriteLine("How many dollars would you like to insert?");
                            int fedMoney = Int32.Parse(Console.ReadLine());
                            logger.RecordDeposit(fedMoney, (VM.CurrentBalance + fedMoney));
                            VM.FeedMoney(fedMoney);
                        }
                        catch (FormatException)
                        {
                            Console.Beep();
                            Tools.ColorfulWriteLine("Please only enter a whole Dollar Value", ConsoleColor.Red);
                        }
                    }
                    else if (input == "2")
                    {
                        Console.WriteLine("Please select your product.");
                        Console.WriteLine();
                        string             slotId          = Console.ReadLine().ToUpper();
                        VendingMachineItem productSelected = VM.GetItemAtSlot(slotId);


                        if (productSelected != null)
                        {
                            totalProductsSelected.Add(productSelected);
                            logger.RecordPurchase(productSelected.Name, slotId, VM.CurrentBalance, VM.CurrentBalance - productSelected.Price);
                        }
                    }
                    else if (input == "3")
                    {
                        change = VM.returnChange();
                        Console.WriteLine("Your change is:");
                        Console.WriteLine(change.Quarters + " in quarters, " + change.Dimes + " in dimes, " + change.Nickels + " in nickels");
                        Console.WriteLine();
                        logger.RecordCompleteTransaction(change.TotalChange, VM.CurrentBalance);
                        for (int i = 0; i < totalProductsSelected.Count; i++)
                        {
                            Console.WriteLine(totalProductsSelected[i].Consume());
                        }

                        break;
                    }
                    else
                    {
                        Console.WriteLine("You did not enter a valid option?");
                    }

                    Console.WriteLine(VM.CurrentBalance.ToString("C"));
                }
            }