static void Main(string[] args) { BeverageCollection beverageCollection = new BeverageCollection(); //Create an instance of the UserInterface class UserInterface userInterface = new UserInterface(); //Display the Welcome Message to the user userInterface.DisplayWelcomeGreeting(); //Display the Menu and get the response. Store the response in the choice integer //This is the 'primer' run of displaying and getting. int choice = userInterface.DisplayMenuAndGetResponse(); while (choice != 6) { switch (choice) { case 1: //Print Entire List Of Items string[] allItems = beverageCollection.GetPrintStringsForAllItems(); if (allItems.Length > 0) { //Display all of the items userInterface.DisplayAllItems(allItems); } else { //Display error message for all items userInterface.DisplayAllItemsError(); } break; case 2: //Search For An Item string searchQuery = userInterface.GetSearchQuery(); string itemInformation = beverageCollection.FindById(searchQuery); if (itemInformation != null) { userInterface.DisplayItemFound(itemInformation); } else { userInterface.DisplayItemFoundError(); } break; case 3: //Add A New Item To The List string[] newItemInformation = userInterface.GetNewItemInformation(); Decimal newItemPrice = userInterface.GetItemPrice(); bool newItemActive = userInterface.GetItemActive(); bool addSuccessful = false; //check if the key already exists if (beverageCollection.FindById(newItemInformation[0]) == null) { //add item addSuccessful = beverageCollection.AddNewItem(newItemInformation[0], newItemInformation[1], newItemInformation[2], newItemPrice, newItemActive); //show the user if the add was successful if (addSuccessful) { userInterface.DisplayAddBeverageSuccess(); } else { userInterface.DisplayAddBeverageFailure(); } } else { //shows error if item already exists userInterface.DisplayItemAlreadyExistsError(); } break; case 4: //update an item on the list string update = userInterface.GetUpdateQuery(); string updateInformation = beverageCollection.FindById(update); if (updateInformation != null) { string[] updateItemInformation = userInterface.GetUpdateItemInformation(); Decimal updateItemPrice = userInterface.GetItemPrice(); bool updateItemActive = userInterface.GetItemActive(); bool updateSuccessful = beverageCollection.UpdateRecord(update, updateItemInformation[0], updateItemInformation[1], updateItemPrice, updateItemActive); //successful update? if (updateSuccessful) { userInterface.DisplayUpdateBeverageSuccess(); } else { userInterface.DisplayUpdateBeverageFailure(); } } else { //display if the item is not found userInterface.DisplayItemFoundError(); } break; case 5: //delete an item from the list string del = userInterface.GetDeleteQuery(); bool deleteSuccessful = beverageCollection.DeleteById(del); //show the user if the delete was successful if (deleteSuccessful) { userInterface.DisplayDeleteBeverageSuccess(); } else { userInterface.DisplayDeleteBeverageFailure(); } break; } //Get the new choice of what to do from the user choice = userInterface.DisplayMenuAndGetResponse(); } }
static void Main(string[] args) { //Create an instance of the UserInterface class UserInterface userInterface = new UserInterface(); //Create an instance of the BeverageRepository class BeverageRepository beverageRepository = new BeverageRepository(); //Display the Welcome Message to the user userInterface.DisplayWelcomeGreeting(); //Display the Menu and get the response. Store the response in the choice integer //This is the 'primer' run of displaying and getting. int choice = userInterface.DisplayMenuAndGetResponse(); while (choice != 6) { switch (choice) { case 1: //Print Entire List Of Items userInterface.DisplayAllItems(beverageRepository.GetPrintString()); break; case 2: //Search For An Item string searchQuery = userInterface.GetSearchQuery(); string itemInformation = beverageRepository.GetBeverageInfo(searchQuery); if (itemInformation != null) { userInterface.DisplayItemFound(itemInformation); } else { userInterface.DisplayItemFoundError(); } break; case 3: //Add A New Item To The database string[] newItemInformation = userInterface.GetNewItemInformation(); bool activeItem; // Check if they entered a "y" or "n", if not, display error message and break out of the process. if (newItemInformation[4].ToLower() == "y") { activeItem = true; } else if (newItemInformation[4].ToLower() == "n") { activeItem = false; } else { userInterface.DisplayErrorMessage(); break; } // Check to make sure the that the id isn't in use if (beverageRepository.GetBeverageInfo(newItemInformation[0]) == null) { try { beverageRepository.AddNewItem(newItemInformation[0], newItemInformation[1], newItemInformation[2], decimal.Parse(newItemInformation[3]), activeItem); userInterface.DisplayAddBeverageSuccess(); } catch (FormatException ex) // Catch error when trying to parse the price { userInterface.DisplayInvalidPriceInput(); } } else { userInterface.DisplayItemAlreadyExistsError(); } break; case 4: // Update an item in the database string[] updatedItemInformation = userInterface.GetUpdatedItemInformation(); bool updatedActiveItem = false; if (updatedItemInformation[4].ToLower() == "y") { activeItem = true; } else if (updatedItemInformation[4].ToLower() == "n") { activeItem = false; } else { userInterface.DisplayErrorMessage(); break; } if (beverageRepository.GetBeverageInfo(updatedItemInformation[0]) != null) { try { beverageRepository.UpdateItem(updatedItemInformation[0], updatedItemInformation[1], updatedItemInformation[2], decimal.Parse(updatedItemInformation[3]), updatedActiveItem); userInterface.DisplayAddBeverageSuccess(); } catch (FormatException ex) { userInterface.DisplayInvalidPriceInput(); } } else { userInterface.DisplayItemNotExist(); } break; case 5: // Delete item from database string id = userInterface.GetItemIDForDeletion(); if (beverageRepository.GetBeverageInfo(id) != null) { beverageRepository.DeleteItem(id); userInterface.DisplaySuccessfullyDeletedItem(); } else { userInterface.DisplayItemNotExist(); } break; } //Get the new choice of what to do from the user choice = userInterface.DisplayMenuAndGetResponse(); } }