-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBackend.java
More file actions
316 lines (281 loc) · 9.22 KB
/
Copy pathBackend.java
File metadata and controls
316 lines (281 loc) · 9.22 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
// --== CS400 File Header Information ==--
// Name: Sean Erickson
// Email: smerickson4@wisc.edu
// Team: blue
// Role: back end developer
// TA: hang
// Lecturer: Gary
// Notes to Grader: <optional extra notes>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.zip.DataFormatException;
import java.io.Reader;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Backend implements BackendInterface {
private HashTableMap<String, List<MovieInterface>> genreHash;
private HashTableMap<String, List<MovieInterface>> ratingHash;
private List<MovieInterface> movieList;
private List<MovieInterface> sortedMovies;
private List<String> genres;
private List<String> ratings;
/**
* This constructor instantiates a backend with the command line arguments
* passed to the app when started by the user. The arguments are expected to
* contain the path to the data file that the backend will read in.
*
* @param args list of command line arguments passed to front end
*/
public Backend(String[] args) throws DataFormatException, FileNotFoundException, IOException {
String arg = args[0];
genres = new ArrayList<String>();
ratings = new ArrayList<String>();
sortedMovies = new ArrayList<MovieInterface>();
MovieDataReader movieReader = new MovieDataReader();
FileReader file = new FileReader(arg);
movieList = movieReader.readDataSet(file);
genreHash = new HashTableMap<String, List<MovieInterface>>(movieList.size());
ratingHash = new HashTableMap<String, List<MovieInterface>>(movieList.size());
hash();
}
/**
* A constructor that will create a backend from data that can be read in with a
* reader object.
*
* @param r A reader that contains the data in CSV format.
*/
public Backend(Reader r) throws IOException, DataFormatException {
genres = new ArrayList<String>();
ratings = new ArrayList<String>();
sortedMovies = new ArrayList<MovieInterface>();
MovieDataReader movieReader = new MovieDataReader();
movieList = movieReader.readDataSet(r);
// System.out.println(movieList.get(2).getGenres());
genreHash = new HashTableMap<String, List<MovieInterface>>(movieList.size() * 2);
ratingHash = new HashTableMap<String, List<MovieInterface>>(movieList.size() * 2);
hash();
}
public List<MovieInterface> getMovieList() {
return movieList;
}
/**
* Private helper that puts all movies into a hash table
*/
private void hash() {
// Puts all movies into a hashtable from the movieList based off genre
// Loop through all movies
for (int i = 0; i < movieList.size(); i++) {
// make temporary movie object for each i
MovieInterface movie = movieList.get(i);
// Loop through genres for each movie
for (int j = 0; j < movie.getGenres().size(); j++) {
// if the table does not contain the key, then make new list and add list to
// table
if (!genreHash.containsKey(movie.getGenres().get(j))) {
List<MovieInterface> newList = new ArrayList<MovieInterface>();
newList.add(movie);
genreHash.put(movie.getGenres().get(j), newList);
}
// otherwise if the table does contain the key, add to list and increase size
else if (genreHash.containsKey(movie.getGenres().get(j))) {
genreHash.get(movie.getGenres().get(j)).add(movie);
genreHash.increaseSize();
}
}
}
// put all movies into hashtable from movielist based off rating
// Loop through genres for each movie
for (int i = 0; i < movieList.size(); i++) {
// Make new temporary movie object and store rating as rounded down double
MovieInterface movie = movieList.get(i);
double movieRating = Math.floor(movie.getAvgVote());
int temp = (int) movieRating;
// Change rating to rounded down string to store in table
String rating = String.valueOf(temp);
// if the table does not contain the key, then make new list and add list to
// table
if (!ratingHash.containsKey(rating)) {
List<MovieInterface> newList = new ArrayList<MovieInterface>();
newList.add(movie);
ratingHash.put(rating, newList);
}
// otherwise if the table does contain the key, add to list and increase size
else if (ratingHash.containsKey(rating)) {
ratingHash.get(rating).add(movie);
ratingHash.increaseSize();
}
}
}
/*
* Private helper that sorts all values that fit criteria in a list
*/
private void sort() {
// Initialize two booleans
boolean swtch1;
boolean swtch2;
// Checks hash tables against lists to count all movies that fit criteria
for (int i = 0; i < movieList.size(); i++) {
swtch1 = true;
swtch2 = true;
MovieInterface movie = movieList.get(i);
// System.out.println(movie.getGenres());
if (!genres.isEmpty()) {
for (int j = 0; j < genres.size(); j++) {
if (!movie.getGenres().contains(genres.get(j))) {
if (sortedMovies.contains(movie))
sortedMovies.remove(movie);
swtch1 = false;
break;
}
}
} else if (genres.isEmpty() && ratings.isEmpty())
swtch1 = false;
else
swtch1 = true;
if (!ratings.isEmpty()) {
// Check that the rating is within search criteria
double movieRating = Math.floor(movie.getAvgVote());
int temp = (int) movieRating;
// Change rating to rounded down string to store in table
String rating = String.valueOf(temp);
if (!ratings.contains(rating)) {
swtch2 = false;
if (sortedMovies.contains(movie))
sortedMovies.remove(movie);
}
} else
swtch2 = true;
// If rating and genres match, add to list
if (swtch1 && swtch2) {
sortedMovies.add(movie);
}
}
Set<MovieInterface> set = new HashSet<>(sortedMovies);
sortedMovies.clear();
sortedMovies.addAll(set);
}
/**
* Method to add a genre that the user selected. It will output but not store
* the genres passed to it.
*/
@Override
public void addGenre(String genre) {
genres.add(genre);
sort();
}
/**
* Method to add a genre that the user selected. It will output but not store
* the ratings passed to it.
*/
@Override
public void addAvgRating(String rating) {
// Make sure rating is a string between 0-10
if (rating.equals("0") || rating.equals("1") || rating.equals("2") || rating.equals("3") || rating.equals("4")
|| rating.equals("5") || rating.equals("6") || rating.equals("7") || rating.equals("8")
|| rating.equals("9") || rating.equals("10")) {
ratings.add(rating);
sort();
} else
System.out.println("invalid rating");
}
/**
* Method to remove a genre that the user selected. It will output but not
* remove the genre passed to it from the backend object.
*/
@Override
public void removeGenre(String genre) {
genres.remove(genre);
sort();
}
/**
* Method to remove a rating that the user selected. It will output but not
* remove the genre passed to it from the backend object.
*/
@Override
public void removeAvgRating(String rating) {
// TODO: Fixme! Add dummy implementation similar to addGenre method.
ratings.remove(rating);
sort();
}
/**
* Return the genres added to this backend object.
*/
@Override
public List<String> getGenres() {
return genres;
}
/**
* Return the ratings added to this backend object.
*/
@Override
public List<String> getAvgRatings() {
return ratings;
}
/**
* Returns the number of movies.
*/
@Override
public int getNumberOfMovies() {
return sortedMovies.size();
}
/**
* Returns all genres in the dataset.
*/
@Override
public List<String> getAllGenres() {
List<String> rtn = new ArrayList<String>();
// loop through all movies
for (int i = 0; i < movieList.size(); i++) {
// loop through all genres of the movie at i
for (int j = 0; j < movieList.get(i).getGenres().size(); j++) {
// if list doesn't contain the genre already, remove it
if (!rtn.contains(movieList.get(i).getGenres().get(j))) {
rtn.add(movieList.get(i).getGenres().get(j));
}
}
}
return rtn;
}
/**
* Returns the movies that match the ratings and genres.
*/
@Override
public List<MovieInterface> getThreeMovies(int startingIndex) {
ArrayList<MovieInterface> movies = new ArrayList<MovieInterface>();
if (sortedMovies.isEmpty())
return movies;
try {
String str = String.valueOf(startingIndex);
while (!ratingHash.containsKey(str) && startingIndex != 0) {
startingIndex--;
str = String.valueOf(startingIndex);
}
for (int j = startingIndex; j > startingIndex - 3; j--) {
List<MovieInterface> temp = ratingHash.get(str);
if (temp.isEmpty())
j--;
else if (temp.size() == 1)
movies.add(temp.get(0));
else if (temp.size() > 1) {
for (int i = 0; i < temp.size(); i++) {
movies.add(temp.get(i));
if (j >= 0)
break;
j--;
}
}
}
Collections.sort(movies);
Collections.reverse(movies);
return movies;
} catch (IndexOutOfBoundsException e) {
return movies;
}
}
}