/// <summary> /// get the next city to travel to from the user /// </summary> /// <returns>string City</returns> public string DisplayGetNextCity() { string nextCity = ""; ConsoleUtil.DisplayReset(); ConsoleUtil.DisplayPromptMessage("Enter the name of the next city:"); nextCity = Console.ReadLine(); return(nextCity); }
/// <summary> /// A general method to get values from an enum from the user /// </summary> /// <typeparam name="T"></typeparam> /// <param name="maxAttempts"></param> /// <param name="userPrompt"></param> /// <param name="returnValue"></param> /// <returns></returns> public static bool GetEnumValueFromUser <T>(int maxAttempts, string userPrompt, out T returnValue, bool enterCancelsOut = false) where T : struct, IConvertible { // // declare values // bool validInput = true; int attempts = 0; string userResponse; returnValue = default(T); do { if (validInput) { ConsoleUtil.DisplayPromptMessage(userPrompt); } else { // // Show the user some valid values for the enum // ConsoleUtil.DisplayMessage("\nValid values are: "); foreach (var item in Enum.GetValues(typeof(T))) { ConsoleUtil.DisplayMessage("(" + item + ")"); } ConsoleUtil.DisplayPromptMessage("Please enter a valid value: "); } // // get the user response // userResponse = ConsoleUtil.UppercaseFirst(Console.ReadLine()); if (enterCancelsOut && userResponse == "") { return(false); } validInput = Enum.TryParse <T>(userResponse, out returnValue); attempts++; }while (!validInput && attempts < maxAttempts); return(validInput); }
/// <summary> /// Gets a company name from the user /// </summary> /// <param name="product"></param> /// <returns></returns> public string DisplayGetCompanyVisited() { // // initialize variable // string companyName; // // set up the console // ConsoleUtil.HeaderText = "Visiting Company"; ConsoleUtil.DisplayReset(); // // get the company name // ConsoleUtil.DisplayPromptMessage("What company did you visit? "); companyName = Console.ReadLine(); DisplayContinuePrompt(); return(companyName); }
/// <summary> /// get the next city to travel to from the user /// </summary> /// <returns>string City</returns> public string DisplayGetNextCity() { // // initialize variables // string nextCity = ""; // // set up the console // ConsoleUtil.HeaderText = "Input City"; ConsoleUtil.DisplayReset(); // // get user input // ConsoleUtil.DisplayPromptMessage("City:"); nextCity = Console.ReadLine(); DisplayContinuePrompt(); return(nextCity); }
/// <summary> /// setup the new salesperson object with the initial 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 Salesperson DisplaySetupAccount() { // // initialize variables // bool validResponse; int userAge; int productAmount; Product.ProductType productType; Salesperson salesperson = new Salesperson(); // // set up the console // ConsoleUtil.HeaderText = "Account Setup"; ConsoleUtil.DisplayReset(); // // get new account info // ConsoleUtil.DisplayPromptMessage("First name: "); salesperson.FirstName = Console.ReadLine(); Console.WriteLine(""); ConsoleUtil.DisplayPromptMessage("Last name: "); salesperson.LastName = Console.ReadLine(); Console.WriteLine(""); ConsoleUtil.DisplayMessage("Age"); validResponse = ConsoleValidator.TryGetIntegerFromUser(10, 90, MAXIMUM_ATTEMPTS, "years old", out userAge); if (validResponse) { salesperson.Age = userAge; } ConsoleUtil.DisplayPromptMessage("Account ID: "); salesperson.AccountID = Console.ReadLine(); Console.WriteLine(""); // // validate user input // ConsoleUtil.DisplayMessage("Setting up inventory.."); ConsoleUtil.DisplayMessage(""); if (!ConsoleValidator.GetEnumValueFromUser <Product.ProductType>(MAXIMUM_ATTEMPTS, "Body Style:", out productType)) { ConsoleUtil.DisplayMessage("Maximum attempts exceeded, returning to main menu."); } if (!ConsoleValidator.TryGetIntegerFromUser(MINIMUM_BUYSELL_AMOUNT, MAXIMUM_BUYSELL_AMOUNT, MAXIMUM_ATTEMPTS, $"{productType.ToString()}s", out productAmount)) { ConsoleUtil.DisplayMessage("Max attempts exceeded, returning to main menu with default value of 0 vehicles."); } // // set salesman product object // salesperson.CurrentStock[productType].NumberOfUnits = productAmount; salesperson.Rank = Salesperson.Ranks.Beginner; DisplayContinuePrompt(); return(salesperson); }
/// <summary> /// helper method to get a valid integer from the user within a range /// </summary> /// <param name="minValue">inclusive minimum value</param> /// <param name="maxValue">inclusive maximum value</param> /// <param name="maxAttempts">maximum number of attempts</param> /// <param name="pluralName">plural name of item</param> /// <param name="validInput">indicates valid user input</param> /// <returns></returns> public static bool TryGetIntegerFromUser(int minValue, int maxValue, int maxAttempts, string pluralName, out int userInteger, bool enterCancelsOut = false) { bool validInput = false; bool maxAttemptsExceeded = false; string userResponse; string feedbackMessage = ""; int attempts = 1; userInteger = 0; while (!validInput && !maxAttemptsExceeded) { // // more attempts available // if (attempts <= maxAttempts) { ConsoleUtil.DisplayPromptMessage($"Enter the number, between {minValue} and {maxValue}, of {pluralName}:"); userResponse = Console.ReadLine(); if (enterCancelsOut && userResponse == "") { return(false); } ConsoleUtil.DisplayMessage(""); // // input is an Integer // if (int.TryParse(userResponse, out userInteger)) { // // input is in range // if (userInteger >= minValue && userInteger <= maxValue) { validInput = true; } // // input is not in range // else { feedbackMessage = $"The number {userInteger} is not in the specified range."; } } // // input is not an Integer // 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(); } ConsoleUtil.DisplayReset(); } else { ConsoleUtil.DisplayMessage(""); } attempts++; } else { maxAttemptsExceeded = true; } } return(validInput); }
/// <summary> /// setup the new salesperson object with the initial 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 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.DisplayPromptMessage("Product Types: "); ConsoleUtil.DisplayMessage(""); foreach (string productTypeName in Enum.GetNames(typeof(ProductType))) { if (productTypeName != ProductType.None.ToString()) { ConsoleUtil.DisplayMessage(productTypeName); } } ConsoleUtil.DisplayMessage(""); ConsoleUtil.DisplayPromptMessage("Enter the product type: "); ProductType productType; if (Enum.TryParse <ProductType>(UppercaseFirst(Console.ReadLine()), out productType)) { salesperson.CurrentStock.Type = productType; } else { salesperson.CurrentStock.Type = 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.DisplayReset(); ConsoleUtil.DisplayMessage("Your account is setup"); DisplayContinuePrompt(); return(salesperson); }