on master help sign in
107 lines 3.9 kB history raw
1using System;
2using static System.Console;
4namespace Iko {
5 public static class Iko {
6 /// <summary>
7 /// Gets a double value from the user.
8 /// </summary>
9 /// <returns>The double.</returns>
10 /// <param name="text">Prompt</param>
11 /// <param name="validate">A validation function</param>
12 /// <param name="falseText">Text to print if user fails</param>
13 public static double GetDouble(string text, Func<double, bool> validate, string falseText) {
14 Write(text);
15 double res;
16 while (true) {
17 if (double.TryParse(ReadLine(), out res) && validate(res)) {
18 return res;
19 } else {
20 WriteLine(falseText);
21 Write(text);
22 }
23 }
24 }
26 /// <summary>
27 /// Gets a double value from the user.
28 /// </summary>
29 /// <returns>The double.</returns>
30 /// <param name="text">Prompt</param>
31 /// <param name="falseText">Text to print if user fails</param>
32 public static double GetDouble(string text, string falseText) {
33 return GetDouble(text, _ => { return true; }, falseText);
34 }
36 /// <summary>
37 /// Gets a float value from the user.
38 /// </summary>
39 /// <returns>The double.</returns>
40 /// <param name="text">Prompt</param>
41 /// <param name="validate">A validation function</param>
42 /// <param name="falseText">Text to print if user fails</param>
43 public static float GetFloat(string text, Func<float, bool> validate, string falseText) {
44 Write(text);
45 float res;
46 while (true) {
47 if (float.TryParse(ReadLine(), out res) && validate(res)) {
48 return res;
49 } else {
50 WriteLine(falseText);
51 Write(text);
52 }
53 }
54 }
56 /// <summary>
57 /// Gets a float value from the user.
58 /// </summary>
59 /// <returns>The double.</returns>
60 /// <param name="text">Prompt</param>
61 /// <param name="falseText">Text to print if user fails</param>
62 public static float GetFloat(string text, string falseText) {
63 return GetFloat(text, _ => { return true; }, falseText);
64 }
66 /// <summary>
67 /// Gets an int value from the user.
68 /// </summary>
69 /// <returns>The double.</returns>
70 /// <param name="text">Prompt</param>
71 /// <param name="validate">A validation function</param>
72 /// <param name="falseText">Text to print if user fails</param>
73 public static int GetInt(string text, Func<int, bool> validate, string falseText) {
74 Write(text);
75 int res;
76 while (true) {
77 if (int.TryParse(ReadLine(), out res) && validate(res)) {
78 return res;
79 } else {
80 WriteLine(falseText);
81 Write(text);
82 }
83 }
84 }
86 /// <summary>
87 /// Gets an int value from the user.
88 /// </summary>
89 /// <returns>The double.</returns>
90 /// <param name="text">Prompt</param>
91 /// <param name="falseText">Text to print if user fails</param>
92 public static int GetInt(string text, string falseText) {
93 return GetInt(text, _ => { return true; }, falseText);
94 }
96 /// <summary>
97 /// Repeats a given function.
98 /// </summary>
99 /// <param name="f">the function to be repeated</param>
100 public static void Repeat(Action f) {
101 do {
102 f();
103 Write("Press Enter to repeat program. Press any other key to quit.");
104 } while (ReadKey().Key == ConsoleKey.Enter);
105 }
106 }