-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleInput.cs
More file actions
206 lines (185 loc) · 7.03 KB
/
ConsoleInput.cs
File metadata and controls
206 lines (185 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAddonLibary
{
public enum ErrorResponse
{
ThrowWithExeption,
ContinueWithErrorMessage
}
public static class ConsoleInput
{
// Get Int value methods
public static int GetIntValue()
{
return GetIntValue(() => ConsoleLogs.SystemLog("Please enter the int value:"), ErrorResponse.ContinueWithErrorMessage);
}
public static int GetIntValue(Action message)
{
return GetIntValue(message, ErrorResponse.ContinueWithErrorMessage);
}
public static int GetIntValue(ErrorResponse type)
{
return GetIntValue(() => ConsoleLogs.SystemLog("Please enter the int value:"), type);
}
public static int GetIntValue(Action message, ErrorResponse type)
{
message.Invoke();
int inputValue = 0;
bool convertationIsDone = false;
while (!convertationIsDone)
{
try
{
inputValue = Convert.ToInt32(Console.ReadLine());
convertationIsDone = true;
}
catch (Exception exception)
{
if (type == ErrorResponse.ContinueWithErrorMessage)
{
ConsoleLogs.ErrorLog("Failed to convert string to int. Please try again");
}
else if (type == ErrorResponse.ThrowWithExeption)
{
ConsoleLogs.ErrorLog(exception.Message);
throw;
}
}
}
return inputValue;
}
// Get Double value methods
public static double GetDoubleValue()
{
return GetDoubleValue(() => ConsoleLogs.SystemLog("Please enter the double value: "), ErrorResponse.ContinueWithErrorMessage);
}
public static double GetDoubleValue(Action message)
{
return GetDoubleValue(message, ErrorResponse.ContinueWithErrorMessage);
}
public static double GetDoubleValue(ErrorResponse type)
{
return GetDoubleValue(() => ConsoleLogs.SystemLog("Please enter the double value: "), type);
}
public static double GetDoubleValue(Action message, ErrorResponse type)
{
ConsoleLogs.SystemLog(message);
double doubleValue = 0;
bool convertationIsDone = false;
while (!convertationIsDone)
{
try
{
string stringValue = Console.ReadLine();
if (!string.IsNullOrEmpty(stringValue))
{
stringValue = stringValue.Replace('.', ',');
doubleValue = Convert.ToDouble(stringValue);
convertationIsDone = true;
}
}
catch (Exception exeption)
{
if (type == ErrorResponse.ContinueWithErrorMessage)
{
ConsoleLogs.ErrorLog("Failed to convert string to double. Please try again");
}
else if (type == ErrorResponse.ThrowWithExeption)
{
ConsoleLogs.ErrorLog(exeption.Message);
throw;
}
}
}
return doubleValue;
}
// Get Boolean value methods
public static bool GetBooleanValue()
{
return GetBooleanValue(() => ConsoleLogs.SystemLog("Please enter the boolean value: "), ErrorResponse.ContinueWithErrorMessage);
}
public static bool GetBooleanValue(Action message)
{
return GetBooleanValue(message, ErrorResponse.ContinueWithErrorMessage);
}
public static bool GetBooleanValue(ErrorResponse type)
{
return GetBooleanValue(() => ConsoleLogs.SystemLog("Please enter the boolean value: "), type);
}
public static bool GetBooleanValue(Action message, ErrorResponse type)
{
ConsoleLogs.SystemLog(message);
bool booleanValue = false;
bool convertationIsDone = false;
while (!convertationIsDone)
{
try
{
booleanValue = Convert.ToBoolean(Console.ReadLine());
convertationIsDone = true;
}
catch (Exception exeption)
{
if (type == ErrorResponse.ContinueWithErrorMessage)
{
ConsoleLogs.ErrorLog("Failed to convert string to bool. Please try again");
}
else if (type == ErrorResponse.ThrowWithExeption)
{
ConsoleLogs.ErrorLog(exeption.Message);
throw;
}
}
}
return booleanValue;
}
public static string GetStringValue()
{
return GetStringValue(() => ConsoleLogs.SystemLog("Please enter the text: "), ErrorResponse.ContinueWithErrorMessage);
}
public static string GetStringValue(Action message)
{
return GetStringValue(message, ErrorResponse.ContinueWithErrorMessage);
}
public static string GetStringValue(Action message, ErrorResponse type)
{
ConsoleLogs.SystemLog(message);
string userMessage = null;
bool convertationIsDone = false;
while (!convertationIsDone)
{
try
{
string stringValue = Console.ReadLine();
if (!string.IsNullOrEmpty(stringValue))
{
convertationIsDone = true;
userMessage = stringValue;
}
else
{
ConsoleLogs.ErrorLog("Your string is null. Please try again");
}
}
catch (Exception exeption)
{
if (type == ErrorResponse.ContinueWithErrorMessage)
{
ConsoleLogs.ErrorLog("Failed to get string. Please try again");
}
else if (type == ErrorResponse.ThrowWithExeption)
{
ConsoleLogs.ErrorLog(exeption.Message);
throw;
}
}
}
return userMessage;
}
}
}