Пример #1
0
        /// <summary>
        /// Menu to take the user through all actions.
        /// </summary>
        static void Menu()
        {
            string currentMethod = "Menu";
            //Instantiating objects.
            SupplierDAO supplierDAO = new SupplierDAO();
            SupplierDO  supplier    = new SupplierDO();

            //Loop to keep menu selection going.
            do
            {
                //Try catch to get any exeptions that may occur.
                try
                {
                    //Menu Options.
                    Console.Clear();
                    Console.WriteLine("\t\tPlease select option by pressing a key:\n");
                    Console.WriteLine(" 1) Display all");
                    Console.WriteLine(" 2) Create new");
                    Console.WriteLine(" 3) Update existing");
                    Console.WriteLine(" 4) Delete");
                    Console.WriteLine(" 5) Product menu");
                    Console.WriteLine(" 6) Exit Application");

                    //Allows user to press either number keys on keyboard or use num pad.
                    ConsoleKeyInfo keyPressed = Console.ReadKey();
                    Console.Clear();

                    //Declaring variables.
                    int supplierId = 0;

                    //Creates a new list object called items to store our list object returned from our ViewAllSuppliers().
                    List <SupplierDO> items = supplierDAO.ViewAllSuppliers();

                    //Displays an updated supplier list.
                    if (keyPressed.Key != ConsoleKey.D6 && keyPressed.Key != ConsoleKey.NumPad6)
                    {
                        DisplaySuppliers(items);
                    }

                    //Determines which key was pressed.
                    switch (keyPressed.Key)
                    {
                    //Display---------------------------------------------------------------------
                    case ConsoleKey.NumPad1:
                    case ConsoleKey.D1:
                        break;

                    //Create----------------------------------------------------------------------
                    case ConsoleKey.NumPad2:
                    case ConsoleKey.D2:
                        //Passing the object returned from GetUserInput method into the parameters for our CreateNewSuppliers().
                        supplierDAO.CreateNewSuppliers(GetUserInput());
                        break;

                    //Update----------------------------------------------------------------------
                    case ConsoleKey.NumPad3:
                    case ConsoleKey.D3:
                        Console.WriteLine("Please enter Supplier Id for the row you would like to change.");
                        //Getting the supplier Id that user wants to update.
                        int.TryParse(Console.ReadLine(), out supplierId);

                        //Adding information returned from GetUserInput() into our object.
                        supplier = GetUserInput();

                        //Setting the value of our object variable for supplierId.
                        supplier.SupplierId = supplierId;

                        //Passing all the user input into UpdateSuppliers() using our object named supplier.
                        supplierDAO.UpdateSuppliers(supplier);
                        break;

                    //Delete----------------------------------------------------------------------
                    case ConsoleKey.NumPad4:
                    case ConsoleKey.D4:
                        Console.WriteLine("Please enter Supplier Id.");

                        //Getting supplier Id that user wants to delete.
                        int.TryParse(Console.ReadLine(), out supplierId);

                        //Setting the value of our object variable for supplierId.
                        supplier.SupplierId = supplierId;

                        //Passing supplier Id to the DeleteSuppliers() so that user can select which row to delete.
                        supplierDAO.DeleteSuppliers(supplier.SupplierId);
                        break;

                    //Product Menu----------------------------------------------------------------
                    case ConsoleKey.NumPad5:
                    case ConsoleKey.D5:

                        //Products menu.
                        ProductsMenu();
                        break;

                    //Exit-------------------------------------------------------------------------
                    case ConsoleKey.NumPad6:
                    case ConsoleKey.D6:

                        //Exiting the console.
                        Environment.Exit(0);
                        break;

                    default:
                        break;
                    }

                    //Displays updated supply list if user does NOT press 1.
                    if (keyPressed.Key != ConsoleKey.NumPad1 && keyPressed.Key != ConsoleKey.D1)
                    {
                        items = supplierDAO.ViewAllSuppliers();
                        DisplaySuppliers(items);
                    }
                }
                //Catches any exception that gets thrown.
                catch (Exception ex)
                {
                    string currentClass = "Program";
                    //Prints error to console, logs, and restarts the menu.
                    supplierDAO.SupplierErrorHandler(ex, currentClass, currentMethod, ex.StackTrace);
                    throw ex;
                }
            } while (true);
        }