/// <summary> /// Prompts the user for an integer with the question specified. It does all the data /// validation required and doesn't return until the user provides a valid input. /// </summary> /// <param name="mainQuestion">The main question to ask</param> /// <param name="promptText">The string to prompt the user with. eg. Amount: </param> /// <returns>A MenuReturn with the entered value stored as the value</returns> public static MenuReturn <int> PromptInt(string mainQuestion, string promptText) { MenuReturn <int> result = new MenuReturn <int>(false, 0); bool canContinue = false; while (!canContinue) { Console.WriteLine("\n{0}", mainQuestion); Console.WriteLine("Type '!back' to go back"); Console.Write(promptText); string input = Console.ReadLine(); if (input == "!back") { Console.Clear(); canContinue = true; } else { try { int amount = int.Parse(input); canContinue = true; result = new MenuReturn <int>(true, amount); } catch { PrintError("Please enter an integer"); } } } return(result); }
/// <summary> /// Prompts the user for an char with the question specified. It does all the data /// validation required and doesn't return until the user provides a valid input. /// </summary> /// <param name="mainQuestion">The main question to ask</param> /// <param name="promptText">The string to prompt the user with. eg. Amount: </param> /// <returns>A MenuReturn with the entered value stored as the value</returns> public static MenuReturn <char> PromptChar(string mainQuestion, string promptText) { bool canContinue = false; MenuReturn <char> result = new MenuReturn <char>(false, ' '); while (!canContinue) { Console.WriteLine("\n{0}", mainQuestion); Console.WriteLine("Type '!back' to go back"); Console.Write(promptText); string input = Console.ReadLine(); if (input == "!back") { Console.Clear(); canContinue = true; } else { try { char choice = input.ToLower()[0]; canContinue = true; result = new MenuReturn <char>(true, choice); } catch { PrintError("Please enter a character"); } } } return(result); }
/// <summary> /// Prompts the user for an string with the question specified. It does all the data /// validation required and doesn't return until the user provides a valid input. /// </summary> /// <param name="mainQuestion">The main question to ask</param> /// <param name="promptText">The string to prompt the user with. eg. Amount: </param> /// <returns>A MenuReturn with the entered value stored as the value</returns> public static MenuReturn <string> PromptString(string mainQuestion, string promptText) { MenuReturn <string> result = new MenuReturn <string>(false, ""); bool canContinue = false; while (!canContinue) { Console.WriteLine("\n{0}", mainQuestion); Console.WriteLine("Type '!back' to go back"); Console.Write(promptText); string input = Console.ReadLine(); if (input == "!back") { Console.Clear(); canContinue = true; } else { result = new MenuReturn <string>(true, input); canContinue = true; } } return(result); }
/// <summary> /// Runs the prompt string method with the specified mainQuestion and promptText. It also takes one condition to be tested /// along side the regular validation. It will not leave while the condition is satisfied. If the condition is satisfied /// it prints the condition error. /// /// Reasoning behind this: /// I got really tired of data validation in my menu's so I started by creating the basic PromptInt, PromptDouble, etc. /// Those worked great and made my menu's much simpler, but I noticed that with almost all my menus I was also testing /// one additional condition that need to be satisfied and loop until its satisfied. So I added these methods to allow /// for the conditions. It really cleaned up (I think) my menu page code. /// </summary> /// <param name="mainQuestion">The main question to ask</param> /// <param name="promptText">The string to prompt the user with. eg. Amount: </param> /// <param name="condition">The condition to test against. If this condition is SATISFIED it will print an error, otherwise it returns the result</param> /// <param name="conditionError">The error to print when the condition is satisfied</param> /// <returns>A MenuReturn with the entered value stored as the value</returns> public static MenuReturn <string> PromptString(string mainQuestion, string promptText, Func <string, bool> condition, string conditionError) { bool canContinue = false; MenuReturn <string> result = new MenuReturn <string>(false, ""); while (!canContinue) { result = PromptString(mainQuestion, promptText); if (result.Continue && condition(result.Value)) { PrintError(conditionError); } else //Exit { canContinue = true; } } return(result); }