-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathConsole.java
More file actions
62 lines (50 loc) · 1.8 KB
/
Console.java
File metadata and controls
62 lines (50 loc) · 1.8 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
package atmproject;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Console {
private final Scanner input;
private final PrintStream output;
public Console(InputStream in, PrintStream out) {
this.input = new Scanner(in);
this.output = out;
}
public void print(String val, Object... args) {
output.format(val, args);
}
public void newln() {
print("\n");
}
public void println(String val, Object... vals) {
print(val + "\n", vals);
}
public String getStringInput(String prompt, Object... args) {
print(prompt, args);
return input.nextLine();
}
public Double getDoubleInput(String prompt, Object... args) {
String stringInput = getStringInput(prompt, args);
try {
Double doubleInput = Double.parseDouble(stringInput);
return doubleInput;
} catch (NumberFormatException nfe) { // TODO - Eliminate recursive nature
println("[ %s ] is an invalid user input!", stringInput);
println("Try inputting a numeric value!");
return getDoubleInput(prompt, args);
}
}
public Long getLongInput(String prompt, Object... args) {
String stringInput = getStringInput(prompt, args);
try {
Long longInput = Long.parseLong(stringInput);
return longInput;
} catch (NumberFormatException nfe) { // TODO - Eliminate recursive nature
println("[ %s ] is an invalid user input!", stringInput);
println("Try inputting an integer value!");
return getLongInput(prompt, args);
}
}
public Integer getIntegerInput(String prompt, Object... args) {
return getLongInput(prompt, args).intValue();
}
}