예제 #1
0
        /// <summary>
        /// This Method gives the user back the remaining change (Works exactly the same as Exercise 19)
        /// </summary>
        public static void MoneyBackCalculation(InsertMoney change)
        {
            int[] changeBackArray = { 1000, 500, 100, 50, 20, 10, 5, 1 };
            int   totalMoney      = 0;

            foreach (int value in change.money)
            {
                totalMoney = totalMoney + value;
            }

            Program.DisplayMessage($"Your change is {totalMoney} \nHere's the change:\n");

            foreach (int value in changeBackArray)
            {
                int coinsBack = totalMoney / value;

                if (coinsBack >= 1)
                {
                    totalMoney -= (coinsBack * value);
                }
                Program.DisplayMessage($"{value} coins = {coinsBack}");
            }
            Program.DisplayMessage("Thank you for using my vending machine, hope it lived up to your expectations!", ConsoleColor.Green);
            Console.ReadKey(true);
        }
예제 #2
0
        /// <summary>
        /// The product menu to show the 3 main alternatives (Drinks, Food, Snacks)
        /// </summary>
        public void Menu(
            InsertMoney Change, List <IProducts> Products, Drinks CocaCola, Drinks Pepsi, Drinks Fanta,
            Food Cheeseburger, Food HotDog, Food Pizza, Snacks Twix, Snacks ChocolateBalls, Snacks Mars)
        {
            AffordProducts afford     = new AffordProducts();
            int            totalMoney = 0;
            bool           stayAlive  = true;

            //foreach (int value in Change.money)
            //{
            //    totalMoney = totalMoney + value;
            //}
            while (stayAlive)
            {
                Program.DisplayMessage(
                    "There's currently 3 options per category. Please choose wisely\n" +
                    "D - Drinks\n" +
                    "F - Food\n" +
                    "S - Snacks\n" +
                    "\n" +
                    "X - Go back\n"
                    );

                char productsChoice = char.ToUpper(Console.ReadKey(true).KeyChar);

                List <IProducts> ProductsValidation = new List <IProducts>();

                switch (productsChoice)
                {
                case 'D':
                    ProductDrinks(totalMoney, Change, afford, Products, ProductsValidation, CocaCola, Pepsi, Fanta);
                    break;

                case 'F':
                    ProductFood(totalMoney, Change, afford, Products, ProductsValidation, Cheeseburger, HotDog, Pizza);
                    break;

                case 'S':
                    ProductSnacks(totalMoney, Change, afford, Products, ProductsValidation, Twix, ChocolateBalls, Mars);
                    break;

                case 'X':
                    stayAlive = false;
                    break;

                default:
                    Program.DisplayMessage("Please type one of the given values. (D, F, S, X)", ConsoleColor.Red);
                    break;
                }
                Console.Clear();
            }
        }
        /// <summary>
        /// Checks if the user has enough money for the desired product.
        /// If yes - Sends out a message that the product has been used.
        /// If no - Sends out a message that the user couldn't afford the product
        /// </summary>
        public int AffordProduct(int totalMoney, List <IProducts> ProductsValidation, InsertMoney Change)
        {
            foreach (IProducts item in ProductsValidation)
            {
                if (totalMoney >= item.Cost)
                {
                    totalMoney -= item.Cost;
                    Change.money.Add(-item.Cost);
                    item.UseInfo();

                    return(totalMoney);
                }
                Program.DisplayMessage(
                    $"You couldn't afford the {item.Name}, " +
                    "please either pick something else or add more money to the vending machine", ConsoleColor.Red
                    );
            }
            return(totalMoney);
        }
예제 #4
0
        /// <summary>
        /// The product menu for drinks
        /// </summary>
        public void ProductDrinks(int totalMoney, InsertMoney Change, AffordProducts afford, List <IProducts> Products, List <IProducts> ProductsValidation,
                                  Drinks CocaCola, Drinks Pepsi, Drinks Fanta)
        {
            Console.Clear();

            Program.DisplayMessage("\nIt would seem you picked drinks! You must be thirsty!", ConsoleColor.Green);
            bool stayAlive = true;

            foreach (int value in Change.money)
            {
                totalMoney = totalMoney + value;
            }

            while (stayAlive)
            {
                Program.DisplayMessage("\nHere's youll get a list of drinks that's currently available. Please pick one");
                Program.DisplayMessage("\n(You select a drink by writing the first letter of the product you want)\n", ConsoleColor.Yellow);

                Program.DisplayMessage(
                    $"You currently have {totalMoney} remaining to spend \nX - Go back. " +
                    $"\nZ - remaining change back.\n"
                    );

                foreach (IProducts item in Products)
                {
                    if (item is Drinks)
                    {
                        item.ProductsInfo();
                    }
                }
                char choiceDrinks = char.ToUpper(Console.ReadKey(true).KeyChar);


                switch (choiceDrinks)
                {
                case 'C':
                    ProductsValidation.Add(CocaCola);
                    totalMoney = afford.AffordProduct(totalMoney, ProductsValidation, Change);
                    break;

                case 'P':
                    ProductsValidation.Add(Pepsi);
                    totalMoney = afford.AffordProduct(totalMoney, ProductsValidation, Change);
                    break;

                case 'F':
                    ProductsValidation.Add(Fanta);
                    totalMoney = afford.AffordProduct(totalMoney, ProductsValidation, Change);
                    break;

                case 'X':
                    stayAlive = false;
                    break;

                case 'Z':
                    Console.Clear();
                    MoneyBack.MoneyBackCalculation(Change);
                    stayAlive = false;
                    break;

                default:
                    Program.DisplayMessage("Invalid keypress, please try again", ConsoleColor.Red);
                    break;
                }
                ProductsValidation.Clear();

                if (stayAlive == true)
                {
                    Console.ReadKey(true);
                }
                Console.Clear();
            }
        }
        /// <summary>
        /// Main menu for the entire Assignment.
        /// </summary>
        public int UserMenu()
        {
            bool           stayAlive    = true;
            int            usefulNumber = 0;
            InsertMoney    change       = new InsertMoney();
            VendingMachine Machine      = new VendingMachine();

            do
            {
                try
                {
                    Program.DisplayMessage(
                        "\nPlease select a option:\n" +
                        "1: To insert money to the vending machine\n" +
                        "2: To look at the vending machines content\n" +
                        "3: To get your change back and exit the machine\n" +
                        "4: To exit the vending machine without getting your change back\n"
                        );

                    int choice = int.Parse(Console.ReadLine());

                    Console.Clear();

                    switch (choice)
                    {
                    case 1:
                        change.Insert();
                        break;

                    case 2:
                        Machine.VendingMachinery(change);
                        break;

                    case 3:
                        stayAlive = false;
                        MoneyBack.MoneyBackCalculation(change);
                        break;

                    case 4:
                        stayAlive = false;
                        break;

                    default:
                        throw new Exception();
                    }
                }
                catch (FormatException)
                {
                    Program.DisplayMessage("Please type a valid number!", ConsoleColor.Red);
                }
                catch (OverflowException)
                {
                    Program.DisplayMessage("Please type a value between 1-3", ConsoleColor.Red);
                }
                catch (ArgumentNullException)
                {
                    Program.DisplayMessage("Please try again", ConsoleColor.Red);
                }
            } while (stayAlive);
            return(usefulNumber);
        }
        /// <summary>
        /// Creates all the wares used in the vending machine.
        /// </summary>
        public void VendingMachinery(InsertMoney change)
        {
            Drinks CocaCola = new Drinks(
                "Coca Cola", 33, 12, "Cold beverage - perfect on warm summer days",
                "You crack open the cold Coca Cola", "sweet, bubbly taste of heaven"
                );

            Drinks Pepsi = new Drinks(
                "Pepsi", 33, 12, "Cold beverage - okay on warm summer days, inferior to Coca Cola",
                "You crack open the cold Pepsi", "sweet, bubbly taste of inferiority over Coca Cola"
                );

            Drinks Fanta = new Drinks(
                "Fanta", 33, 12, "Cold beverage - good on warm summer days, superior to Pepsi",
                "You crack open the cold Fanta", "sweet, bubbly taste of superior oranges");

            Food Cheeseburger = new Food(
                "Cheeseburger", 150, 29, "A filling burger - perfect if you ever feel hungry",
                "You unwrap the badboy", "filling taste of perfection"
                );

            Food Hotdog = new Food(
                "Hotdog", 90, 25, "A spicy hotdog - perfect for lunch",
                "You pour ketchup on it", "spicyness of the chili"
                );

            Food Pizza = new Food(
                "Hawaii pizza", 300, 85, "a heavenly pizza - perfect ANYTIME of the day",
                "You prepare your tastebuds", "godlike taste of an hawaiian pizza"
                );

            Snacks Twix = new Snacks(
                "Twix", 62, 10, "A biscuity snack - perfect to eat infront of the tv or computer",
                "You unwrap the paper", "wonderful caramel and chocolate melting together with the biscuit"
                );
            Snacks ChocolateBall = new Snacks(
                "Chocolate ball", 65, 11, "A chocolaty snack - filled with hopes and dreams",
                "You feel your mouth watering", "Chocolaty heaven in the form of a ball"
                );

            Snacks Mars = new Snacks(
                "Mars", 65, 10, "A Marsbar - for when you feel a little special",
                "You open the marsbar", "Chocolaty wonderfulness of the Mars"
                );

            ProductMenu productMenu = new ProductMenu();

            Products.Clear();

            Products.Add(CocaCola);
            Products.Add(Pepsi);
            Products.Add(Fanta);

            Products.Add(Cheeseburger);
            Products.Add(Hotdog);
            Products.Add(Pizza);

            Products.Add(Twix);
            Products.Add(ChocolateBall);
            Products.Add(Mars);

            productMenu.Menu(change, Products, CocaCola, Pepsi, Fanta, Cheeseburger, Hotdog, Pizza, Twix, ChocolateBall, Mars);
        }