/
Console.java
106 lines (86 loc) · 1.96 KB
/
Console.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package es.usantatecla.a5_units.a1_interval.a2_statics;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Console {
private static Console instance = new Console();
public static Console getInstance() {
return instance;
}
private BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
public String readString(String title) {
String input = null;
this.write(title);
try {
input = this.bufferedReader.readLine();
} catch (Exception ex) {
}
return input;
}
public String readString() {
return this.readString("");
}
public int readInt(String title) {
int input = 0;
boolean ok = false;
do {
try {
input = Integer.parseInt(this.readString(title));
ok = true;
} catch (Exception ex) {
this.writeError("integer");
}
} while (!ok);
return input;
}
public double readDouble(String title) {
double input = 0;
boolean ok = false;
do {
try {
input = Double.parseDouble(this.readString(title));
ok = true;
} catch (Exception ex) {
this.writeError("integer");
}
} while (!ok);
return input;
}
public char readChar(String title) {
char charValue = ' ';
boolean ok = false;
do {
String input = this.readString(title);
if (input.length() != 1) {
this.writeError("character");
} else {
charValue = input.charAt(0);
ok = true;
}
} while (!ok);
return charValue;
}
public void write(String string) {
System.out.print(string);
}
public void write(int integer) {
System.out.print(integer);
}
public void write(char character) {
System.out.print(character);
}
public void writeln() {
System.out.println();
}
public void writeln(String string) {
this.write(string);
this.writeln();
}
public void writeln(int integer) {
this.write(integer);
this.writeln();
}
public void writeError(String format) {
this.write("FORMAT ERROR! Enter a " + format + " formatted value.");
this.writeln();
}
}