public void fulfillOrder(VendingMachine sodaVendingMach, PurchasePrice sodaPrice)
        {
            decimal moneyRemaining = payment;

            while (moneyRemaining >= sodaPrice.PriceDecimal && flavorOrder.Any())
            {
                // if the can was dispensed, subtract its price from the
                // money entered
                if (sodaVendingMach.DispenseCan(flavorOrder[0]))
                {
                    System.Diagnostics.Debug.WriteLine("{0} Can dispensed from initial order", flavorOrder[0]);
                    moneyRemaining -= sodaPrice.PriceDecimal;
                }
                // remove the request from the order whether the can was dispensed or not
                flavorOrder.RemoveAt(0);
            }
            foreach (Flavor soda in flavorOrder)
            {
                Console.WriteLine("Requested {0} soda could not be dispensed", soda);
            }
            if (moneyRemaining > 0M)
            {
                Console.WriteLine("Here is your {0:c} back", moneyRemaining);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            PurchasePrice sodaPrice = new PurchasePrice(0.35M);
            CanRack       sodaRack  = new CanRack();

            Console.WriteLine("Welcome to the .NET C# Soda Vending Machine");

            Boolean timeToExit = false;

            do
            {
                sodaRack.DisplayCanRack();
                Console.Write("Please insert {0:c} worth of coins: ", sodaPrice.PriceDecimal);

                decimal totalValueInserted = 0M;
                while (totalValueInserted < sodaPrice.PriceDecimal)
                {
                    // get the coin inserted
                    string coinNameInserted = Console.ReadLine().ToUpper();
                    Coin   coinInserted     = new Coin(coinNameInserted);
                    Console.WriteLine("You have inserted a {0} worth {1:c}", coinInserted, coinInserted.ValueOf);

                    // running total of the value of the coins inserted
                    totalValueInserted += coinInserted.ValueOf;
                    Console.WriteLine("Total value inserted is {0:c}", totalValueInserted);
                }

                // select a flavor of soda
                Boolean canDispensed = false;
                while (!canDispensed)
                {
                    Console.Write("What flavor would you like? : ");


                    // oooh, this looks like trouble. Why?
                    //Needs exception handling
                    Flavor flavor = new Flavor();

                    bool flavorFound = false;
                    while (flavorFound == false) //I think this will work for exception handling?
                    {
                        try
                        {
                            //ask the user for flavor
                            string flavorName = Console.ReadLine().ToUpper();

                            flavor      = FlavorOps.ToFlavor(flavorName); //If this parse is successful...
                            flavorFound = true;                           //... this WILL execute and get us out of the loop. Maybe?
                        }
                        catch (System.ArgumentException e)
                        {
                            Console.WriteLine($"{e.Message}" + " Please try again.");
                        }
                    }



                    if (!sodaRack.IsEmpty(flavor))
                    {
                        sodaRack.RemoveACanOf(flavor);
                        Console.WriteLine("Thanks, here is your can of {0}.", flavor);
                        canDispensed = true;
                    }
                    else
                    {
                        Console.WriteLine("We are out of {0}", flavor);
                    }
                }

                Console.Write("Exit the vending machine? (y/n): ");
                string response = Console.ReadLine();
                timeToExit = response.Trim().ToUpper().StartsWith("Y");
            } while (!timeToExit);
        }