using System;
using static System.Console;
namespace Iko {
public static class Iko {
///
/// Gets a double value from the user.
///
/// The double.
/// Prompt
/// A validation function
/// Text to print if user fails
public static double GetDouble(string text, Func validate, string falseText) {
Write(text);
double res;
while (true) {
if (double.TryParse(ReadLine(), out res) && validate(res)) {
return res;
} else {
WriteLine(falseText);
Write(text);
}
}
}
///
/// Gets a double value from the user.
///
/// The double.
/// Prompt
/// Text to print if user fails
public static double GetDouble(string text, string falseText) {
return GetDouble(text, _ => { return true; }, falseText);
}
///
/// Gets a float value from the user.
///
/// The double.
/// Prompt
/// A validation function
/// Text to print if user fails
public static float GetFloat(string text, Func validate, string falseText) {
Write(text);
float res;
while (true) {
if (float.TryParse(ReadLine(), out res) && validate(res)) {
return res;
} else {
WriteLine(falseText);
Write(text);
}
}
}
///
/// Gets a float value from the user.
///
/// The double.
/// Prompt
/// Text to print if user fails
public static float GetFloat(string text, string falseText) {
return GetFloat(text, _ => { return true; }, falseText);
}
///
/// Gets an int value from the user.
///
/// The double.
/// Prompt
/// A validation function
/// Text to print if user fails
public static int GetInt(string text, Func validate, string falseText) {
Write(text);
int res;
while (true) {
if (int.TryParse(ReadLine(), out res) && validate(res)) {
return res;
} else {
WriteLine(falseText);
Write(text);
}
}
}
///
/// Gets an int value from the user.
///
/// The double.
/// Prompt
/// Text to print if user fails
public static int GetInt(string text, string falseText) {
return GetInt(text, _ => { return true; }, falseText);
}
///
/// Repeats a given function.
///
/// the function to be repeated
public static void Repeat(Action f) {
do {
f();
Write("Press Enter to repeat program. Press any other key to quit.");
} while (ReadKey().Key == ConsoleKey.Enter);
}
}
}