A method that asks user for an answer, validates if the answer is right and returns the desired value. If the answer is invalid, it asks the user again.
private static object GetUserDecision(string question, Type typeToReturn, Func<object, bool> validator)
{
string answer;
if (typeToReturn == typeof(bool))
{
do
{
Write(question + " (y/n) ");
answer = Console.ReadLine();
answer = answer.ToLower().Trim();
} while (answer != "y" && answer != "n");
return answer == "y";
}
else if (typeToReturn == typeof(string))
{
do
{
Console.WriteLine(question);
answer = Console.ReadLine();
} while (!validator(answer));
return answer;
}
return null;
}
private static bool GetUserDecision(string question)
{
return (bool) GetUserDecision(question, typeof (bool), o => true);
}