- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextUI.java
More file actions
Latest commit
198 lines (165 loc) · 4.56 KB
/
Copy pathTextUI.java
File metadata and controls
198 lines (165 loc) · 4.56 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
// Class TextUI for program HelloWorld
// version 1.01.01-delta(rc)-Model T
// by Ray Arias, 31 August 2022
// subpackage: TextUI, version 1.01.01-delta(rc)-Model T
// semantic version 1.1.1-rc
importjava.util.Scanner;
/* This is a class of static methods for
* obtaining user input, displaying out-
* put, and converting one datatype to
* another.
*/
publicclassTextUI {
// Creates and initializes a Scanner
// object in from java.init.Scanner
// and returns its reference.
publicstaticScanneropenUI() {
Scannerresult = newScanner(System.in);
returnresult;
}
// Reads a String of input from the user
// using the Scanner object and returns
// it.
publicstaticStringreadln(Scannerscanner) {
Stringresult = scanner.nextLine();
returnresult;
}
// Reads a double from the user using
// the Scanner object and returns it.
publicstaticdoublereaddbl(Scannerscanner) {
doubleresult = scanner.nextDouble();
scanner.nextLine();
returnresult;
}
// Reads an int from the user using
// the Scanner object and returns it.
publicstaticintreadint(Scannerscanner) {
intresult = scanner.nextInt();
scanner.nextLine();
returnresult;
}
// Closes the Scanner object.
publicstaticvoidcloseUI(Scannerscanner) {
scanner.close();
}
// Uses the above methods to quickly
// create an instance of Scanner
// class, use it to read a line of
// user input, close the Scanner in-
// stance, and return the input as a
// String.
publicstaticStringquickReadln() {
Scannerscanner = TextUI.openUI();
Stringresult = TextUI.readln(scanner);
TextUI.closeUI(scanner);
returnresult;
} // THIS METHOD NEEDS FIXING
// Sends characters to the console to
// clear the screen.
publicstaticvoidclrscn() {
TextUI.display("\033[H");
TextUI.display("\033[2J");
}
// This method returns the numerical
// value of a char as an int.
publicstaticintchar2int(charc) {
returnCharacter.getNumericValue(c);
}
// This method returns the ANSI char
// associated with the int given as a
// parameter.
publicstaticcharchr(inti) {
return (char) i;
}
// Displays char (no newline, unless)
// c is '\n'.
publicstaticvoidchr(charc) {
System.out.print(c);
}
// Displays escape codes by passing
// only the character associated with
// them without the backward slash be-
// fore it.
publicstaticvoidescchr(charc) {
charcc = c;
switch (cc) {
case'\\':
System.out.print('\\');
break;
case'\"':
System.out.print('\"');
break;
case'\'':
System.out.print('\'');
break;
case'n':
System.out.print('\n');
break;
case't':
System.out.print('\t');
break;
case'r':
System.out.print('\r');
break;
case'b':
System.out.print('\b');
break;
case'f':
System.out.print('\f');
break;
}
}
// Display newline
publicstaticvoidnewln() {
System.out.println();
}
// Displays String specified in
// parameter without going to a
// new line at the end.
publicstaticvoiddisplay(Stringtext) {
System.out.print(text);
}
// Displays int specified in
// parameter without going to a
// new line at the end.
publicstaticvoiddisplay(inti) {
System.out.print(i);
}
// Displays double specified in
// parameter without going to a
// new line at the end.
publicstaticvoiddisplay(doubled) {
System.out.print(d);
}
// Displays String specified in
// patameter and then go to a
// new line afterward.
publicstaticvoiddisplayln(Stringtext) {
System.out.println(text);
}
// Displays int specified in
// patameter and then go to a
// new line afterward.
publicstaticvoiddisplayln(inti) {
System.out.println(i);
}
// Displays double specified in
// patameter and then go to a
// new line afterward.
publicstaticvoiddisplayln(doubled) {
System.out.println(d);
}
// Displays an int and a String in
// the format expressed in the first
// String.
publicstaticvoiddispf(Stringformat, intn, Stringdescription) {
System.out.printf(format, n, description);
}
// Extracts the user's choice as the
// first character in the String typed
// in by the user.
publicstaticcharuserChoice(StringinputString) {
charresult = inputString.toUpperCase().charAt(0);
returnresult;
}
}