Ejemplo n.º 1
0
        // Exercise 01 - Vending Machine
        // Shawn Hopkins [email protected]

        static void Main(string[] args)
        {
            // Access app configuration from appsettings.json
            var config = Settings().Build();

            var paymentProcessor = new VendingMachine(int.Parse(config["CostOfSoda"]));

            Console.WriteLine("Welcome to the .NET Soda vending machine");
            Console.Write($"Please insert {paymentProcessor.CostOfSoda} cents: ");

            while (!paymentProcessor.IsAmountSufficient())
            {
                try
                {
                    paymentProcessor.GetEnteredAmount(Console.ReadLine());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


                Console.Write($"You have inserted {paymentProcessor.AmountSpent} cents\n");

                if (!paymentProcessor.IsAmountSufficient())
                {
                    Console.Write($"Please deposit {paymentProcessor.Balance()} cents more to purchase the soda: ");
                }
            }

            var msg = paymentProcessor.Balance() < 0 ? $"and {Math.Abs(paymentProcessor.Balance())} cents change" : string.Empty;

            Console.WriteLine($"Thanks. Here is your soda {msg}");
        }
Ejemplo n.º 2
0
        // Hopkins, Shawn, hopkinss
        // Exercise 02 - Vending Machine
        // [email protected]
        static void Main(string[] args)
        {
            var config = Settings().Build();

            // Get the default values for project from configuration file
            var vendingMachine = new VendingMachine(int.Parse(config["Price"]),
                                                    numberOfCans: int.Parse(config["MaxNumberOfCans"]));

            // show the contents of the vending machine in debug window
            vendingMachine.ShowVendingMachineStatus();

            Console.WriteLine("Welcome to the .NET Soda vending machine.\n");
            bool isVending = true;

            while (isVending)
            {
                // Select a soda flavor
                SodaFlavor soda = default;
                while (soda == default)
                {
                    Console.WriteLine("Enter a flavor of soda from the menu:");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    foreach (var v in vendingMachine.AvailableSodaFlavors())
                    {
                        Console.WriteLine($"\t{v}");
                    }
                    Console.ForegroundColor = ConsoleColor.White;

                    soda = Utility.ParseEnum <SodaFlavor>(Console.ReadLine());
                    if (soda == default)
                    {
                        Console.WriteLine($"You must enter a flavor available in the menu!\n\n");
                    }
                }

                // pay for the soda
                while (!vendingMachine.IsAmountSufficient())
                {
                    Console.Write($"Please insert {vendingMachine.PurchasePrice} cents (type 'q' to quit): ");
                    try
                    {
                        vendingMachine.GetEnteredAmount(Console.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetType() == typeof(ApplicationException))
                        {
                            Environment.Exit(0);
                        }
                        else
                        {
                            Console.WriteLine($"Sorry, {ex.Message}\n");
                        }
                    }
                    Console.Write($"You have inserted {vendingMachine.AmountSpent} cents\n");

                    if (!vendingMachine.IsAmountSufficient())
                    {
                        Console.Write($"Please deposit {vendingMachine.Balance()} cents more to purchase the soda: ");
                    }
                }

                // update the inventory
                try
                {
                    vendingMachine.ManageInventory(RackAction.RemoveACanOf, soda, 1);
                    var msg = vendingMachine.Balance() < 0 ? $"and {Math.Abs(vendingMachine.Balance())} cents change" : string.Empty;
                    vendingMachine.ResetBalance();
                    Console.WriteLine($"Thanks. Here is your soda {msg}\n\n");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Sorry, {ex.Message}\n");
                    if (ex.Message.Contains("empty"))
                    {
                        Console.Write($"Would you like to refill the {soda} soda in the vending machine? (y/n): ");
                        var resp = Console.ReadKey();

                        // Restock the selected soda
                        if (resp.Key == ConsoleKey.Y)
                        {
                            vendingMachine.ManageInventory(RackAction.AddACanOf, soda, vendingMachine.MaxInventory);
                            Console.WriteLine($"\nRestocked the {soda} soda...\n\n");
                        }
                        else
                        {
                            Console.Clear();
                        }
                    }
                }

                // display contents of vending machine to debug
                vendingMachine.ShowVendingMachineStatus();
            }
        }