/// <summary> /// Displays a menu and get a valid numeric response from the user. /// </summary> /// <returns>An integer representing the menu choice the user chose.</returns> public int DisplayMenuAndGetResponse(MenuItemsSet menuItemsSet, string menuTitle) { // Print the menu's title Console.WriteLine(); Console.WriteLine(menuTitle); Console.WriteLine(); // (Used to write the menu number. Will be incremented in each pass through the upcoming loop.) int currentMenuNumber = 1; // Print off each menu item in the menu items set. Write the number before each one and increment for every time you print a menu item. foreach (string menuItem in menuItemsSet.MenuItems) { Console.WriteLine(currentMenuNumber + ". " + menuItem); currentMenuNumber++; } // (This while loop will exit on a return statement) while (true) { // (Used to store the converted numeric user input) int numericValidUserInput; // Prompt for a menu selection string rawUserInput = GetStringResponse("Enter a menu option"); // If the selection is valid [is it a number? Is that number between 1 and the last item in the list of menu items?] if (int.TryParse(rawUserInput, out numericValidUserInput) && numericValidUserInput >= 1 && numericValidUserInput <= menuItemsSet.MenuItems.Count) { // We have a valid answer; return the data. return numericValidUserInput; } else { // We don't have a valid answer - display an error DisplayMessage("That is not a valid option. Please make a valid choice."); } } }
/// <summary> /// Initializes the data that the program will be working with. /// </summary> private static void initializeData() { // TODO: This data technically has to do with the user interface, but it's specific to this program only - hence why I put it in Program.cs. // The User Interface class is meant to be extremely generic so that it can be utilized more dynamically, so all specific data that // only applies to this single program was written in Program.cs. // Object initialization userInterface = new UserInterface(); beverageDatabaseHook = new BeverageZDwyerEntities(); // Data initialization (don't have to do it here, technically, but figured might as well) greeting = "Welcome to the beverage database management system."; dontAddBeveragesWithTheSameID = true; // Should we add beverages to the database that have a duplicate id? /* MENU ITEM SET INITIALIZATION */ /* (MenuItemSet objects are used by the DisplayMenuAndGetResponse method in UserInterface to draw a menu) */ /* (The MenuItemSet constructor can take a variable amount of strings with the params argument specifier I just found out about a few minutes ago lol) */ // Main menu mainMenuItemsSet = new MenuItemsSet ( "Print all beverages", "Retreive beverage by property or ID", "Add a new beverage", "Update an existing beverage", "Delete a beverage", "Exit program" ); // Find item by property menu findItemByWhatMenuItemsSet = new MenuItemsSet ( "ID", "Name", "Pack", "Price", "Active status", "(Back to main menu)" ); // Find beverages with a price that is... menu priceMenuItemsSet = new MenuItemsSet ( "...less than or equal to a given value", "...equal to a given value", "...more than or equal to a given value" ); // Find by active status menu findItemByActiveStatus = new MenuItemsSet ( "Is active", "Is not active" ); }