public int DisplayGetNumberOfUnitsToBuy(Product product) { ConsoleUtil.HeaderText = "Buy Inventory"; ConsoleUtil.DisplayReset(); ConsoleUtil.DisplayMessage("Buying " + product.Type.ToString() + " products."); ConsoleUtil.DisplayMessage(""); if (!ConsoleValidator.TryGetIntegerFromUser(MINIMUM_BUYSELL_AMOUNT, MAXIMUM_BUYSELL_AMOUNT, MAXIMUM_ATTEMPTS, "products", out int numberOfUnitsToBuy)) { ConsoleUtil.DisplayMessage("Please set the number of products you would like to buy."); numberOfUnitsToBuy = 0; DisplayContinuePrompt(); } ConsoleUtil.DisplayReset(); ConsoleUtil.DisplayMessage(numberOfUnitsToBuy + " " + product.Type.ToString() + " products have been added to the inventory."); DisplayContinuePrompt(); return(numberOfUnitsToBuy); }
/// <summary> /// setup the new salesperson object with the initial data and validate the data /// Note: To maintain the pattern of only the Controller changing the data this method should /// return a Salesperson object with the initial data to the controller. For simplicity in /// this demo, the ConsoleView object is allowed to access the Salesperson object's properties. /// </summary> public void DisplaySetupAccount() { ConsoleUtil.HeaderText = "Account Setup"; ConsoleUtil.DisplayReset(); ConsoleUtil.DisplayMessage("Setup your account now."); Console.WriteLine(); ConsoleUtil.DisplayPromptMessage("Enter your first name: "); _salesperson.FirstName = Console.ReadLine(); Console.WriteLine(); ConsoleUtil.DisplayPromptMessage("Enter your last name: "); _salesperson.LastName = Console.ReadLine(); Console.WriteLine(); //validating account number bool validAccountNumber = false; while (!validAccountNumber) { //indicates whether or not an exception was thrown string exceptionMessage = ""; try { ConsoleUtil.DisplayPromptMessage("Enter your account number: "); _salesperson.AccountNumber = int.Parse(Console.ReadLine()); } catch (FormatException e) { ConsoleUtil.DisplayMessage("You did not enter an integer."); exceptionMessage = e.Message; } catch (OverflowException e) { ConsoleUtil.DisplayMessage("Your number was too large."); exceptionMessage = e.Message; } catch (Exception e) { ConsoleUtil.DisplayMessage("An error occurred."); exceptionMessage = e.Message; } finally { Console.WriteLine(); } if (exceptionMessage == "") { validAccountNumber = true; } } //validating widget type bool validWidgetType = false; while (!validWidgetType) { //indicates whether or not an exception was thrown string exceptionMessage = ""; try { ConsoleUtil.DisplayPromptMessage("Enter the type of widget you will be selling: "); string userResponse = Console.ReadLine(); _salesperson.CurrentStock.Type = (WidgetItemStock.WidgetType)Enum.Parse(typeof(WidgetItemStock.WidgetType), userResponse, true); int widgetAsInt = (int)_salesperson.CurrentStock.Type; if (widgetAsInt < 1 || widgetAsInt > 3) { throw new IndexOutOfRangeException(); } } catch (ArgumentException e) { ConsoleUtil.DisplayMessage("You did not enter a valid widget type."); exceptionMessage = e.Message; } catch (IndexOutOfRangeException) { ConsoleUtil.DisplayMessage("You did not enter a valid widget type."); exceptionMessage = "Not within the enum or user selected type none."; } catch (Exception e) { ConsoleUtil.DisplayMessage("An error occurred."); exceptionMessage = e.Message; } finally { Console.WriteLine(); } if (exceptionMessage == "") { validWidgetType = true; } } //validating number of widgets bool validWidgetCount = false; while (!validWidgetCount) { //indicates whether or not an exception was thrown string exceptionMessage = ""; try { ConsoleUtil.DisplayPromptMessage("Enter the number of widgets: "); _salesperson.CurrentStock.AddUnits(int.Parse(Console.ReadLine())); } catch (FormatException e) { ConsoleUtil.DisplayMessage("You did not enter an integer."); exceptionMessage = e.Message; } catch (OverflowException e) { ConsoleUtil.DisplayMessage("Your number was too large."); exceptionMessage = e.Message; } catch (Exception e) { ConsoleUtil.DisplayMessage("An error occurred."); exceptionMessage = e.Message; } finally { Console.WriteLine(); } if (exceptionMessage == "") { validWidgetCount = true; } } DisplayContinuePrompt(); }
public void DisplayUpdateAccountInfo() { bool updatingAccountInfo = true; while (updatingAccountInfo) { ConsoleUtil.HeaderText = "Update Account Info"; ConsoleUtil.DisplayReset(); Console.CursorVisible = false; ConsoleUtil.DisplayMessage("Select the attribute you wish to update:"); Console.WriteLine(); Console.WriteLine( "\t" + "1. First Name" + Environment.NewLine + "\t" + "2. Last Name" + Environment.NewLine + "\t" + "3. Account Number" + Environment.NewLine + "\t" + "4. Widget Type" + Environment.NewLine + "\t" + "E. Exit" + Environment.NewLine); ConsoleKeyInfo userResponse = Console.ReadKey(true); ConsoleUtil.DisplayReset(); switch (userResponse.KeyChar) { case '1': ConsoleUtil.DisplayPromptMessage("Enter the new first name: "); _salesperson.FirstName = Console.ReadLine(); DisplayContinuePrompt(); break; case '2': ConsoleUtil.DisplayPromptMessage("Enter the new last name: "); _salesperson.LastName = Console.ReadLine(); DisplayContinuePrompt(); break; case '3': ConsoleUtil.DisplayPromptMessage("Enter the new account number: "); try { _salesperson.AccountNumber = int.Parse(Console.ReadLine()); } catch (FormatException) { ConsoleUtil.DisplayMessage("You did not enter an integer."); } catch (OverflowException) { ConsoleUtil.DisplayMessage("Your integer was too large."); } catch (Exception) { ConsoleUtil.DisplayMessage("An error occurred."); } DisplayContinuePrompt(); break; case '4': ConsoleUtil.DisplayPromptMessage("Enter the new widget type: "); try { _salesperson.CurrentStock.Type = (WidgetItemStock.WidgetType)Enum.Parse(typeof(WidgetItemStock.WidgetType), Console.ReadLine(), true); int widgetAsInt = (int)_salesperson.CurrentStock.Type; if (widgetAsInt < 1 || widgetAsInt > 3) { throw new IndexOutOfRangeException(); } } catch (ArgumentException e) { ConsoleUtil.DisplayMessage("You did not enter a valid widget type."); } //using one of the predefined exception types for when the user enters a number, say '0' or '4' that does not correspond to a widget type catch (IndexOutOfRangeException) { ConsoleUtil.DisplayMessage("You did not enter a valid widget type."); } catch (Exception e) { ConsoleUtil.DisplayMessage("An error occurred."); } DisplayContinuePrompt(); break; case 'E': case 'e': updatingAccountInfo = false; DisplayContinuePrompt(); break; default: ConsoleUtil.DisplayMessage("You did not enter a valid input."); DisplayContinuePrompt(); break; } } }
/// <summary> /// get the menu choice from the user /// </summary> public MenuOption DisplayGetUserMenuChoice() { MenuOption userMenuChoice = MenuOption.None; bool usingMenu = true; while (usingMenu) { // // set up display area // ConsoleUtil.HeaderText = "Main Menu"; ConsoleUtil.DisplayReset(); Console.CursorVisible = false; // // display the menu // ConsoleUtil.DisplayMessage("Please type the number of your menu choice."); Console.WriteLine(); Console.WriteLine( "\t" + "1. Buy Widgets" + Environment.NewLine + "\t" + "2. Sell Widgets" + Environment.NewLine + "\t" + "3. Display Inventory" + Environment.NewLine + "\t" + "4. Display Cities Visited and Current City" + Environment.NewLine + "\t" + "5. Travel" + Environment.NewLine + "\t" + "6. Display Account Info" + Environment.NewLine + "\t" + "7. Update Account Info" + Environment.NewLine + "\t" + "E. Exit" + Environment.NewLine); // // get and process the user's response // note: ReadKey argument set to "true" disables the echoing of the key press // ConsoleKeyInfo userResponse = Console.ReadKey(true); switch (userResponse.KeyChar) { case '1': userMenuChoice = MenuOption.Buy; usingMenu = false; break; case '2': userMenuChoice = MenuOption.Sell; usingMenu = false; break; case '3': userMenuChoice = MenuOption.DisplayInventory; usingMenu = false; break; case '4': userMenuChoice = MenuOption.DisplayCities; usingMenu = false; break; case '5': userMenuChoice = MenuOption.Travel; usingMenu = false; break; case '6': userMenuChoice = MenuOption.DisplayAccountInfo; usingMenu = false; break; case '7': userMenuChoice = MenuOption.UpdateAccountInfo; usingMenu = false; break; case 'E': case 'e': userMenuChoice = MenuOption.Exit; usingMenu = false; break; default: ConsoleUtil.DisplayMessage("You did not enter a valid input."); DisplayContinuePrompt(); ConsoleUtil.DisplayReset(); break; } } Console.CursorVisible = true; return userMenuChoice; }
public static bool TryGetIntegerFromUser(int minValue, int maxValue, int maxAttempts, string pluralName, out int userInteger) { bool validInput = false; bool maxAttemptsExceeded = false; string userResponse; string feedbackMessage = ""; int attempts = 1; userInteger = 0; while (!validInput && !maxAttemptsExceeded) { if (attempts <= maxAttempts) { ConsoleUtil.DisplayPromptMessage($"Please enter the number between {minValue} and {maxValue}, of {pluralName}:"); userResponse = Console.ReadLine(); ConsoleUtil.DisplayMessage(""); if (int.TryParse(userResponse, out userInteger)) { if (userInteger >= minValue && userInteger <= maxValue) { validInput = true; } else { feedbackMessage = $"The number {userInteger} is not in the specified range."; } } else { feedbackMessage = $"{userResponse} is not an integer."; } if (!validInput && attempts <= maxAttempts) { ConsoleUtil.DisplayMessage($"You entered: {userResponse}"); ConsoleUtil.DisplayMessage(feedbackMessage); if (attempts < maxAttempts) { ConsoleUtil.DisplayMessage($"Please enter an integer between {minValue} and {maxValue}."); ConsoleUtil.DisplayMessage("Press any key to try again."); Console.ReadKey(); } else { ConsoleUtil.DisplayMessage("It appears you have exceeded the maximum number of attempts allowed."); ConsoleUtil.DisplayMessage("Press any key to continue."); Console.ReadKey(); } Console.Clear(); } else { ConsoleUtil.DisplayMessage(""); } attempts++; } else { maxAttemptsExceeded = true; } } return(validInput); }
public Salesperson DisplaySetupAccount() { Salesperson salesperson = new Salesperson(); ConsoleUtil.HeaderText = "Account Setup"; ConsoleUtil.DisplayReset(); ConsoleUtil.DisplayMessage("Setup your account now."); ConsoleUtil.DisplayMessage(""); ConsoleUtil.DisplayPromptMessage("Enter your first name: "); salesperson.FirstName = Console.ReadLine(); ConsoleUtil.DisplayMessage(""); ConsoleUtil.DisplayPromptMessage("Enter your last name: "); salesperson.LastName = Console.ReadLine(); ConsoleUtil.DisplayMessage(""); ConsoleUtil.DisplayPromptMessage("Enter your account ID: "); salesperson.AccountID = Console.ReadLine(); ConsoleUtil.DisplayMessage(""); ConsoleUtil.DisplayMessage("Product Types"); ConsoleUtil.DisplayMessage(""); foreach (string productTypeName in Enum.GetNames(typeof(Product.ProductType))) { if (productTypeName != Product.ProductType.None.ToString()) { ConsoleUtil.DisplayMessage(productTypeName); } } ConsoleUtil.DisplayMessage(""); ConsoleUtil.DisplayPromptMessage("Enter the product type: "); Product.ProductType productType; if (Enum.TryParse <Product.ProductType>(UppercaseFirst(Console.ReadLine()), out productType)) { salesperson.CurrentStock.Type = productType; } else { salesperson.CurrentStock.Type = Product.ProductType.None; } if (ConsoleValidator.TryGetIntegerFromUser(0, 100, 3, "products", out int numberOfUnits)) { salesperson.CurrentStock.AddProducts(numberOfUnits); } else { ConsoleUtil.DisplayMessage("It appears you are having difficulty setting the number of products in your stock."); ConsoleUtil.DisplayMessage("By default, the number of products in your inventory are now set to zero."); salesperson.CurrentStock.AddProducts(0); DisplayContinuePrompt(); } ConsoleUtil.DisplayMessage(""); ConsoleUtil.DisplayReset(); ConsoleUtil.DisplayMessage("Your account has been setup"); DisplayContinuePrompt(); return(salesperson); }
/// <summary> /// get the menu choice from the user /// </summary> public MenuOption DisplayGetUserMenuChoice() { MenuOption userMenuChoice = MenuOption.None; bool usingMenu = true; while (usingMenu) { // // set up display area // ConsoleUtil.DisplayReset(); Console.CursorVisible = false; // // display the menu // ConsoleUtil.DisplayMessage("Please type the number of your menu choice."); ConsoleUtil.DisplayMessage(""); Console.Write( "\t" + "1. Travel" + Environment.NewLine + "\t" + "2. Buy" + Environment.NewLine + "\t" + "3. Sell" + Environment.NewLine + "\t" + "4. Display Inventory" + Environment.NewLine + "\t" + "5. Display Cities" + Environment.NewLine + "\t" + "6. Display Account Info" + Environment.NewLine + "\t" + "E. Exit" + Environment.NewLine); // // get and process the user's response // note: ReadKey argument set to "true" disables the echoing of the key press // ConsoleKeyInfo userResponse = Console.ReadKey(true); switch (userResponse.KeyChar) { case '1': userMenuChoice = MenuOption.Travel; usingMenu = false; break; case '2': userMenuChoice = MenuOption.Buy; usingMenu = false; break; case '3': userMenuChoice = MenuOption.Sell; usingMenu = false; break; case '4': userMenuChoice = MenuOption.DisplayInventory; usingMenu = false; break; case '5': userMenuChoice = MenuOption.DisplayCities; usingMenu = false; break; case '6': userMenuChoice = MenuOption.DisplayAccountInfo; usingMenu = false; break; case 'E': case 'e': userMenuChoice = MenuOption.Exit; usingMenu = false; break; default: ConsoleUtil.DisplayMessage( "It appears you have selected an incorrect choice." + Environment.NewLine + "Press any key to continue or the ESC key to quit the application."); userResponse = Console.ReadKey(true); if (userResponse.Key == ConsoleKey.Escape) { usingMenu = false; } break; } } Console.CursorVisible = true; return(userMenuChoice); }
/// <summary> /// get the menu choice from the user /// </summary> public MenuOption DisplayGetUserMenuChoice() { MenuOption userMenuChoice = MenuOption.None; bool usingMenu = true; while (usingMenu) { ConsoleUtil.DisplayReset(); Console.CursorVisible = false; ConsoleUtil.DisplayMessage("Please type the number of your menu choice."); ConsoleUtil.DisplayMessage(""); Console.Write( "\t" + "1. Travel" + Environment.NewLine + "\t" + "2. Buy" + Environment.NewLine + "\t" + "3. Sell" + Environment.NewLine + "\t" + "4. Display Inventory" + Environment.NewLine + "\t" + "5. Display Cities" + Environment.NewLine + "\t" + "6. Display Account Info" + Environment.NewLine + "\t" + "E. Exit" + Environment.NewLine); ConsoleKeyInfo userResponse = Console.ReadKey(true); switch (userResponse.KeyChar) { case '1': userMenuChoice = MenuOption.Travel; usingMenu = false; break; case '2': userMenuChoice = MenuOption.Buy; usingMenu = false; break; case '3': userMenuChoice = MenuOption.Sell; usingMenu = false; break; case '4': userMenuChoice = MenuOption.DisplayInventory; usingMenu = false; break; case '5': userMenuChoice = MenuOption.DisplayCities; usingMenu = false; break; case '6': userMenuChoice = MenuOption.DisplayAccountInfo; usingMenu = false; break; case 'E': case 'e': userMenuChoice = MenuOption.Exit; usingMenu = false; break; default: ConsoleUtil.DisplayMessage( "That is not a valid imput. Please enter a valid input." + Environment.NewLine + "Press any key to continue or the E key to quit the application."); userResponse = Console.ReadKey(true); if (userResponse.Key == ConsoleKey.Escape) { usingMenu = false; } break; } } Console.CursorVisible = true; return(userMenuChoice); }