Exemplo n.º 1
0
        static void Main(string[] args)
        {
            int moneyPool = 0;

            string[] article = { "Kaffe", "Thè", "Cola", "Ost Sandwich", "Skink Sandwich", "Tonfisk-Sandwich", "Chips", "Popcorn", "Jordnötter" };

            Checkout checkout = new Checkout();

            //Init all products
            Coffee         coffee         = new Coffee(15);
            Tea            tea            = new Tea(10);
            Cola           cola           = new Cola(20);
            CheeseSandwich cheeseSandwich = new CheeseSandwich(20);
            HamSandwich    hamSandwich    = new HamSandwich(25);
            TunaSandwich   tunaSanwich    = new TunaSandwich(35);
            Chips          chips          = new Chips(30);
            Popcorn        popcorn        = new Popcorn(25);
            Peanuts        peanuts        = new Peanuts(25);

            Console.WriteLine("Välkommen!");                        //Greetings

            Console.WriteLine("Var god mata in pengar.");           //Put money into moneyPool

            do
            {
                var input = Console.ReadLine();
                try
                {
                    moneyPool = Int32.Parse(input);
                }
                catch (FormatException)
                {
                    Console.WriteLine($"You entered '{input}' and it needs to be '1,2,3...,n-1,n '");
                }
                Console.WriteLine($"Du har {moneyPool} kronor att handla för.");
                if (moneyPool < 1)
                {
                    Console.WriteLine("Lägg till pengar. Ange belopp: ");
                }
                else
                {
                    break;
                }
            } while (moneyPool < 1);


            Console.WriteLine("Meny");                              //Print menu
            Console.WriteLine("---------------------------------------------------------------");
            for (int i = 0; i < article.Length; i++)
            {
                Console.WriteLine($"Välj {i + 1}: för {article[i]}");
            }

            do     //Loop for multiple choice of items
            {
                Console.WriteLine("Var god välj vara (nummer 1-9) eller (0) för att avsluta: ");
                int    choice = 0;
                string input  = Console.ReadLine();

                try
                {
                    choice = Int32.Parse(input);
                }
                catch (FormatException)
                {
                    Console.WriteLine($"You entered '{input}' and it needs to be '1,2,3...,n-1,n '");
                }

                if (choice == 0)
                {
                    break;
                }
                switch (choice)
                {
                case 1:

                    moneyPool = coffee.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                case 2:

                    moneyPool = tea.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                case 3:

                    moneyPool = cola.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                case 4:

                    moneyPool = cheeseSandwich.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                case 5:

                    moneyPool = hamSandwich.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                case 6:

                    moneyPool = tunaSanwich.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                case 7:

                    moneyPool = chips.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                case 8:

                    moneyPool = popcorn.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                case 9:

                    moneyPool = peanuts.Purchase(moneyPool);
                    Console.WriteLine($"Du har {moneyPool} kronor kvar att handla för.");
                    break;

                default:

                    break;
                }
            } while (true);

            checkout.PayBackMoney(moneyPool);          //Refund or give change back
            Console.WriteLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int[] denominations = { 1, 5, 10, 20, 50, 100, 500, 1000 };
            int   i;
            char  choice = ' ', menuChoice = ' ';

            User user = new User();

            do
            {
                menuChoice = GetChoice(5, "\n\n1. Insert coins\n2. Purchase an item\n3. Consume an item\n4. Examine an item\n5. End\n");

                switch (menuChoice)
                {
                case '1':
                    Console.WriteLine("\n\nChoose 1 - 8 for 1, 5, 10, 20, 50, 100, 500 or 1000kr");
                    Console.WriteLine("End with an Enter");
                    do
                    {
                        choice = GetChoice(8, $"\rSum: {user.Sum}. Please add a coin into the slot: ");
                        if (choice != '\r')
                        {
                            i = choice - '1';
                            user.AddToSum(denominations[i]);
                        }
                    } while (choice != '\r');
                    break;

                case '2':
                    Console.WriteLine("\n\nChoose 1. Fanta, 2. Popcorn, 3. Sandwich or 4. To End");
                    Console.WriteLine("Press Enter to list items you have bought.");
                    do
                    {
                        choice = GetChoice(4, $"\rSum: {user.Sum}. Please choose an item to purchase: ");
                        switch (choice)
                        {
                        case '1':
                            Fanta fanta = new Fanta();
                            Console.WriteLine($"\nYou chose a Fanta for {fanta.Price}kr");
                            user.AddToSum(-fanta.Price);
                            user.Purchase("Fanta");
                            break;

                        case '2':
                            Popcorn popcorn = new Popcorn();
                            Console.WriteLine($"\nYou chose a Popcorn for {popcorn.Price}kr");
                            user.AddToSum(-popcorn.Price);
                            user.Purchase("Popcorn");
                            break;

                        case '3':
                            Sandwich sandwich = new Sandwich();
                            Console.WriteLine($"\nYou chose a Sandwich for {sandwich.Price}kr");
                            user.AddToSum(-sandwich.Price);
                            user.Purchase("Sandwich");
                            break;

                        default:
                            Console.WriteLine("\n\nWill now show items purchased!");
                            foreach (var item in user.ItemList)
                            {
                                Console.WriteLine(item);
                            }
                            break;
                        }
                    } while (user.Sum > 0 && choice != '4');
                    Console.WriteLine($"\nYou have {user.Sum} in change left.");
                    break;

                case '3':
                    Console.WriteLine("\n\nYou will now consume items you have bought.");
                    Console.WriteLine("Press Enter to list items you have bought.");
                    do
                    {
                        Console.WriteLine("\n\nChoose 1. Fanta, 2. Popcorn, 3. Sandwich or 4. To End");
                        choice = GetChoice(4, $"\r# of items left: {user.ItemList.Count}. Please choose an item to consume: ");
                        switch (choice)
                        {
                        case '1':
                            user.Use("Fanta");
                            break;

                        case '2':
                            user.Use("Popcorn");
                            break;

                        case '3':
                            user.Use("Sandwich");
                            break;

                        default:
                            Console.WriteLine("\n\nRemaining items bought:");
                            foreach (string item in user.ItemList)
                            {
                                Console.WriteLine(item);
                            }
                            break;
                        }
                    } while (choice != '4' && user.ItemList.Count > 0);
                    break;

                case '4':
                    Console.WriteLine("\n\nYou will now examine items in the machine.");
                    do
                    {
                        Console.WriteLine("\n\nExamine 1. Fanta, 2. Popcorn, 3. Sandwich or 4. To End");
                        choice = GetChoice(4, $"\rItem to examine: ");
                        switch (choice)
                        {
                        case '1':
                            Fanta fanta = new Fanta();
                            fanta.Examine();
                            break;

                        case '2':
                            Popcorn popcorn = new Popcorn();
                            popcorn.Examine();
                            break;

                        case '3':
                            Sandwich sandwich = new Sandwich();
                            sandwich.Examine();
                            break;

                        default:
                            break;
                        }
                    } while (choice != '4');
                    break;

                default:
                    break;
                }
            } while (menuChoice != '5');
            Console.WriteLine($"\n\nHere you have your {user.Sum}kr back.");
        }