Пример #1
0
        private void UpdateItem()
        {
            // instatiate needed stuff
            var itemToEdit       = new MenuItems();
            var ingredientToEdit = new Ingredients();

            Console.WriteLine("\nEnter the name of the menu item to update:");
            string itemNameToEdit = Console.ReadLine();

            // Get the menu item to update
            if (_menu.GetMenuItemByName(itemNameToEdit) != null)
            {
                // set initial values so a skipped assignment results in no change
                itemToEdit.MenuItem           = _menu.GetMenuItemByName(itemNameToEdit).MenuItem;
                itemToEdit.MenuName           = _menu.GetMenuItemByName(itemNameToEdit).MenuName;
                itemToEdit.Description        = _menu.GetMenuItemByName(itemNameToEdit).Description;
                itemToEdit.Price              = _menu.GetMenuItemByName(itemNameToEdit).Price;
                itemToEdit._ListOfIngredients = _menu.GetMenuItemByName(itemNameToEdit)._ListOfIngredients;
                // the the current version of the item
                Console.WriteLine("\nCurrent menu item information:");
                DisplayMenuItem(itemToEdit);
                // start getting new values. enter to leave unchanged
                Console.WriteLine("\nEnter new item number (enter to leave unchanged):");
                string newDataString = Console.ReadLine();
                if (newDataString != "")
                {
                    itemToEdit.MenuItem = Int32.Parse(newDataString);
                }
                Console.WriteLine("\nEnter new item name (enter to leave unchanged):");
                newDataString = Console.ReadLine();
                if (newDataString != "")
                {
                    itemToEdit.MenuName = newDataString;
                }
                Console.WriteLine("\nEnter new item description (enter to leave unchanged):");
                newDataString = Console.ReadLine();
                if (newDataString != "")
                {
                    itemToEdit.Description = newDataString;
                }
                Console.WriteLine("\nEnter new item price (enter to leave unchanged):");
                newDataString = Console.ReadLine();
                if (newDataString != "")
                {
                    itemToEdit.Price = Double.Parse(newDataString);
                }
                // cal the CRUD method for update. returns true if change made
                if (_menu.UpdateMenuItem(itemNameToEdit, itemToEdit))
                {
                    Console.WriteLine("Menu item updated");
                }
                else
                {
                    Console.WriteLine("Menu item not updated");
                }
                // cannot update an empty list of ingredients
                // ----- should create a method to add ingredients to an already existing menu
                if (itemToEdit._ListOfIngredients != null)
                {
                    Console.WriteLine("Do you wish to edit the ingredients as well (y/n)?");
                    string editIngredientsString = Console.ReadLine();
                    if (editIngredientsString.ToLower() == "y")
                    {
                        bool   active           = true;
                        string toContinueToEdit = "f";
                        string ingredientToUpdate;
                        while (active)
                        {
                            Console.WriteLine("\n Which ingredient would you like to edit");
                            ingredientToUpdate = Console.ReadLine();
                            // call CRUD to update ingredients, passing the results of the get ingredients helper
                            _menu.UpdateMenuItemIngredients(itemNameToEdit, ingredientToUpdate, GetSingleIngredient("new "));
                            Console.WriteLine("\n Would you like to edit more ingredients (y/n)");
                            toContinueToEdit = Console.ReadLine();
                            if (toContinueToEdit.ToLower() != "y")
                            {
                                active = false;
                            }
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Menu Item not found");
            }
        }