-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserInterface.java
574 lines (539 loc) · 18.5 KB
/
UserInterface.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/**
* @author Brahma Dathan and Sarnath Ramnath
* @Copyright (c) 2010
* Modified by Matt Carlson, Jamison Czech, Slava Makharovich, Prashant Shrestha
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.StringTokenizer;
/**
*
* This class implements the user interface for the Theater project. The
* commands are encoded as integers using a number of static final variables. A
* number of utility methods exist to make it easier to parse the input.
*
*/
public class UserInterface {
private static UserInterface userInterface;
private BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
private static Theater theater;
private static final int EXIT = 0;
private static final int ADD_CLIENT = 1;
private static final int REMOVE_CLIENT = 2;
private static final int LIST_CLIENTS = 3;
private static final int ADD_CUSTOMER = 4;
private static final int REMOVE_CUSTOMER = 5;
private static final int ADD_CARD = 6;
private static final int REMOVE_CARD = 7;
private static final int LIST_CUSTOMERS = 8;
private static final int ADD_SHOW = 9;
private static final int LIST_SHOWS = 10;
private static final int SAVE = 11;
private static final int RETRIEVE = 12;
private static final int HELP = 13;
/**
* Made private for singleton pattern. Conditionally looks for any saved
* data. Otherwise, it gets a singleton Theater object.
*/
private UserInterface() {
if (yesOrNo("Search for previous theater data?")) {
retrieve();
} else {
theater = Theater.instance();
}
}
/**
* Supports the singleton pattern
*
* @return the singleton object
*/
public static UserInterface instance() {
if (userInterface == null) {
return userInterface = new UserInterface();
} else {
return userInterface;
}
}
/**
* Gets a token after prompting
*
* @param prompt
* - whatever the user wants as prompt
* @return - the token from the keyboard
*
*/
public String getToken(String prompt) {
do {
try {
System.out.println(prompt);
String line = reader.readLine();
StringTokenizer tokenizer = new StringTokenizer(line, "\n\r\f");
if (tokenizer.hasMoreTokens()) {
return tokenizer.nextToken();
}
} catch (IOException ioe) {
System.exit(0);
}
} while (true);
}
/**
* Queries for a yes or no and returns true for yes and false for no
*
* @param prompt
* The string to be prepended to the yes/no prompt
* @return
* true for yes and false for no
*/
private boolean yesOrNo(String prompt) {
String more = getToken(prompt + " (Y|y)[es] or anything else for no");
if (more.charAt(0) != 'y' && more.charAt(0) != 'Y') {
return false;
}
return true;
}
/**
* Converts the string to a number
*
* @param prompt
* the string for prompting
* @return
* the integer corresponding to the string
*
*/
public int getNumber(String prompt) {
do {
try {
String item = getToken(prompt);
Integer number = Integer.valueOf(item);
return number.intValue();
} catch (NumberFormatException nfe) {
System.out.println("Please input a number ");
}
} while (true);
}
/**
* Prompts for a date and gets a date object
*
* @param prompt
* the prompt
* @return the data as a Calendar object
*/
public Calendar getDate(String prompt) {
do {
try {
Calendar date = new GregorianCalendar();
String item = getToken(prompt);
DateFormat dateFormat = SimpleDateFormat
.getDateInstance(DateFormat.SHORT);
date.setTime(dateFormat.parse(item));
return date;
} catch (Exception fe) {
System.out.println("Please input a date as mm/dd/yy");
}
} while (true);
}
/**
* Prompts for a command from the keyboard
*
* @return
* a valid command
*/
public int getCommand() {
do {
try {
int value = Integer.parseInt(getToken("\nPlease select a choice from the menu," +
"\nEnter " + HELP + " for help: "));
if (value >= EXIT && value <= HELP) {
return value;
}
} catch (NumberFormatException nfe) {
System.out.println("Enter a number");
}
} while (true);
}
/**
* Displays the help screen
*
*/
public void help() {
System.out.println("Enter a number between 0 and 13 as explained below:");
System.out.println(EXIT + " to Exit\n");
System.out.println(ADD_CLIENT + " to add a client ");
System.out.println(REMOVE_CLIENT + " to remove client ");
System.out.println(LIST_CLIENTS + " to list all clients ");
System.out.println(ADD_CUSTOMER + " to add a customer ");
System.out.println(REMOVE_CUSTOMER + " to remove a customer ");
System.out.println(ADD_CARD + " to add a credit card");
System.out.println(REMOVE_CARD + " to remove a credit card");
System.out.println(LIST_CUSTOMERS + " to list all customers and credit cards");
System.out.println(ADD_SHOW + " to add a show");
System.out.println(LIST_SHOWS + " to list all shows");
System.out.println(SAVE + " to save data");
System.out.println(RETRIEVE + " to retrieve data");
System.out.println(HELP + " for help");
}
/**
* Method to be called for adding a client. Prompts the user for the
* appropriate values and uses the appropriate Theater method for adding the
* client.
*
*/
public void addClient() {
String name = getToken("Enter member name");
String address = getToken("Enter address");
String phone = getToken("Enter phone");
Client result;
result = theater.addClient(name, address, phone);
if (result == null) {
System.out.println("Could not add client");
}
System.out.println(result);
}
/**
* Method to be called for removing a client. Prompts the user for the
* appropriate value and uses the appropriate Theater method for removing
* the client if possible.
*
*/
public void removeClient() {
int result;
do {
String clientID = getToken("Enter client id");
result = theater.removeClient(clientID);
switch (result) {
case Theater.CLIENT_NOT_FOUND:
System.out.println("No such client");
break;
case Theater.CLIENT_HAS_SHOW:
System.out.println(
"Client cannot be removed. Client has a scheduled show.");
break;
case Theater.CLIENT_REMOVED:
System.out.println("Client was successfully removed");
break;
default:
System.out.println("There was an error");
break;
}
if (!yesOrNo("Remove more clients?")) {
break;
}
} while (true);
}
/**
* Method to be called for displaying clients. Uses the appropriate Theater
* method for displaying clients.
*
*/
public void getClientList() {
Iterator result;
result = theater.getClientList();
if (result == null) {
System.out.println("No clients have been added yet.");
} else {
while (result.hasNext()) {
Client client = (Client) result.next();
System.out.println(client.toString() + "\n");
}
System.out.println("\n There are no more clients \n");
}
}
/**
* Method to be called for adding a customer. Prompts the user for the
* appropriate values and uses the appropriate Theater method for adding the
* customer.
*
*/
public void addCustomer() {
Customer result;
do {
String name = getToken("Enter name");
String address = getToken("Enter address");
String phoneNumber = getToken("Enter phone number");
// needs to call card validation method
String cardNumber = getToken("Enter credit card number");
// needs to call expiration validation method
String expiration = getToken("Enter expiration date");
result = theater.addCustomer(name, address, phoneNumber, cardNumber,
expiration);
if (result != null) {
System.out.println(result);
} else {
System.out.println("Customer could not be added");
}
if (!yesOrNo("Add more customers?")) {
break;
}
} while (true);
}
/**
* Method to be called for removing a customer. Prompts the user for the
* appropriate value and uses the appropriate Theater method for removing
* the customer if possible.
*
*/
public void removeCustomer() {
int result;
do {
String customerID = getToken("Enter customer id");
result = theater.removeCustomer(customerID);
switch (result) {
case Theater.CUSTOMER_NOT_FOUND:
System.out.println("No such customer");
break;
case Theater.CUSTOMER_REMOVED:
System.out.println("Customer was successfully removed");
break;
default:
System.out.println("There was an error");
break;
}
if (!yesOrNo("Remove more customers?")) {
break;
}
} while (true);
}
/**
* Method to be called for add a credit cart. Prompts the user for the
* appropriate values and uses the appropriate Theater method for adding the
* credit card if possible.
*
*/
public void addCreditCard() {
int result;
do {
String customerID = getToken("Enter customer id");
// needs to call card validation method
String cardNumber = getToken("Enter credit card number");
// needs to call expiration validation method
String expiration = getToken("Enter expiration date");
result = theater.addCreditCard(customerID, cardNumber, expiration);
switch (result) {
case Theater.CUSTOMER_NOT_FOUND:
System.out.println("No such customer");
break;
case Theater.DUPLICATE_CARD:
System.out.println("Card already in the system");
break;
case Theater.CARD_ADDED:
System.out.println("Card successfully added");
break;
default:
System.out.println("There was an error");
break;
}
if (!yesOrNo("Add another card?")) {
break;
}
} while (true);
}
/**
* Method to be called for removing a credit card. Prompts the user for the
* appropriate value and uses the appropriate Theater method for removing
* the credit card if possible.
*
*/
public void removeCard() {
int result;
do {
String cardNumber = getToken("Enter cardNumber");
result = theater.removeCard(cardNumber);
switch (result) {
case Theater.CARD_NOT_FOUND:
System.out.println("Credit ");
break;
case Theater.ONLY_CARD:
System.out.println(
"Card cannot be removed. Customer must have at least one card on file.");
break;
case Theater.CARD_REMOVED:
System.out.println("Card was successfully removed");
break;
default:
System.out.println("There was an error");
break;
}
if (!yesOrNo("Remove more cards?")) {
break;
}
} while (true);
}
/**
* Method to be called for getting customer list. Uses the appropriate
* Theater method for getting customers.
*
*/
public void getCustomers() {
Iterator result = theater.getCustomers();
if (result == null) {
System.out.println("No customers in list");
return;
} else {
while (result.hasNext()) {
Customer customer = (Customer) result.next();
System.out.println("Customer: \n");
System.out.println(customer.toString());
Iterator cardList = customer.getCustomerCard();
while (cardList.hasNext()) {
CreditCard creditCard = (CreditCard) cardList.next();
System.out.println("Card number: "
+ creditCard.getCardNumber() + " Expiration: "
+ creditCard.getExpirationDate());
}
}
System.out.println("\n There are no more customers \n");
}
}
/**
* Method to be called for add a show. Prompts the user for the appropriate
* values and uses the appropriate Theater method for adding the show if
* possible.
*
*/
public void addShow() {
int result;
do {
String clientID = getToken("Enter client id");
String showName = getToken("Enter name of show");
Calendar startDate = getDate("Enter start date");
Calendar endDate = getDate("Enter end date");
result = theater.addShow(clientID, showName, startDate, endDate);
switch (result) {
case Theater.CLIENT_NOT_FOUND:
System.out.println("No such client");
break;
case Theater.DATE_NOT_OPEN:
System.out.println("Date range is unavailable");
break;
case Theater.SHOW_ADDED:
System.out.println("Show successfully added");
break;
default:
System.out.println("There was an error");
break;
}
if (!yesOrNo("Add another show?")) {
break;
}
} while (true);
}
/**
* Method to be called for displaying shows. Uses the appropriate Theater
* method for displaying shows.
*
*/
public void getShows() {
Iterator result;
result = theater.getShows();
if (result == null) {
System.out.println("No shows have been added");
} else {
while (result.hasNext()) {
Show show = (Show) result.next();
System.out.println(show.toString() + "\n");
}
System.out.println("\n There are no more shows \n");
}
}
/**
* Method to be called for saving the Theater object. Uses the appropriate
* Theater method for saving.
*
*/
private void save() {
if (theater.save()) {
System.out.println(
" The theater has been successfully saved in the file TheaterData \n");
} else {
System.out.println(" There has been an error in saving \n");
}
}
/**
* Method to be called for retrieving saved data. Uses the appropriate
* Theater method for retrieval.
*
*/
private void retrieve() {
try {
Theater tempTheater = Theater.retrieve();
if (tempTheater != null) {
System.out.println(
" The theater has been successfully retrieved from the file TheaterData \n");
theater = tempTheater;
} else {
System.out.println("File doesnt exist; creating new theater");
theater = theater.instance();
}
} catch (Exception cnfe) {
cnfe.printStackTrace();
}
}
/**
* Orchestrates the whole process. Calls the appropriate method for the
* different functionalities.
*
*/
public void process() {
int command;
help();
while ((command = getCommand()) != EXIT) {
switch (command) {
case ADD_CLIENT:
addClient();
break;
case REMOVE_CLIENT:
removeClient();
break;
case LIST_CLIENTS:
getClientList();
break;
case ADD_CUSTOMER:
addCustomer();
break;
case ADD_CARD:
addCreditCard();
break;
case REMOVE_CUSTOMER:
removeCustomer();
break;
case REMOVE_CARD:
removeCard();
break;
case LIST_CUSTOMERS:
getCustomers();
break;
case ADD_SHOW:
addShow();
break;
case LIST_SHOWS:
getShows();
break;
case SAVE:
save();
break;
case RETRIEVE:
retrieve();
break;
case HELP:
help();
break;
}
}
}
/**
* The method to start the application. Simply calls process().
*
* @param args
* not used
*/
public static void main(String[] args) {
UserInterface.instance().process();
}
}