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
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var configBuilder = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = configBuilder.Build();

            var optionsBuilder = new DbContextOptionsBuilder <PizzaStoreDBContext>();

            optionsBuilder.UseSqlServer(configuration.GetConnectionString("PizzaStoreDB"));
            var options = optionsBuilder.Options;

            var PizzaStoreDBContext = new Data.PizzaStoreDBContext(options);
            var PizzaStoreRepo      = new PizzaStoreRepository(PizzaStoreDBContext);

            Console.WriteLine("Welcome to Revature's PizzaStore!");
            PizzaStoreRepo.PrintOrderHistory(PizzaStoreRepo.SortByLatest());
            Console.WriteLine("To begin, please enter your Name");
            Console.Write("First Name: ");
            string fn = Console.ReadLine();

            Console.Write("Last Name: ");
            string ln   = Console.ReadLine();
            string name = User.FirstandLastName(fn, ln);

            //Check if pre-existing user:


            Console.WriteLine("Here are our locations:");
            Console.WriteLine("Location 1 - 123 Alpha street");
            Console.WriteLine("Location 2 - 456 Bravo street");
            Console.WriteLine("Location 3 - 789 Charlie street");
            Console.WriteLine("Location 4 - 246 Delta street");
            Console.WriteLine("Location 5 - 135 Echo street");
            string answer;

            while (true)
            {
                Console.WriteLine("Please enter your preferred location number");
                answer = Console.ReadLine();
                if (!int.TryParse(answer, out int n))
                {
                    Console.WriteLine("Please enter a number between 1 and 5");
                }
                else if (int.Parse(answer) > 0 && int.Parse(answer) < 6)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Not a valid store! Write a number 1 to 5");
                }
            }
            int PreferredLocation = int.Parse(answer);

            PizzaStoreRepo.PrintOrderHistory(PizzaStoreRepo.GetOrdersByLocation(PreferredLocation));
            string toPrint;

            //If user exists already:
            if (PizzaStoreRepo.DoesUserExist(fn, ln))
            {
                User u = PizzaStoreRepo.GetUser(fn, ln);
                if (u.DefaultLocation != PreferredLocation)
                {
                    Console.WriteLine($"Your default location is {u.DefaultLocation}. Would you like to change it to {PreferredLocation}? [y/n]");
                    string ans = Console.ReadLine();
                    if (ans == "y")
                    {
                        u.DefaultLocation = PreferredLocation;
                        PizzaStoreRepo.UpdateUser(u);
                        PizzaStoreRepo.Save();
                        //UPDATE THE USER HERE AND SAVE TO REPO
                    }
                }
                Console.WriteLine($"Your most recent order is:");
                PizzaStoreRepo.PrintOrderHistory(PizzaStoreRepo.GetMostRecentOrderByUser(u));
                toPrint = OrderHandler.BeginOrder(name, u, Master.LocationList[PreferredLocation - 1], PizzaStoreRepo);
            }
            else
            {
                User u = new User(name)
                {
                    FirstName       = fn,
                    LastName        = ln,
                    Name            = name,
                    DefaultLocation = PreferredLocation
                };
                Master.UserDict.Add(name, u);
                PizzaStoreRepo.AddUser(u);
                PizzaStoreRepo.Save();
                Console.WriteLine($"the user id is {PizzaStoreRepo.GetUserID(u)}");
                Console.WriteLine("Delete this user?");
                string answe = Console.ReadLine();
                if (answe == "y")
                {
                    PizzaStoreRepo.DeleteUser(u);
                    PizzaStoreRepo.Save();
                    return;
                }
                toPrint = OrderHandler.BeginOrder(name, u, Master.LocationList[PreferredLocation - 1], PizzaStoreRepo);
            }
            Console.WriteLine(toPrint);
        }