forked from cuongquangnam/CZ2002-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOController.java
316 lines (281 loc) · 11 KB
/
IOController.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package com.controllers;
//import Model.Constant.*;
//import Model.Holiday;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import static Controller.CineplexManager.*;
/**
* This class handles all input from standard input as well as some formatting methods.
*
* @version 1.0
*/
public class IOController {
/**
* This method is to read an integer from standard input whose value should be in a
* certain range.
* @param i the lower bound (inclusive) of the input
* @param j the upper bound (inclusive) of the input
* @return the input from standard input with specified range
*/
public static int readChoice(int i, int j) {
Scanner sc = new Scanner(System.in);
int choice;
try {
choice = sc.nextInt();
} catch (InputMismatchException ex) {
System.out.println("Invalid input, try again.");
sc.nextLine(); // flush scanner
return readChoice(i, j);
}
if (choice < i || choice > j) {
System.out.println("Invalid input, try again.");
return readChoice(i, j);
}
return choice;
}
/**
* The method is to read a {@code String} from standard input.
* @param message the message to be shown to the user
* @return the input from standard input
*/
public static String readString(String... message) {
for (String m : message) System.out.println(m);
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
/**
* This method is to read a {@code double} from standard input.
* @param message the message to be shown to the user
* @return the input from standard input
*/
public static double readDouble(String... message) {
for (String m : message) System.out.println(m);
Scanner sc = new Scanner(System.in);
double output;
try {
output = sc.nextDouble();
return output;
} catch (InputMismatchException ex) {
System.out.println("Invalid input, try again.");
sc.nextLine(); // flush scanner
return readDouble(message);
}
}
/**
* This method is to generate multiple spaces with given size.
* @param size the number of spaces to be generated
* @return the spaces generated
*/
public static String generateSpaces(int size) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < size; i++) stringBuilder.append(" ");
return stringBuilder.toString();
}
/**
* This method is to add line breaks for a {@code String} whose length exceeds a certain value
* as well as adding spaces to the second line onwards.
* @param input the String to be formatted
* @param maxLineLength the maximum length a line can be
* @param lengthOfSpace the number of spaces to be added to the second line onwards
* @return the formatted String
*/
public static String addLinebreaks(String input, int maxLineLength, int lengthOfSpace) {
StringTokenizer tok = new StringTokenizer(input, " ");
StringBuilder output = new StringBuilder(input.length());
int lineLen = 0;
while (tok.hasMoreTokens()) {
String word = tok.nextToken();
if (lineLen + word.length() > maxLineLength) {
output.append("\n");
for (int i = 0; i < lengthOfSpace; i++) output.append(" ");
lineLen = 0;
}
output.append(word).append(" ");
lineLen += word.length();
}
return output.toString();
}
/**
* This method is to read an Email address from standard input.
* @param message the message to be shown to the user
* @return the input from standard input with Email format
*/
public static String readEmail(String... message) {
for (String m : message) System.out.println(m);
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
Pattern EMAIL_PATTERN = Pattern.compile(
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" +
"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
Matcher matcher = EMAIL_PATTERN.matcher(input);
if (matcher.matches()) {
return input;
}
else {
System.out.println("Invalid Email address, try again.");
return readEmail(message);
}
}
// /**
// * This method is to map the {@code String} to respective {@code AgeRestriction}.
// * @param input the {@code String} to be mapped
// * @return the {@code AgeRestriction} the input mapped to
// */
// public static AgeRestriction readAgeRestriction(String input) {
// switch (input.toUpperCase()) {
// case "G":
// return AgeRestriction.G;
// case "PG":
// return AgeRestriction.PG;
// case "PG13":
// return AgeRestriction.PG13;
// case "NC16":
// return AgeRestriction.NC16;
// case "M18":
// return AgeRestriction.M18;
// case "R21":
// return AgeRestriction.R21;
// default:
// return null;
// }
// }
/**
* This method is to map the {@code String} to respective {@code MovieStatus}.
* @param input the {@code String} to be mapped
* @return the {@code MovieStatus} the input mapped to
*/
// public static MovieStatus readMovieStatus(String input) {
// switch (input.toUpperCase()) {
// case "COMING SOON":
// return MovieStatus.COMING_SOON;
// case "NOW SHOWING":
// return MovieStatus.NOW_SHOWING;
// case "END OF SHOWING":
// return MovieStatus.END_OF_SHOWING;
// default:
// return null;
// }
// }
/**
* This method is read a {@code String} with format MM-dd kk:mm from standard input and transform it to be a {@code Date}.
* @param message the message to be shown to the user
* @return the {@code Date} after formatting
*/
public static Date readTimeMMddkkmm(String... message) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
try {
String input = readString(message);
input = new SimpleDateFormat("yyyy").format(new Date()) + "-" + input; // set year as current year
Date date = simpleDateFormat.parse(input);
return date;
} catch (ParseException ex) {
System.out.println("Wrong format. Try again.");
return readTimeMMddkkmm(message);
}
}
/**
* This method is read a {@code String} with format MM-dd from standard input and transform it to be a {@code Date}.
* @param message the message to be shown to the user
* @return the {@code Date} after formatting
*/
public static Date readTimeMMdd(String... message) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
String input = readString(message);
input = new SimpleDateFormat("yyyy").format(new Date()) + "-" + input; // set year as current year
Date date = simpleDateFormat.parse(input);
return date;
} catch (ParseException ex) {
System.out.println("Wrong format. Try again.");
return readTimeMMdd(message);
}
}
/**
* This method is to ask user for confirmation from standard input.
* @param message the message to be shown to the user
* @return true if the input from standard input is Y, false otherwise
*/
public static boolean askConfirm(String... message) {
for (String m : message) System.out.println(m);
Scanner sc = new Scanner(System.in);
if (sc.next().toUpperCase().equals("Y")) return true;
else return false;
}
/**
* This method is to print specified {@code String} to standard output.
* @param menu the menu message to be written to standard output
*/
public static void printMenu(String... menu) {
for (String s : menu) {
System.out.println(s);
}
}
/**
* This method is to print specified {@code String} to standard output.
* @param header the header message to be written to standard output
*/
public static void printHeader(String header) {
int length = 65;
for (int i = 0; i < length; i++) System.out.print("-");
System.out.println();
int indent = (length - header.length()) / 2;
for (int i = 0; i < indent; i++) System.out.print(" ");
System.out.print(header);
for (int i = 0; i < indent; i++) System.out.print(" ");
System.out.println();
for (int i = 0; i < length; i++) System.out.print("-");
System.out.println();
}
/**
* This method is to format a {@code Date} to a {@code String} with format MMMM dd, kk:mm.
* @param time the {@code Date} to be formatted
* @return the {@code String} formatted
*/
public static String formatTimeMMddkkmm(Date time) {
return new SimpleDateFormat("MMMM dd, kk:mm").format(time);
}
/**
* This method is to format a {@code Date} to a {@code String} with format MMMM dd.
* @param time the {@code Date} to be formatted
* @return the {@code String} formatted
*/
public static String formatTimeMMdd(Date time) {
return new SimpleDateFormat("MMMM, dd").format(time);
}
/**
* This method is to test whether a {@code Date} is a weekend
* @param time the {@code Date} to be tested
* @return true if the {@code Date} is a weekend, false otherwise
*/
public static boolean isWeekend(Date time) {
String whatDay = new SimpleDateFormat("EEEE").format(time);
if (whatDay.equals("Saturday") || whatDay.equals("Sunday")) return true;
else return false;
}
/**
* This method is to test whether two {@code Date} equals in month and date
* @param d1 the first {@code Date} to be compared
* @param d2 the second {@code Date} to be compared
* @return true if they equals in month and date, false otherwise
*/
public static boolean dateEquals(Date d1, Date d2) {
return formatTimeMMdd(d1).equals(formatTimeMMdd(d2));
}
/**
* This method is used to round a double value to a specified decimal place.
* @param value the value to be rounded
* @param places the number of decimal places of the result
* @return the result after rounding
*/
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
}