-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
663 lines (622 loc) · 31.4 KB
/
Main.java
File metadata and controls
663 lines (622 loc) · 31.4 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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/* FINAL PROJECT - CHI, EUGENIE, MADISON - DEC 20, 2021
This Main class should be run only after CreateSerialization has been run.
Main.java includes a Main Log-in Menu (Customer/Admin/Quit), 1 menu for admin and 1 menu for customer.
*/
import java.util.Scanner;
import java.io.*;
public class Main implements java.io.Serializable{
public static AllMovies listOfMovies ;
public static AllCustomers listOfCustomers ;
public static SearchMovie hashMovies ;
public static SortReleaseDate bstMovies;
public static void main(String[] args) {
System.out.println("\n================================");
/* Deserialization of objects prior to the execution of entire program */
// Deserialize allMovies
try
{
// Reading the object from a file
FileInputStream file = new FileInputStream("allMovies.ser");
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
listOfMovies = (AllMovies)in.readObject();
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
// Deserialize searchMovie
try
{
FileInputStream file = new FileInputStream("searchMovie.ser");
ObjectInputStream in = new ObjectInputStream(file);
hashMovies = (SearchMovie)in.readObject();
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
// Deserialize sortReleaseDate
try
{
FileInputStream file = new FileInputStream("sortReleaseDate.ser");
ObjectInputStream in = new ObjectInputStream(file);
bstMovies = (SortReleaseDate)in.readObject();
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
// Deserialize allCustomers
try
{
FileInputStream file = new FileInputStream("allCustomers.ser");
ObjectInputStream in = new ObjectInputStream(file);
listOfCustomers = (AllCustomers)in.readObject();
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
// ===========================================================================================================
/* Execution of main menu starts here */
Main.mMenu();
}
/* This is the main Log-In Menu with 3 options: Customer / Admin / Quit */
public static void mMenu() {
int choice = 0;
Scanner sc = new Scanner(System.in);
do {
System.out.println("\nWelcome to our program!");
System.out.println("1. Customer");
System.out.println("2. Administrator");
System.out.println("3. Quit");
try {
System.out.println("\nEnter option number: ");
choice = Integer.parseInt(sc.nextLine());
}
// catch if input is not an int
catch(NumberFormatException e) {
}
// Customer menu starts here
if (choice == 1) {
try {
System.out.println("\nEnter your credit card number (last 4 digits): ");
int userCredit = Integer.parseInt(sc.nextLine());
// if the credit card number already exists in the system (CreateSerialization.java),
// welcome back the existing customer and log them in
if (listOfCustomers.searchByCredit(userCredit) != null) {
System.out.println("\n\n*** Welcome back, " + listOfCustomers.searchByCredit(userCredit).getCustomerName() + "! ***");
Main.cMenu(userCredit);
// else, new customers can register with their name and emai (credit num already saved from userCredit input)
} else {
System.out.println("\n\n*** Welcome New Customer! ***");
System.out.println("Please register by typing your info below.");
System.out.println("Name: ");
String cusName = sc.nextLine();
System.out.println("Email: ");
String userEmail = sc.nextLine();
Customer newCus = new Customer(cusName, userCredit, userEmail);
listOfCustomers.insert(newCus);
// Serialize allMovies
try {
//Saving of object in a file
FileOutputStream file = new FileOutputStream("allMovies.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(listOfMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serializate searchMovie
try {
FileOutputStream file = new FileOutputStream("searchMovie.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(hashMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serialize sortReleaseDate
try {
FileOutputStream file = new FileOutputStream("sortReleaseDate.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(bstMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serialize allCustomers
try {
FileOutputStream file = new FileOutputStream("allCustomers.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(listOfCustomers);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
System.out.println("\nThank you for signing up! Please choose from the options below (1-6): ");
Main.cMenu(userCredit);
}
} catch(NumberFormatException e) {
System.out.println("\n-> Wrong format. Try again.");
Main.mMenu();
}
// Admin menu starts here
} else if (choice == 2) {
System.out.println("\nEnter admin email: ");
String adminEmail= sc.nextLine();
// one admin account, log in if credentials match
if (adminEmail.equals("admin@movies.com")) {
System.out.println("\nEnter admin password: ");
String adminPassword = sc.nextLine();
if (adminPassword.equals("212admin")) {
System.out.println("\n\n*** Welcome Admin! ***");
Main.aMenu();
} else {
System.out.println("\n-> Invalid credentials. Try again.");
Main.mMenu();
}
} else {
System.out.println("\n-> Invalid credentials. Try again.");
Main.mMenu();
}
// if input is none of the 3 options provided, print error
} else if (choice != 3) {
System.out.println("\n-> Error, not an option, pick again (1-3)");
Main.mMenu();
// else, quit and serialize all objects
} else {
System.out.println("\n*** Thank you for using our program! ***");
System.out.println();
// Serialize allMovies
try {
FileOutputStream file = new FileOutputStream("allMovies.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(listOfMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serialize searchMovie
try {
FileOutputStream file = new FileOutputStream("searchMovie.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(hashMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serialize sortReleaseDate
try {
FileOutputStream file = new FileOutputStream("sortReleaseDate.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(bstMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serialize allCustomers
try {
FileOutputStream file = new FileOutputStream("allCustomers.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(listOfCustomers);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
}
break;
} while (choice != 3);
}
/* This is the Customer Menu with 6 options */
public static void cMenu(int userCredit) {
int optionNum = 0;
Scanner sc = new Scanner(System.in);
do {
System.out.println("\n\nCUSTOMER MENU");
System.out.println("1. View All Movies"); // print all movies from AllMovie heap
System.out.println("2. Find Movie"); // lookup movie in SearchMovie hash
System.out.println("3. Watch Next"); // print wishlist from Queue Array Wishlist, fifo
System.out.println("4. Have Watched"); // print havewatched, which can be added from wishlist, from Queue Array WatchedMovies
System.out.println("5. Change Personal Info"); // update customer name or email in ser files
System.out.println("6. Log Out"); //return to Main Menu
//error checking of user's option input format
try {
System.out.println("\nEnter option number: ");
optionNum = Integer.parseInt(sc.nextLine());
}
catch(NumberFormatException e) {
}
if (optionNum == 1) {
System.out.println("\n*** All Movies ***");
System.out.println("Would you like to sort all movies by release date? (Yes/No)");
String userInput2 = sc.nextLine();
if (userInput2.equals("Yes")) {
System.out.println("\nPlease choose a sorting option");
System.out.println("a. Newest -> Oldest");
System.out.println("b. Oldest -> Newest");
String userSort = sc.nextLine();
if (userSort.equals("a")) {
System.out.println("-> Newest movies: ");
bstMovies.traversee();
} else if (userSort.equals("b")) {
System.out.println("-> Oldest movies: ");
bstMovies.traverse();
}
} else if (userInput2.equals("No")) {
System.out.println("\n-> Unordered movie list: ");
listOfMovies.printAllMovies();
} else {
System.out.println("\n-> Wrong input. Try again.");
}
// search movie by ID, if the movie is available, the customer can add it to their wishlist (if they'd like to)
} else if (optionNum == 2) {
try {
System.out.println("\n*** Find movie by ID ***");
System.out.println("Enter your movie ID: ");
int user_movieID = Integer.parseInt(sc.nextLine());
Movie result = hashMovies.lookup(user_movieID);
if (result != null) {
if (result.checkStatus() == true) {
System.out.println("Status: Available");
System.out.println("\nWould you like to add " + result.getTitle() + " to your wishlist? (Yes/No)");
String userInput1 = sc.nextLine();
if (userInput1.equals("Yes")) {
listOfCustomers.searchByCredit(userCredit).getWishList().addMovie(result);
System.out.println("\n-> Here's your current wishlist: ");
listOfCustomers.searchByCredit(userCredit).getWishList().displayWishList(); //movie found is added to wishlist
} else if (userInput1.equals("No")) {
System.out.println();
} else {
System.out.println("\n-> Wrong input. Try again.");
}
} else if (result.checkStatus() == false) {
System.out.println("Status: Unvailable");
System.out.println("\n-> Sorry, this movie is not available to watch.");
}
}
} catch(NumberFormatException e) {
System.out.println("\n-> Wrong format. Try again.");
}
// first return the next movie in WishList, then ask if the customer have watched the movie,
// if yes, it will be removed from wishlist and added to havewatched list
} else if (optionNum == 3) {
System.out.println("\n*** Watch Next ***");
if (listOfCustomers.searchByCredit(userCredit).getWishList().firstMovie() != null) {
Movie x = listOfCustomers.searchByCredit(userCredit).getWishList().firstMovie();
if (x.checkStatus() == true) {
System.out.println("Next movie to watch: " + x.getTitle());
System.out.println("-> This movie is available");
System.out.println("\nDid you watch " + x.getTitle() + "? (Yes/No)");
String userWishlist = sc.nextLine();
if (userWishlist.equals("Yes")) {
System.out.println("-> " + x.getTitle() + " has been deleted from your wishlist");
listOfCustomers.searchByCredit(userCredit).getWatched().addMovie(x);
x = x.getNext();
listOfCustomers.searchByCredit(userCredit).getWishList().deleteMovie();
} else if (userWishlist.equals("No")) {
System.out.println("-> " + x.getTitle() + " is kept in your wishlist");
} else {
System.out.println("\n-> Wrong input. Try again.");
}
} else if (x.checkStatus() == false) {
System.out.println("* Next movie: " + x.getTitle() + ".");
System.out.println("Sorry, movie not available to watch.");
System.out.println("-> " + x.getTitle() + " is deleted from wishlist.");
listOfCustomers.searchByCredit(userCredit).getWishList().deleteMovie();
}
} else {
System.out.println("-> There's currently no movie to watch next. Please add movie(s) to your wishlist first.");
}
// print movies the customer has watched, which can only be added from wishlist
} else if (optionNum == 4) {
System.out.println("\n*** Have Watched ***");
System.out.println("Movies you have watched: ");
listOfCustomers.searchByCredit(userCredit).getWatched().displayWatchedMovies();
// customers can change either or both of their personal info (name/email)
} else if (optionNum == 5) {
System.out.println("\n*** Enter your updated info below ***");
System.out.println("Name: ");
String newName = sc.nextLine();
System.out.println("Email: ");
String newEmail = sc.nextLine();
listOfCustomers.searchByCredit(userCredit).setCustomerName(newName);
listOfCustomers.searchByCredit(userCredit).setCustomerEmail(newEmail);
// Serialization of objects to save customer's updated info abvove
// Serialize allMovies
try {
FileOutputStream file = new FileOutputStream("allMovies.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(listOfMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serialize searchMovie
try {
FileOutputStream file = new FileOutputStream("searchMovie.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(hashMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serialize sortReleaseDate
try {
FileOutputStream file = new FileOutputStream("sortReleaseDate.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(bstMovies);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
// Serialize allCustomers
try {
FileOutputStream file = new FileOutputStream("allCustomers.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(listOfCustomers);
out.close();
file.close();
}
catch(IOException ex) {
System.out.println("IOException is caught");
}
} else if (optionNum != 6) {
System.out.println("\n-> Error, not an option, pick again(1-3)");
}
} while (optionNum != 6);
System.out.println("\n\n*** You have logged out successfully ***\n\n");
Main.mMenu();
}
/* This is the Admin Menu with 7 options */
// Deserialization of objects to update customer's changed info
public static void aMenu() {
// Deserialize allMovies
try
{
FileInputStream file = new FileInputStream("allMovies.ser");
ObjectInputStream in = new ObjectInputStream(file);
listOfMovies = (AllMovies)in.readObject();
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
// Deserialize searchMovie
try
{
FileInputStream file = new FileInputStream("searchMovie.ser");
ObjectInputStream in = new ObjectInputStream(file);
hashMovies = (SearchMovie)in.readObject();
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
// Deserialize sortReleaseDate
try
{
FileInputStream file = new FileInputStream("sortReleaseDate.ser");
ObjectInputStream in = new ObjectInputStream(file);
bstMovies = (SortReleaseDate)in.readObject();
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
// Deserialize allCustomers
try
{
FileInputStream file = new FileInputStream("allCustomers.ser");
ObjectInputStream in = new ObjectInputStream(file);
listOfCustomers = (AllCustomers)in.readObject();
in.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
int adChoice = 0;
Scanner sc = new Scanner(System.in);
do {
System.out.println("\n\nADMINISTRATOR MENU");
System.out.println("1. View All Existing Customers");
System.out.println("2. Find Customer");
System.out.println("3. View All Movies");
System.out.println("4. Add New Movie");
System.out.println("5. Find and Set Least Rated Movie to Unavailable");
System.out.println("6. Find and Delete Movie");
System.out.println("7. Log Out");
// error checking of user's option input format
try {
System.out.println("\nEnter option number: ");
adChoice = Integer.parseInt(sc.nextLine());
} catch(NumberFormatException e) {
}
// print all customer info from AllCustomers hash
if (adChoice == 1) {
System.out.println("\n*** All Customers ***");
listOfCustomers.printCustomerList();
// find a customer based on the last 4 digits of their credit card number
} else if (adChoice == 2) {
try {
System.out.println("\n*** Find a Customer ***");
System.out.println("Enter customer credit card number (last 4 digits): ");
int cust_ID = Integer.parseInt(sc.nextLine());
Customer result = listOfCustomers.searchByCredit(cust_ID);
if (result != null) {
System.out.println("\n-> Customer found");
System.out.println("Name: " + result.getCustomerName());
System.out.println("Email: " + result.getCustomerEmail());
System.out.println("Credit Card Number (last 4 digits): " + result.getCreditNum());
} else {
System.out.println("-> Customer not found");
}
} catch(NumberFormatException e) {
System.out.println("\nWrong format. Try again.");
}
// print all movies from AllMovies heap
} else if (adChoice == 3) {
System.out.println("\n*** All Movies ***");
listOfMovies.printAllMovies();
// add a new movie, check if it's already in the database, if not, ask for movie info
// admin also has the choice to view the updated movie list right after adding a new one
} else if (adChoice == 4) {
try {
System.out.println("\nEnter ID of new movie: ");
int newID = Integer.parseInt(sc.nextLine());
if (hashMovies.lookup(newID) != null) {
System.out.println("-> Movie ID already exists. Please try again");
} else {
System.out.println(" \n*** Complete New Movie Info ***");
System.out.println("\nTitle: ");
String newTitle = sc.nextLine();
System.out.println("Release Date (YYYYMMDD): ");
int newRD = Integer.parseInt(sc.nextLine());
System.out.println("Rating (0-100): ");
int newRate = Integer.parseInt(sc.nextLine());
Movie newMovie = new Movie(newTitle, newRD, newID, newRate);
listOfMovies.insert(newMovie);
bstMovies.insert(newMovie);
hashMovies.insert(newMovie);
System.out.println("-> " + newMovie.getTitle() + " has been added to movie list.");
System.out.println("\n-> View updated movie list? (Yes/No)");
String viewChoice = sc.nextLine();
if (viewChoice.equals("Yes")) {
System.out.println();
listOfMovies.printAllMovies();
} else {
Main.aMenu();
}
}
} catch(NumberFormatException e) {
System.out.println("\n-> Wrong format. Try again.");
}
// see the least rated movie and admin has the option set it to unavailble
// so that it can be deleted later (in admin option 5)
} else if (adChoice == 5) {
System.out.println("\n*** Find and Set Least Rated Movie to Unavailable ***");
Movie least1 = listOfMovies.findLeastRT();
int leastID = least1.getID();
Movie least_hash = hashMovies.lookup(leastID);
System.out.println("\n-> " + least1.getTitle() + " is the least rated movie.");
System.out.println("Change " + least1.getTitle() + "'s status to unavailable? (Yes/No)");
String adminInput2 = sc.nextLine();
if (adminInput2.equals("Yes")) {
System.out.println("\n-> " + least1.getTitle()+ " has been made unavailable.");
least1.setToUnavailable();
least_hash.setToUnavailable();
System.out.println("-> Check " + least1.getTitle() + "'s status again: isAvailable = " + least1.checkStatus() + ".");
} else if (adminInput2.equals("No")) {
System.out.println("-> " + least1.getTitle()+ " is kept available.");
} else {
System.out.println("-> Wrong input. Try again.");
}
// search a movie by ID, delete a movie only if it's unavailable
} else if (adChoice == 6) {
try {
System.out.println("\n*** Search and Delete Movie ***");
System.out.println("Enter the movie ID: ");
int admin_movieID = Integer.parseInt(sc.nextLine());
Movie result = hashMovies.lookup(admin_movieID);
if (result != null) {
if (result.checkStatus() == false) {
System.out.println("Status: Unavailable");
System.out.println("\nWould you like to delete " + result.getTitle() + "? (Yes/No)");
String adminInput1 = sc.nextLine();
if (adminInput1.equals("Yes")) {
hashMovies.delete(admin_movieID);
listOfMovies.deleteLeastRT();
System.out.println("-> " + result.getTitle() + " has been deleted.");
System.out.println("\n-> Here's the updated movie list: ");
listOfMovies.printAllMovies();
} else if (adminInput1.equals("No")) {
System.out.println("\n-> " + result.getTitle() + "is kept in movie list.");
}
} else if (result.checkStatus() == true) {
System.out.println("\nCareful! " + result.getTitle() + " is available.");
System.out.println("You don't want to delete an available movie.");
}
}
} catch(NumberFormatException e) {
System.out.println("\n-> Wrong format. Try again.");
}
} else if (adChoice != 7) {
System.out.println("\n-> Error, not an option, pick again(1-3)");
}
} while (adChoice != 7);
System.out.println("\n\n*** You have logged out successfully ***\n\n");
Main.mMenu();
}
}