-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOManager.java
81 lines (54 loc) · 1.44 KB
/
IOManager.java
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
package deliverable_2;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class IOManager {
protected Scanner scanner;
protected PrintStream out;
public IOManager(InputStream in, PrintStream out) {
this.scanner = new Scanner(in);
this.out = out;
}
public IOManager(Scanner in, PrintStream out) {
this.scanner = in;
this.out = out;
}
public IOManager() {
this.scanner = new Scanner(System.in);
this.out = System.out;
}
public void print(String text) {
this.out.print(text);
}
public void println(String text) {
this.out.println(text);
}
public void printf(String format, Object... args) {
this.out.printf(format, args);
}
public String getValidatedInput(String prompt, String [] validInputs, String scold, boolean repeatOnFailure) {
boolean validated = false;
String userInput = null;
print(prompt);
print("(");
for (int i = 0; i < validInputs.length - 1; i++) {
print(validInputs[i] + ',');
}
print(validInputs[validInputs.length - 1]);
print(") > ");
do {
// at least the second attempt, so previous input was invalid
if (userInput != null) {
println(scold);
}
userInput = this.scanner.next();
for (String input : validInputs) {
if (input.equals(userInput)) {
validated = true;
return userInput;
}
}
} while(!validated && repeatOnFailure);
return null;
}
}