Esempio n. 1
0
        // public static Store SelectStore(Store store, PizzaStore.Storing.PizzaStoreDBContext db){

        //   Console.WriteLine("Choose a Store: ");
        //   int i;
        //   for(i = 0; i < db.Store.ToList().Count; i++){

        //     Console.WriteLine($"{i+1}. {db.Store.ToList()[i].Name}");

        //   }

        //   Console.WriteLine($"{i+1}. Cancel");

        //   int input = Convert.ToInt32(Console.ReadLine());


        //   if (input < i+1 && input > 0){

        //    store.Name = db.Store.ToList()[i-1].Name;

        //   }

        //   return store;

        // }

        public static void PlaceOrder(User user, int uID, PizzaStore.Storing.PizzaStoreDBContext db, int sID, string storeName)
        {
            Order order = new Order();

            order.store = new Store()
            {
                Name = storeName
            };

            bool running = true;

            while (running)
            {
                Console.WriteLine("=== Editing Order ===");
                Console.WriteLine("1. Choose Preset Pizza");
                Console.WriteLine("2. Create Custom Pizza");
                Console.WriteLine("3. View Order");
                Console.WriteLine("4. Edit Pizza");
                Console.WriteLine("5. Remove Pizza");
                Console.WriteLine("6. Checkout");
                Console.WriteLine("7. Exit (Order will be lost)");

                string input = Console.ReadLine();

                switch (input)
                {
                case "1":
                    Console.WriteLine("=== Choose Preset Pizza ===");
                    Console.WriteLine("1. Meat Lovers (Hand tossed, Pepperoni, Ham, Sausage)");
                    Console.WriteLine("2. Fred Special (Hand tossed, Ham, Pineapple)");
                    Console.WriteLine("3. Jeremy Special (Thin, Bacon, Pineapple)");
                    Console.WriteLine("4. Deluxe (Hand Tossed, Pepperoni, Sausage, Mushroom, Green Pepper, Onion)");

                    string input2 = Console.ReadLine();
                    Pizza  pizza;

                    switch (input2)
                    {
                    case "1": // Meat Lovers
                        if (order.Pizzas.Count < 50)
                        {
                            pizza      = order.CreatePizza();
                            pizza.Name = "Meat Lover's";
                            pizza.addTopping(new Topping("pepperoni"));
                            pizza.addTopping(new Topping("ham"));
                            pizza.addTopping(new Topping("sausage"));
                            pizza.Crust = new Crust("handtossed");

                            Console.WriteLine("--Choose a size--");
                            Console.WriteLine("1. Small");
                            Console.WriteLine("2. Medium");
                            Console.WriteLine("3. Large");

                            string input5 = Console.ReadLine();

                            switch (input5)
                            {
                            case "1":
                                pizza.Size = new Size("small");
                                break;

                            case "2":
                                pizza.Size = new Size("medium");
                                break;

                            case "3":
                                pizza.Size = new Size("large");
                                break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("You have reached the maximum number of pizzas. The pizza has not been added.");
                        }

                        break;

                    case "2": //Fred Special

                        if (order.Pizzas.Count < 50)
                        {
                            pizza      = order.CreatePizza();
                            pizza.Name = "Fred Special";
                            pizza.addTopping(new Topping("ham"));
                            pizza.addTopping(new Topping("pineapple"));
                            pizza.Crust = new Crust("handtossed");

                            Console.WriteLine("--Choose a size--");
                            Console.WriteLine("1. Small");
                            Console.WriteLine("2. Medium");
                            Console.WriteLine("3. Large");

                            string input6 = Console.ReadLine();

                            switch (input6)
                            {
                            case "1":
                                pizza.Size = new Size("small");
                                break;

                            case "2":
                                pizza.Size = new Size("medium");
                                break;

                            case "3":
                                pizza.Size = new Size("large");
                                break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("You have reached the maximum number of pizzas. The pizza has not been added.");
                        }

                        break;

                    case "3": // Jeremy Special

                        if (order.Pizzas.Count < 50)
                        {
                            pizza      = order.CreatePizza();
                            pizza.Name = "Jeremy Special";
                            pizza.addTopping(new Topping("bacon"));
                            pizza.addTopping(new Topping("pineapple"));
                            pizza.Crust = new Crust("handtossed");

                            Console.WriteLine("--Choose a size--");
                            Console.WriteLine("1. Small");
                            Console.WriteLine("2. Medium");
                            Console.WriteLine("3. Large");

                            string input7 = Console.ReadLine();

                            switch (input7)
                            {
                            case "1":
                                pizza.Size = new Size("small");
                                break;

                            case "2":
                                pizza.Size = new Size("medium");
                                break;

                            case "3":
                                pizza.Size = new Size("large");
                                break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("You have reached the maximum number of pizzas. The pizza has not been added.");
                        }

                        break;

                    case "4": // Deluxe

                        if (order.Pizzas.Count < 50)
                        {
                            pizza      = order.CreatePizza();
                            pizza.Name = "Deluxe";
                            pizza.addTopping(new Topping("bacon"));
                            pizza.addTopping(new Topping("pineapple"));
                            pizza.Crust = new Crust("handtossed");

                            Console.WriteLine("--Choose a size--");
                            Console.WriteLine("1. Small");
                            Console.WriteLine("2. Medium");
                            Console.WriteLine("3. Large");

                            string input8 = Console.ReadLine();

                            switch (input8)
                            {
                            case "1":
                                pizza.Size = new Size("small");
                                break;

                            case "2":
                                pizza.Size = new Size("medium");
                                break;

                            case "3":
                                pizza.Size = new Size("large");
                                break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("You have reached the maximum number of pizzas. The pizza has not been added.");
                        }

                        break;

                    default:
                        break;
                    }
                    break;

                case "2": //Create Custom Pizza

                    Pizza tempPizza = new Pizza(new List <Topping>(), new Crust("handtossed"), new Size("medium"), "Custom");


                    Console.WriteLine("===Current Pizza===");

                    tempPizza = EditPizza(tempPizza);

                    if (tempPizza.Size.pSize.Equals("") || tempPizza.Crust.Name.Equals("") || tempPizza.Toppings.Count == 0)
                    {
                        Console.WriteLine("Custom Pizza creation unsuccessful.");
                    }
                    else
                    {
                        order.Pizzas.Add(tempPizza);
                        Console.WriteLine("Custom Pizza creation successful!");
                    }
                    break;

                case "3": //View Order
                    Console.WriteLine(order.getPizzaList());
                    Console.WriteLine($"Total: ${order.getPrice()}");
                    break;

                case "4": //Edit Pizza

                    Console.WriteLine("Choose a pizza to edit:");
                    int i;
                    for (i = 0; i < order.Pizzas.Count; i++)
                    {
                        Console.WriteLine($"{i+1}. {order.Pizzas[i].Name}");
                    }

                    Console.WriteLine($"{i+1}. Cancel");

                    int input3 = Convert.ToInt32(Console.ReadLine());

                    if (input3 < i + 1 && input3 > 0)
                    {
                        order.Pizzas[i - 1] = EditPizza(order.Pizzas[i - 1]);
                    }

                    break;

                case "5": //Remove Pizza

                    Console.WriteLine("Choose a pizza to remove:");
                    int j;
                    for (j = 0; j < order.Pizzas.Count; j++)
                    {
                        Console.WriteLine($"{j+1}. {order.Pizzas[j].Name}");
                    }

                    Console.WriteLine($"{j+1}. Cancel");

                    int input4 = Convert.ToInt32(Console.ReadLine());

                    //j = number of pizzas
                    if (input4 - 1 < j && input4 > 0)
                    {
                        order.Pizzas.RemoveAt(input4 - 1);
                        Console.WriteLine($"Pizza #{input4} removed.");
                    }

                    break;

                case "6": //Checkout



                    if (order.Pizzas.Count == 0)
                    {
                        Console.WriteLine("Order was empty, so it was canceled.");
                    }
                    else
                    {
                        order.PlaceOrder();

                        //Add to database
                        PizzaRepository pr = new PizzaRepository();
                        pr.AddOrder(order, sID, uID);


                        user.Orders.Add(order);
                    }
                    running = false;
                    break;

                case "7": //Exit

                    Console.WriteLine("Order Canceled.");
                    running = false;
                    break;

                default:
                    break;
                }
            }
        }
        public IActionResult SubmitOrder(StoreViewModel model)
        {
            TempData.Keep("StoreID");
            int storeID = (int)TempData["StoreID"];

            try {
                _ = userLoggedIn;
            } catch (NullReferenceException) {
                model.ReasonForError = "You are not logged into the system. You will only be able to view menus until you return to the main page and log in.";
                return(View("Visit", model));
            }

            StoreModel store = _repo.GetStore(storeID);

            model.StoreName = store.Name;
            // reference needs to be re-established if an error occurs submitting the order
            List <SelectListItem> c = new List <SelectListItem>();

            foreach (CrustModel crust in _repo.GetCrusts())
            {
                c.Add(new SelectListItem {
                    Text = crust.Name, Value = crust.ID.ToString()
                });
            }
            model.Crusts = c;

            bool submitOrderClicked    = Request.Form["SubmitOrder"].ToString() != "";
            bool addCustomPizzaClicked = Request.Form["AddCustom"].ToString() != "";
            bool backButtonClicked     = Request.Form["Back"].ToString() != "";
            int  buttonsClicked        = (submitOrderClicked ? 1 : 0) + (addCustomPizzaClicked ? 1 : 0) + (backButtonClicked ? 1 : 0);

            if (buttonsClicked > 1)
            {
                Console.WriteLine("Multiple buttons registered as clicked on the menu page");
                model.ReasonForError = "There was a problem processing your request. Please try again.";
                return(View("Visit", model));
            }
            else if (submitOrderClicked)
            {
                Tuple <int, string> userCanOrder = _repo.UserCanOrder(storeID, userLoggedIn);
                if (userCanOrder.Item1 == 1)
                {
                    model.ReasonForError = $"You cannot place another order at any store for {userCanOrder.Item2}";
                    return(View("Visit", model));
                }
                else if (userCanOrder.Item1 == 2)
                {
                    model.ReasonForError = $"You cannot place another order at this store for {userCanOrder.Item2}";
                    return(View("Visit", model));
                }

                decimal overallCost     = 0.00M;
                int     overallQuantity = 0;

                int max = _repo.GetNextOrderNumber();

                bool noIssues = true;
                foreach (CheckModel selectedPizza in model.Menu)
                {
                    if (selectedPizza.Checked)
                    {
                        string size = selectedPizza.SelectedSize.ToString().ToLower();
                        if (Enum.IsDefined(typeof(Size), size))
                        {
                            model.ReasonForError = $"Invalid size on pizza {selectedPizza.Name}";
                            return(View("Visit", model));
                        }
                        if (selectedPizza.Quantity == 0)
                        {
                            model.ReasonForError = $"{selectedPizza.Name} pizza must have a quantity greater than 0 if selected to be ordered";
                            return(View("Visit", model));
                        }
                        else if (selectedPizza.Quantity < 0)
                        {
                            model.ReasonForError = $"{selectedPizza.Name} pizza must have a positive quantity greater";
                            return(View("Visit", model));
                        }

                        int        crustID;
                        CrustModel crust = null;
                        if (int.TryParse(selectedPizza.SelectedCrust, out crustID))
                        {
                            crust = _repo.GetCrust(crustID);
                        }
                        if (crust == null)
                        {
                            model.ReasonForError = $"No crust was selected on the {selectedPizza.Name} pizza. Please try selecting a different crust.";
                            return(View("Visit", model));
                        }

                        PizzaModel pizza;
                        if (selectedPizza.ID != 0)
                        {
                            pizza = _repo.GetPizza(selectedPizza.ID);
                        }
                        else
                        {
                            pizza = new PizzaModel {
                                Cost = 20.00M
                            };
                        }
                        if (pizza == null)
                        {
                            Console.WriteLine($"Unknown pizza with ID {selectedPizza.ID} submitted; skipping");
                            continue;
                        }
                        decimal costOfThesePizzas = pizza.Cost * (decimal)selectedPizza.Quantity;
                        string  toppingIDs        = "";
                        int     toppingCount      = 0;
                        foreach (ToppingViewModel topping in selectedPizza.SelectedToppings)
                        {
                            if (topping.IsSelected)
                            {
                                toppingIDs += $"{topping.ID},";
                                toppingCount++;
                            }
                        }
                        if (toppingCount > 5)
                        {
                            model.ReasonForError = $"{selectedPizza.Name} has more than 5 toppings selected. Please uncheck some toppings on this pizza.";
                            return(View("Visit", model));
                        }
                        else if (toppingCount < 2)
                        {
                            model.ReasonForError = $"{selectedPizza.Name} needs at least 2 toppings selected. Please add some more toppings on this pizza.";
                            return(View("Visit", model));
                        }
                        toppingIDs = toppingIDs.Substring(0, toppingIDs.Length - 1);


                        noIssues &= _repo.AddOrder(new OrderModel {
                            OrderID   = max + 1,
                            StoreID   = storeID,
                            PizzaID   = pizza.ID,
                            UserID    = userLoggedIn,
                            Created   = DateTime.Now,
                            Quantity  = selectedPizza.Quantity,
                            TotalCost = costOfThesePizzas,
                            Size      = selectedPizza.SelectedSize.ToString(),
                            CrustID   = crust.ID,
                            Toppings  = toppingIDs
                        });
                        overallCost     += costOfThesePizzas;
                        overallQuantity += selectedPizza.Quantity;
                    }
                }
                if (overallCost > 250.00M)
                {
                    model.ReasonForError = "This order exceeds $250. Please remove some pizzas, then try again.";
                    return(View("Visit", model));
                }
                else if (overallQuantity > 50)
                {
                    model.ReasonForError = "This order exceeds 50 pizzas. Please remove some pizzas, then try again.";
                    return(View("Visit", model));
                }
                else if (overallQuantity == 0)
                {
                    model.ReasonForError = "There are no pizzas in this order. Please add some pizzas, then try again.";
                    return(View("Visit", model));
                }
                else if (!noIssues)
                {
                    model.ReasonForError = "There was a problem adding some pizzas to your order";
                }

                return(View("Submitted"));
            }
            else if (addCustomPizzaClicked)
            {
                List <ToppingModel> toppings         = _repo.GetToppings();
                ToppingViewModel[]  toppingsSelected = new ToppingViewModel[toppings.Count()];
                for (int i = 0; i < toppingsSelected.Length; i++)
                {
                    ToppingModel topping = toppings[i];
                    toppingsSelected[i] = new ToppingViewModel {
                        ID = topping.ID, Name = topping.Name, IsSelected = false
                    };
                }
                model.Menu.Add(new CheckModel {
                    ID = 0, Name = "Custom", Checked = true, Cost = 20.00M, DefaultCrust = 0, SelectedToppings = toppingsSelected
                });
                return(View("Visit", model));
            }
            else if (backButtonClicked)
            {
                return(Redirect("/User/StoreSelection"));
            }
            else // no buttons check is placed down here to remove the 'not all code paths return a value' error
            {
                Console.WriteLine("Request was sent but no buttons registered as clicked");
                model.ReasonForError = "There was a problem processing your request. Please try again.";
                return(View("Visit", model));
            }
        }