-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
335 lines (298 loc) · 13 KB
/
Driver.java
File metadata and controls
335 lines (298 loc) · 13 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
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Driver class is the main controller class that runs the program and controls the logic of reading input
* from the user, displaying appropiate menu and text, creating and storing objects in the database. This is done
* through methods of appropiate classes for each function.
*
* @author (Bhavik Maneck)
* @version (v1)
*/
public class Driver
{
private Database movieDatabase;
/**
* Constructor for objects of class Driver
*
* Loads a database of movies from file
*/
public Driver()
{
movieDatabase = new Database();
movieDatabase.loadDatabaseFromFile("myvideos.txt");
}
/*
* Deletes a movie from the database, if a matching title read from user is found.
* Also confirms with user before deleting.
*/
private void deleteMovie()
{
UserInput userInput = new UserInput();
String movieTitleToDelete = userInput.readStringFromUser("Enter movie title to delete: ", false);
// Search for the movie to delete by its title
ArrayList<Movie> moviesForDelete = movieDatabase.searchForMovie(movieTitleToDelete,"title",0);
if (moviesForDelete.size() == 0) //If no matching movie is found
{
System.out.print("\nNo movies found matching that title.\n\n");
}
else //Movie was found to delete
{
System.out.print("\n");
moviesForDelete.get(0).displayMovieInformation(); //There will only be one movie element as titles are unique so get it and display it
try
{
//Set up the confirmation menu with yes or no options
String deleteMenuTitle = "\nAre you sure you want to delete: " + moviesForDelete.get(0).getTitle() + " ?\n";
ArrayList<String> deleteMenuOptions = new ArrayList<String>();
deleteMenuOptions.add("Yes\n");
deleteMenuOptions.add("No\n");
Menu confirmDeleteMenu = new Menu(deleteMenuTitle,deleteMenuOptions);
//Initially set this to 0 so while loop will be entered
int deleteOptionChosen = 0;
//Keep asking for user to enter option until valid input received (readIntegerFromUser returns 0 for error)
while (deleteOptionChosen == 0)
{
userInput.clearScreen();
confirmDeleteMenu.displayMenu();
// Read which delete option the user wants (1 for yes 2 for no)
deleteOptionChosen = userInput.readIntegerFromUser(2, "Please choose an option between 1 and 2: ");
}
if (deleteOptionChosen == 1) //User wants to delete the movie
{
/*
* We can just get the first movie in the ArrayList of movies because only movies of one title can exist
* and we know we found a movie in our search
*/
movieDatabase.deleteMovie(moviesForDelete.get(0));
System.out.print("\nMovie successfuly deleted.\n\n");
}
else //User doesn't want to delete the movie
{
System.out.print("\nOk. movie won't be deleted.\n\n");
}
}
catch (IllegalStateException e)
{
System.out.print("\nError creating menu!\n");
}
}
}
/*
* Displays all movies with information for above and including a minimum rating read from the user
*/
private void displayFavouriteMovies()
{
UserInput userInput = new UserInput();
int minFavRating = 0; // Set to 0 initially so while loop asking for user to enter rating is entered
while (minFavRating == 0) // Keep asking user to enter number between 1 and 10
{
userInput.clearScreen();
//readIntegerFromUser returns 0 if number between 1 and 10 is not entered (so loop keeps going)
minFavRating = userInput.readIntegerFromUser(10, "Please choose a minimum favourite rating between 1 and 10: ");
}
ArrayList<Movie> favMovies = movieDatabase.searchForMovie("", "favourite", minFavRating);
if (favMovies.size() == 0)
{
System.out.print("\nNo movies found above or equal to that rating.\n\n");
}
else
{
System.out.print("\nFavourite Movies:\n\n");
Iterator<Movie> it = favMovies.iterator();
while (it.hasNext())
{
Movie movie = it.next();
movie.displayMovieInformation();
}
}
}
/*
* Run the program on the command line after compiling with: java Driver
*/
public static void main(String[] args){
Driver movieLibrary = new Driver();
movieLibrary.runDriver();
}
/*
* Read new movie information from user
*
* Returns a Movie object with information set
*/
private Movie readMovieInformationFromUser()
{
UserInput userInput = new UserInput();
String messagePromptForUser = "\nPlease enter the movie title: ";
boolean titleAlreadyExists = true; // Initially set to true so will enter while and start asking user to enter title
String title = "";
while (titleAlreadyExists) // Keep asking user for a title until one is entered that isn't already in the database
{
userInput.clearScreen();
title = userInput.readStringFromUser(messagePromptForUser, false);
ArrayList<Movie> moviesMatchingTitle = movieDatabase.searchForMovie(title,"title",0);
if (moviesMatchingTitle.size() == 0)
{
titleAlreadyExists = false; //Movie title doesn't exist in database so exit out of loop
}
else
{
System.out.print("\nError a movie with that title already exists!.\n\n");
userInput.pressEnterToContinue();
}
}
//Read movie director
messagePromptForUser = "\nPlease enter the movie director: ";
String director = userInput.readStringFromUser(messagePromptForUser, false);
//Read actor 1
messagePromptForUser = "\nPlease enter the frist movie actor: ";
String actor1 = userInput.readStringFromUser(messagePromptForUser, true);
//Read actor 2
messagePromptForUser = "\nPlease enter the second movie actor: ";
String actor2 = userInput.readStringFromUser(messagePromptForUser, true);
//Read actor 3
messagePromptForUser = "\nPlease enter the third movie actor: ";
String actor3 = userInput.readStringFromUser(messagePromptForUser, true);
//Read movie rating from user
messagePromptForUser = "Please enter the movie rating: ";
int ratingFromUser = 0; // Set to 0 initially so while loop is entered
while (ratingFromUser == 0)
{
//method returns 0 for error so will keep asking until valid integer is entered
ratingFromUser = userInput.readIntegerFromUser(10, messagePromptForUser);
}
try
{
Movie newMovie = new Movie(title, director, actor1, actor2, actor3, ratingFromUser);
// Return the new movie object with all the read in information
return newMovie;
}
catch (IllegalStateException e)
{
System.out.print(e.getMessage());
return null;
}
}
/*
* Main method for continuously displaying the menu, reading option from the user and calling program option methods
*/
public void runDriver()
{
boolean continueProgram = true;
int optionChosen = 0;
String menuTitle = "\nMovie Database\n";
ArrayList<String> menuOptions = new ArrayList<String>();
menuOptions.add("Search movie\n");
menuOptions.add("Add a movie\n");
menuOptions.add("Delete movie\n");
menuOptions.add("Display Favourite Movies\n");
menuOptions.add("Exit\n");
try
{
Menu driverMenu = new Menu(menuTitle,menuOptions);
UserInput userInput = new UserInput();
while (continueProgram)
{
userInput.clearScreen();
driverMenu.displayMenu();
//Read the integer the user inputted, 0 is returned by readIntegerFromUser for error
optionChosen = userInput.readIntegerFromUser(5, "Please choose an option between 1 and 5: ");
userInput.clearScreen();
switch (optionChosen)
{
// Search for movie by title or director
case 1:
searchForMovie();
userInput.pressEnterToContinue();
break;
// Read a new movie from the user and add it to the database of movies
case 2:
Movie newMovie = readMovieInformationFromUser();
movieDatabase.addMovie(newMovie);
break;
// Delete a movie from the database, by matching title
case 3:
deleteMovie();
userInput.pressEnterToContinue();
break;
// Display all movies above and including a minimum rating taken from the user
case 4:
displayFavouriteMovies();
userInput.pressEnterToContinue();
break;
// Exit the program, save all movies in database to file before finishing
case 5:
continueProgram = false;
movieDatabase.saveMoviesToFile("myvideos.txt");
movieDatabase.clearAll();
break;
// Out of bounds option
default:
System.out.print("\nError! That option does not exist!\n");
break;
}
}
}
catch (IllegalStateException e)
{
System.out.print("\nError creating menu!\n");
}
}
/*
* Search for a movie by either title or director, search is case-insensitive
*
* List of movies matching the search is displayed to the user
*/
private void searchForMovie()
{
try
{
//Set up the search menu with two options
String searchMenuTitle = "\nMovie Search\n";
ArrayList<String> searchMenuOptions = new ArrayList<String>();
searchMenuOptions.add("Search by title\n");
searchMenuOptions.add("Search by director\n");
Menu searchOptionMenu = new Menu(searchMenuTitle,searchMenuOptions);
UserInput userInput = new UserInput();
int searchOptionChosen = 0; //Initially set this to 0 so while loop will be entered
//Keep asking for user to enter option until valid input received (readIntegerFromUser returns 0 for error)
while (searchOptionChosen == 0)
{
userInput.clearScreen();
searchOptionMenu.displayMenu();
searchOptionChosen = userInput.readIntegerFromUser(2, "Please choose an option between 1 and 2: ");
}
// searchKey refers to the field in the Movie object we wish to search over
String searchKey = "";
if (searchOptionChosen == 1)
{
searchKey = "title";
}
else
{
searchKey = "director";
}
// searchString will be the string the user wants to search for
String searchString = userInput.readStringFromUser("Enter "+searchKey+" to search for: ", false);
ArrayList<Movie> foundMovies = movieDatabase.searchForMovie(searchString,searchKey,0);
if (foundMovies.size() == 0)
{
System.out.print("\nNo movies found.\n\n");
}
else
{
System.out.print("\n");
Iterator<Movie> it = foundMovies.iterator();
while (it.hasNext())
{
Movie movie = it.next();
movie.displayMovieInformation();
}
}
}
catch (IllegalStateException e)
{
System.out.print("\nError creating menu!\n");
}
}
}