Exemplo n.º 1
0
        public static string BeginOrder(string name, User u, Location l, PizzaStoreRepository repo)
        {
            Console.WriteLine("View your order history?");
            string ans = Console.ReadLine();

            if (ans == "y")
            {
                repo.PrintOrderHistory(repo.GetOrdersByUser(u));
            }

            Order o = new Order
            {
                User       = u,
                UserID     = repo.GetUserID(u),
                OrderTime  = DateTime.Now,
                LocationID = l.LocationID,
                NumPizza   = 0,
                Price      = 0
            };

            repo.AddOrder(o);
            repo.Save();
            o.Id = repo.GetMostRecentOrderID();

            //First check if the user ordered within 2 hours
            //if ((repo.GetMostRecentOrderByUser(o).OrderTime - o.OrderTime).TotalMinutes < 120)
            //{
            //    return $"No, {u.FirstName}, we can't prcoess your order since you have ordered within the past 2 hours.";
            //}


            if (l.UserExistInOrderHistory(l.OrderHistory, name))
            {
                //if the user exists, give a suggestion based on the last order...
                Pizza Suggested = Location.SortOrderHistory(l.OrderHistoryByUser(l.OrderHistory, name), "latest")[0].PizzaList[0];
                Console.WriteLine($"You have ordered a {Suggested.PizzaSize} with {Suggested.Toppings} in the past. Would you like to add this to your order? [y/n]");
                string answ = Console.ReadLine();
                if (answ == "y")
                {
                    o.PizzaList.Add(Suggested);
                    o.NumPizza++;
                    o.Price += Suggested.Price;
                    Console.WriteLine("Would you like to order more pizzas? [y/n]");
                    string more = Console.ReadLine();
                    if (more == "n")
                    {
                        FinalizeOrder(l, o, repo);
                    }
                }
            }
            //Now regardless of user in system or not they can order
            while (o.Price < 500)
            {
                if (o.NumPizza > 11)
                {
                    Console.WriteLine("Cannot order more pizza since you are ordering more than 12 pizzas.");
                    return("Failed to place order");
                }
                //Actually begin to order
                Pizza p = new Pizza()
                {
                    OrderID   = repo.GetMostRecentOrderID(),
                    PizzaSize = "A"
                };

                Console.WriteLine("Please select the size of your pizza [S/M/L]");
                p.PizzaSize = Console.ReadLine();
                //Update pizza price based on size
                if (p.PizzaSize == "S")
                {
                    p.Price    += 10;
                    p.PizzaSize = "S";
                }
                if (p.PizzaSize == "M")
                {
                    p.Price    += 15;
                    p.PizzaSize = "M";
                }
                if (p.PizzaSize == "L")
                {
                    p.Price    += 20;
                    p.PizzaSize = "L";
                }
                Console.WriteLine("We have the following toppings:");
                l.Toppings.ForEach(Console.WriteLine);
                for (var i = 0; i < l.Toppings.Count; i++)
                {
                    Console.WriteLine($"Add {l.Toppings[i]}? [y/n]");
                    AddTopping(l, p, l.Toppings[i], Console.ReadLine(), repo);
                }
                repo.AddPizza(p);
                repo.Save();
                p.Id = repo.GetMostRecentPizza().Id;
                o.PizzaList.Add(p);
                o.Price += p.Price;
                o.NumPizza++;

                Console.WriteLine($"You currently have {o.NumPizza} pizza(s). Would you like to order more? [y/n]");
                string b = Console.ReadLine();
                if (b == "n")
                {
                    break;
                }
            }
            FinalizeOrder(l, o, repo);
            return("Order Finished.");
        }
        static void Main(string[] args)
        {
            #region Database

            #region Database

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("Appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            Console.WriteLine(configuration.GetConnectionString("PizzaPalace"));



            #endregion

            var optionsBuilder = new DbContextOptionsBuilder <PizzaPalaceContext>();
            optionsBuilder.UseSqlServer(configuration.GetConnectionString("PizzaPalace"));
            var repo = new PizzaStoreRepository(new PizzaPalaceContext(optionsBuilder.Options));


            #endregion


            int     option = 1;
            Methods m      = new Methods();


            #region Menu

            while (option != 0)
            {
                try
                {
                    m.PrintAll();
                    option = int.Parse(Console.ReadLine());

                    //This if will call a method an populate the inventory.
                    if (option == 1)
                    {
                        m.LoadFromDB();
                    }

                    //This if will contain the order info to pass it to parameters to another class so it can be processed.
                    if (option == 2)
                    {
                        Console.WriteLine("These are the options for your pizza. Choose one: ");
                        m.PrintPizza();
                        Console.WriteLine();

                        try
                        {
                            int pizzaOption = int.Parse(Console.ReadLine());
                            int pizzaQty    = 0;
                            int i;
                            int count = 0;

                            if (int.TryParse(pizzaOption.ToString(), out i))
                            {
                                count++;
                                if (count == 1)
                                {
                                    if (pizzaOption >= 0)
                                    {
                                        try
                                        {
                                            #region UserInfo
                                            Console.WriteLine("Lets place a order but first. Fill the require  information about you. ");
                                            Console.WriteLine("Enter the First Name: ");
                                            string fName = Console.ReadLine();
                                            Console.WriteLine("Enter the Last Name: ");
                                            string lName = Console.ReadLine();
                                            m.SearchUser(fName, lName);
                                            Console.WriteLine("What is the location of this user? ");
                                            m.PrintLocations();
                                            int locationUser = int.Parse(Console.ReadLine());

                                            Console.WriteLine("Enter the user ID you want to use the order with: ");
                                            int userID = int.Parse(Console.ReadLine());

                                            m.PrintLocations();
                                            Console.WriteLine("What is the location ID of the store: ");
                                            int locationID = int.Parse(Console.ReadLine());


                                            #endregion

                                            Console.WriteLine("How many " + m.piz[pizzaOption].NamePizza.ToString() + " Pizza do you want? ");
                                            pizzaQty = int.Parse(Console.ReadLine());

                                            if (int.TryParse(pizzaQty.ToString(), out i))
                                            {
                                                if (pizzaQty <= int.Parse(m.piz[pizzaOption].CountPizza.ToString()))
                                                {
                                                    // m.OrderPizza(userID, locationID, pizzaOption, pizzaQty);



                                                    repo.AddOrder(locationID, userID, locationUser, DateTime.Now, pizzaOption, pizzaQty);
                                                    repo.Save();
                                                }
                                                else
                                                {
                                                    Console.WriteLine("We are low on stock at the moment. Please try again later. Thanks.");
                                                }
                                            }
                                        }
                                        catch (FormatException ex)
                                        {
                                            Logger logger = LogManager.GetCurrentClassLogger();
                                            logger.ErrorException("Format Error", ex);
                                            Console.WriteLine($"Unexpected error: {ex.Message}");
                                        }
                                        catch (Exception ex)
                                        {
                                            Logger logger = LogManager.GetCurrentClassLogger();
                                            logger.ErrorException("Format Error", ex);
                                            Console.WriteLine($"Unexpected error: {ex.Message}");
                                        }
                                    }
                                }
                            }
                        }
                        catch (FormatException ex)
                        {
                            Logger logger = LogManager.GetCurrentClassLogger();
                            logger.ErrorException("Format Error", ex);
                            Console.WriteLine($"Unexpected error: {ex.Message}");
                        }
                        catch (Exception ex)
                        {
                            Logger logger = LogManager.GetCurrentClassLogger();
                            logger.ErrorException("Format Error", ex);
                            Console.WriteLine($"Unexpected error: {ex.Message}");
                        }
                    }

                    if (option == 3)
                    {
                        Console.WriteLine("Enter a user ID to search the suggested pizza: ");
                        m.PrintSuggestedOrder(int.Parse(Console.ReadLine()));
                        // m.getCountOfList();
                    }


                    if (option == 4)
                    {
                        Console.WriteLine("Enter a Name to display its data: ");
                        var name = Console.ReadLine();
                        Console.WriteLine("Enter Last Name: ");
                        var lName = Console.ReadLine();
                        m.SearchUser(name, lName);
                    }


                    if (option == 5)
                    {
                        Console.WriteLine("Enter the order ID you want to display: ");
                        m.DisplayOrderByID(int.Parse(Console.ReadLine()));
                    }

                    if (option == 6)
                    {
                        Console.WriteLine("What is the location of the store you are looking for? ");
                        m.DisplayOrderByLocation(int.Parse(Console.ReadLine()));
                    }

                    if (option == 7)
                    {
                        Console.WriteLine("Enter a ID from user you want to view the order history from: ");
                        m.DisplayOrdersByUser(Console.ReadLine());
                    }

                    if (option == 8)
                    {
                        m.SortByAll();
                    }

                    if (option == 9)
                    {
                        m.serializeToXML();
                    }

                    if (option == 10)
                    {
                        m.DezerializedAsync();
                    }
                }
                catch (FormatException ex)
                {
                    Logger logger = LogManager.GetCurrentClassLogger();
                    logger.ErrorException("Format Error: ", ex);
                    Console.WriteLine(ex);
                }
                catch (Exception ex)
                {
                    Logger logger = LogManager.GetCurrentClassLogger();
                    logger.ErrorException("Format Error", ex);
                    Console.WriteLine($"Unexpected error: {ex.Message}");
                }
            }

            #endregion
        }