-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontend.java
More file actions
334 lines (303 loc) · 13.8 KB
/
Frontend.java
File metadata and controls
334 lines (303 loc) · 13.8 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
import java.util.Scanner;
import java.util.List;
import java.lang.NumberFormatException;
public class Frontend {
private Backend BD;
private List<String> selectedGenres;
private List<String> selectedRatings;
private List<String> GENRES;
private final String[] RATINGS;
public static void main(String[] args) {
Frontend application = new Frontend(args);
application.run();
}
public Frontend(String[] args) {
this.BD = new Backend(args);
this.GENRES = BD.getAllGenres();
this.RATINGS = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
}
/**
* Runs the Movie Mapper program
*/
public void run() {
// add all ratings to selection; 0.0 - 10.0
addAllRatings();
System.out.println("========================================================\n");
System.out.println("Movie Mapper has successfully loaded!");
baseMode(1);
}
/**
* Allows user to select between base mode, genre selection mode, and rating selection mode
*
* @param inBaseMode indicates whether user is in base mode
* @param inGenreSelectionMode indicates whether user is in genre selection mode
* @param inRatingSelectionMode indicates whether user is in rating selection mode
*/
private void selection(boolean inBaseMode, boolean inGenreSelectionMode,
boolean inRatingSelectionMode) {
Scanner scan = null;
String selection = null;
boolean skipMessages = false;
int numberInput = 0;
if (inBaseMode) {
do {
scan = new Scanner(System.in);
selection = scan.next();
try {
numberInput = Integer.parseInt(selection);
baseMode(numberInput);
skipMessages = true;
break;
} catch (NumberFormatException ex) {
if (!selection.equals("g") && !selection.equals("r") && !selection.equals("x"))
System.out.print(
"Whoops! You didn't enter in a valid command. Please try again: ");
}
} while (!selection.equals("g") && !selection.equals("r") && !selection.equals("x"));
if (!skipMessages) {
switch (selection) {
case "g":
System.out.println(
"========================================================\n");
System.out.println("You are currently in genre selection mode...");
genreSelectionMode(false, true);
break;
case "r":
System.out.println(
"========================================================\n");
System.out.println("You are currently in rating selection mode...");
ratingSelectionMode(false, true);
break;
case "x":
System.out.println(
"========================================================\n");
System.out.println("You've selected exit. Goodbye!");
return;
}
}
} else if (inGenreSelectionMode) {
do {
try {
scan = new Scanner(System.in);
selection = scan.next();
numberInput = Integer.parseInt(selection);
if (!(numberInput > 0 && numberInput <= GENRES.size()))
throw new Exception();
break;
} catch (NumberFormatException ex) {
if (!selection.equals("x") && !selection.equals("s"))
System.out.print(
"Whoops! You didn't enter in a valid command. Please try again: ");
} catch (Exception ex) {
System.out.print(
"Whoops! The number that you choose if out of bounds. Please try again: ");
}
} while (!selection.equals("x") && !selection.equals("s"));
if (!selection.equals("x"))
if (selection.equals("s")) {
genreSelectionMode(true, true);
} else {
selectDeselectGenre(Integer.parseInt(selection), false);
}
else {
baseMode(1);
}
} else if (inRatingSelectionMode) {
do {
try {
scan = new Scanner(System.in);
selection = scan.next();
numberInput = Integer.parseInt(selection);
if (!(numberInput >= 0 && numberInput <= 10))
throw new Exception();
break;
} catch (NumberFormatException ex) {
if (!selection.equals("x") && !selection.equals("s"))
System.out.print(
"Whoops! You didn't enter in a valid command. Please try again: ");
} catch (Exception ex) {
System.out.print(
"Whoops! The number that you choose if out of bounds. Please try again: ");
}
} while (!selection.equals("x") && !selection.equals("s"));
if (!selection.equals("x"))
if (selection.equals("s")) {
ratingSelectionMode(true, true);
} else {
selectDeselectRating(Integer.parseInt(selection), false);
}
else {
baseMode(1);
}
}
}
/**
* Users will be able to scroll through the list by typing in numbers as commands of the rank
* (by rating) of the movies to display.
*
* @param startingIndex Number that corresponds to the first rank position
*/
private void baseMode(int startingIndex) {
// Base Mode
// get the top three movies by average rating
List<MovieInterface> baseModeMovies = BD.getThreeMovies(startingIndex - 1);
MovieInterface movie1 = null;
MovieInterface movie2 = null;
MovieInterface movie3 = null;
if (baseModeMovies.size() == 1) {
movie1 = baseModeMovies.get(0);
} else if (baseModeMovies.size() == 2) {
movie1 = baseModeMovies.get(0);
movie2 = baseModeMovies.get(1);
} else if (baseModeMovies.size() == 3) {
movie1 = baseModeMovies.get(0);
movie2 = baseModeMovies.get(1);
movie3 = baseModeMovies.get(2);
}
String title1 = movie1 == null ? "" : movie1.getTitle();
String title2 = movie2 == null ? "" : movie2.getTitle();
String title3 = movie3 == null ? "" : movie3.getTitle();
String rating1 = movie1 == null ? "" : "" + movie1.getAvgVote();
String rating2 = movie2 == null ? "" : "" + movie2.getAvgVote();
String rating3 = movie3 == null ? "" : "" + movie3.getAvgVote();
System.out.println("========================================================\n");
System.out.println("You are currently in base mode...");
System.out.println("Here are the movies ranked " + startingIndex + " to "
+ (startingIndex + 2) + " by average rating: \n");
System.out.printf("%-15s%-40s\n", "Average Vote", "Movie Title");
System.out.printf("%-15s%-40s\n", rating1, "(" + (startingIndex) + ") " + title1);
System.out.printf("%-15s%-40s\n", rating2, "(" + (startingIndex + 1) + ") " + title2);
System.out.printf("%-15s%-40s\n", rating3, "(" + (startingIndex + 2) + ") " + title3);
System.out.printf("\n%-15s%-15s\n", "Commands", "Purpose");
System.out.printf("%-15s%-15s\n", "(number)",
"to view the movies ranked <number> to <number + 2>");
System.out.printf("%-15s%-15s\n", "(g)", "to enter the genre selection mode");
System.out.printf("%-15s%-15s\n", "(r)", "to enter the rating selection mode");
System.out.printf("%-15s%-15s\n", "(x)", "to exit");
System.out.print("\nEnter in a command: ");
selection(true, false, false);
}
/**
* Selects all ratings (0-10) to start off with
*/
private void addAllRatings() {
int i = 0;
while (i <= 10) {
BD.addAvgRating("" + i);
i += 1;
}
}
/**
* Allows users to select or deselect genres
*
* @param genreNumber Number that corresponds to a genre
* @param showGenreSelections Shows selected genres if true
*/
private void selectDeselectGenre(int genreNumber, boolean showGenreSelections) {
if (elementInOtherList(selectedGenres, GENRES.get(genreNumber - 1))) {
System.out.print("Deselected: " + GENRES.get(genreNumber - 1) + "...");
BD.removeGenre(GENRES.get(genreNumber - 1));
} else {
BD.addGenre(GENRES.get(genreNumber - 1));
System.out.print("Selected: " + GENRES.get(genreNumber - 1) + "...");
}
genreSelectionMode(true, showGenreSelections);
}
/**
* Allows users to select or deselect ratings
*
* @param rating Number that corresponds to a genre
* @param showRatingSelections Shows selected genres if true
*/
private void selectDeselectRating(int rating, boolean showRatingSelections) {
if (elementInOtherList(selectedRatings, RATINGS[rating])) {
System.out.print("Deselected: " + RATINGS[rating] + "...");
BD.removeAvgRating(RATINGS[rating]);
} else {
System.out.print("Selected: " + RATINGS[rating] + "...");
BD.addAvgRating(RATINGS[rating]);
}
ratingSelectionMode(true, showRatingSelections);
}
/**
* Checks to see if an element is within another list
*
* @param list List to be searched
* @param element Element of interest
* @return true if element is in List
*/
private boolean elementInOtherList(List<String> list, String element) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(element))
return true;
}
return false;
}
/**
* The genre selection mode lets users select and deselect genres based on which movies are
* retrieved from the database.
*
* @param addedRemovedGenre Indicates whether a genre has been recently added/removed
* @param showGenreSelections Shows list of selected genres
*/
private void genreSelectionMode(boolean addedRemovedGenre, boolean showGenreSelections) {
selectedGenres = BD.getGenres();
if (!addedRemovedGenre) {
System.out.println(
"The genre(s) that you select/deselect will be used to filter your movie results.");
System.out.println("Selected genres are indicated with a \"*\"");
}
if (showGenreSelections) {
System.out.println("Here are all the selected/deselected genres: ");
System.out.printf("\n%-6s%-14s%-20s\n", "", "Genre", "Selected");
// generate list of selected/deselected genres
for (int i = 0; i < GENRES.size(); i++) {
System.out.printf("%-6s%-14s%-20s\n", "(" + (i + 1) + ") ", GENRES.get(i),
elementInOtherList(selectedGenres, GENRES.get(i)) ? "*" : "");
}
}
if (!addedRemovedGenre) {
System.out.printf("\n%-15s%-15s\n", "Commands", "Purpose");
System.out.printf("%-15s%-15s\n", "(number)",
"to select/deselect the genre associated with <number>");
System.out.printf("%-15s%-15s\n", "(s)",
"to show the list of selected/deselected genres");
System.out.printf("%-15s%-15s\n", "(x)", "to return to base mode");
}
System.out.print("\nEnter in a command: ");
selection(false, true, false);
}
/**
* The rating selection mode lets users select and deselect ratings based on which movies are
* retrieved from the database.
*
* @param addedRemovedRating Indicates whether a rating has been recently added/removed
* @param showRatingSelections Shows list of selected ratings
*/
private void ratingSelectionMode(boolean addedRemovedRating, boolean showRatingSelections) {
selectedRatings = BD.getAvgRatings();
if (!addedRemovedRating) {
System.out.println(
"The rating(s) that you select/deselect will be used to filter your movie results.");
System.out.println("Selected ratings are indicated with a \"*\"");
}
if (showRatingSelections) {
System.out.println("Here are all the selected/deselected ratings: ");
System.out.printf("\n%-14s%-20s\n", "Rating", "Selected");
// generate list of selected/deselected ratings
for (int i = 0; i < RATINGS.length; i++) {
System.out.printf("%-20s%-20s\n", RATINGS[i],
elementInOtherList(selectedRatings, RATINGS[i]) ? "*" : "");
}
}
if (!addedRemovedRating) {
System.out.printf("\n%-15s%-15s\n", "Commands", "Purpose");
System.out.printf("%-15s%-15s\n", "(number)", "to select/deselect a rating");
System.out.printf("%-15s%-15s\n", "(s)",
"to show the list of selected/deselected ratings");
System.out.printf("%-15s%-15s\n", "(x)", "to return to base mode");
}
System.out.print("\nEnter in a command: ");
selection(false, false, true);
}
}