-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInput.java
More file actions
251 lines (217 loc) · 6.82 KB
/
Copy pathUserInput.java
File metadata and controls
251 lines (217 loc) · 6.82 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Manages restrictions, getting, setting, and validating user input based on user-defined rules
*/
import java.util.ArrayList;
class UserInput {
/**
* Asks the user for a number
*
* @param msg The message to ask the user
* @param restriction The restriction
* @return The number from the user
*/
public static int getInt(String msg, InputRestriction restriction) {
System.out.print(msg);
return ATM.kb.nextInt();
}
/**
* Asks the user for any input. Warning: this can include any value, including an empty string or
* null
*
* @param string The message to ask the user when they input a value
* @return The value input by the user.
*/
private static String getString(String string) {
return UserInput.getString(string, null);
}
/**
* Asks the user for input, with a restriction
*
* @param msg The message to ask the user
* @param restriction The restriction which is placed on the input
* @return The input from the user, which satisfies the restriction
*/
private static String getString(String msg, InputRestriction restriction) {
String input;
do {
System.out.print(msg);
input = ATM.kb.nextLine();
if (restriction == null) {
break;
}
} while (!restriction.checkConformity(input).doesConform());
return input;
}
}
/*
* This class allows restrictions to be placed on user input so that the program receives valid
* data.
*/
class InputRestriction extends UserInput {
public static final Object NoRestriction = null;
private boolean shouldBeNumeric = false;
private Integer maxLength = null;
private Integer minLength = null;
private String errorMsg = null;
private boolean ignoreWhitespace = true;
private ArrayList<String> options = new ArrayList<>();
/**
* Checks whether or not the input conforms to the restrictions
*
* @param input The input
* @return The results of the input conformity audit
*/
InputRestrictionResult checkConformity(String input) {
InputRestrictionResult result = new InputRestrictionResult();
if (shouldIgnoreWhitespace()) {
input = input.trim();
}
// if options are set, make sure that the text is contained
// within one of them
if (options.size() != 0) {
if (!options.contains(input)) {
result.setDoesConform(false);
return result;
}
}
if ((minLength == null || (input.length() >= minLength))
&& (maxLength == null || (input.length() <= maxLength))) {
if (shouldBeNumeric) {
if (input.replaceAll("[^0-9]", "").length() == input.length()) {
result.setDoesConform(true);
}
} else {
result.setDoesConform(true);
}
}
// if the input conforms, then return the success result
// otherwise set the error message and return
if (result.doesConform()) {
return result;
} else {
result.setErrorMessage(errorMsg);
result.setDoesConform(false);
return result;
}
}
/**
* The error message to return if the user did not enter in the right data
*
* @param errorMsg The error message
*/
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
/**
* Forces the input to be a certain length; otherwise an error occurs and the user must reinput
* their value
*
* @param exactLen The exact length of the text
*/
public void setExactLength(int exactLen) {
this.maxLength = exactLen;
this.minLength = exactLen;
}
/**
* The maximum length of the text
*
* @param maxLength The max length of the text (in characters)
*/
void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
/**
* The minimum length of the text
*
* @param minLength The minimum length of the text (in characters)
*/
void setMinLength(int minLength) {
this.minLength = minLength;
}
/**
* @param shouldBeNumeric Whether or not the input should be numeric
*/
void setShouldBeNumeric(boolean shouldBeNumeric) {
this.shouldBeNumeric = shouldBeNumeric;
}
/**
* Only allows the user to enter in a certain option
*
* @param options The list of options the user can choose from.
*/
public void setOptions(ArrayList<String> options) {
if (options.size() > 1) {
this.options = options;
} else {
System.out.println("Options not set. Min length is two.");
}
}
/**
* Removes all options
*/
public void clearOptions() {
this.options.clear();
}
private boolean shouldIgnoreWhitespace() {
return ignoreWhitespace;
}
public void setShouldIgnoreWhitespace(boolean ignoreWhitespace) {
this.ignoreWhitespace = ignoreWhitespace;
}
public class InputRestrictionResult {
private boolean doesConform = false;
private boolean shouldDisplayErrorMessage = true;
private String errorMessage;
public String getErrorMessage() {
if (errorMessage != null) {
return errorMessage;
} else {
return "The input does not meet the specified requirements.";
}
}
/*
* Whether or not to display the error message
*/
public boolean isShouldDisplayErrorMessage() {
return shouldDisplayErrorMessage;
}
public void setDoesConform(boolean doesConform) {
this.doesConform = doesConform;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public void setShouldDisplayErrorMessage(boolean shouldDisplayErrorMessage) {
this.shouldDisplayErrorMessage = shouldDisplayErrorMessage;
}
public boolean doesConform() {
return doesConform;
}
}
}
/*
* This class provides common restrictions for keyboard input. For example, the credit card
* restriction only allows 16 numbers. The pin restriction only allows four numbers.
*/
abstract class Restrictions extends InputRestriction {
/*
* The input restriction for asking the user for their card number (has to be exactly 16 digits)
*/
static public InputRestriction CCNumber() {
InputRestriction cardNumberRestriction = new InputRestriction();
cardNumberRestriction.setShouldBeNumeric(true);
cardNumberRestriction.setMaxLength(16);
cardNumberRestriction.setMinLength(16);
return cardNumberRestriction;
}
/*
* Input restriction for asking the user for their pin (has to be exactly four digits)
*/
static public InputRestriction pin() {
InputRestriction pinRestriction = new InputRestriction();
pinRestriction.setShouldBeNumeric(true);
pinRestriction.setMaxLength(4);
pinRestriction.setMinLength(4);
return pinRestriction;
}
}