-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackend.java
More file actions
286 lines (271 loc) · 9.43 KB
/
Backend.java
File metadata and controls
286 lines (271 loc) · 9.43 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
// --== CS400 File Header Information ==--
// Name: Pu Guo
// Email: pguo25@wisc.edu
// Team: JB Blue
// Role: Backend developer
// TA: Xinyi
// Lecturer: Gary Dahl
// Notes to Grader: Found bugs in HashTableMap class done in phase 2, in which I forgot to
// use the absolute value of hash code, fixed in this version
import java.util.List;
import java.util.zip.DataFormatException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
/**
* this class store Data got in files into two hash table, and access the information by selecting
* genres and ratings, modifier for genres, ratings, and the accessor for movies are implemented
* @author Pu Guo
*/
public class Backend implements BackendInterface{
private ArrayList<String> Genres;//store selected Genres
private ArrayList<String> Ratings;//store selected Ratings
private MapADT<String, List<MovieInterface>> hashGenre;//hash table that use genre as key
private MapADT<String, List<MovieInterface>> hashRating;//hash table that use rating as key
private ArrayList<MovieInterface> Movies; //selected movies
private List<String> AllGenre;
/**
* Constructor that takes in a FileReader and parse them into two hash tables, as well as
* initializing all the list, it also handles exceptions
* @param inputFileReader the file reader that contains data
*/
public Backend(Reader inputFileReader) {
Genres = new ArrayList<String>();
Ratings = new ArrayList<String>();
Movies = new ArrayList<MovieInterface>();
MovieDataReaderInterface reader = new MovieDataReader();
hashGenre = new HashTableMap<String, List<MovieInterface>>();
hashRating = new HashTableMap<String, List<MovieInterface>>();
AllGenre = new ArrayList<String>();
//initialize all field
try {
//retrieve the list of movie objects
List<MovieInterface> list = reader.readDataSet(inputFileReader);
for(int i =0;i<list.size();i++) {
//put into hashGenre table
//movie with multiple genres will be put in multiple times
for(int j =0;j<list.get(i).getGenres().size();j++){
if(!hashGenre.containsKey(list.get(i).getGenres().get(j))){
List<MovieInterface> mov = new ArrayList<MovieInterface>();
mov.add(list.get(i));
hashGenre.put(list.get(i).getGenres().get(j),mov);
AllGenre.add(list.get(i).getGenres().get(j));
}
else{
List<MovieInterface> mov = hashGenre.get(list.get(i).getGenres().get(j));
mov.add(list.get(i));
hashGenre.put(list.get(i).getGenres().get(j),mov);
}
}
//transfer rating from float to string of its first digit
double a = Math.floor(list.get(i).getAvgVote());
String rate = (int) a+"";
for(int j =0;j<=10;j++) {
List<MovieInterface> mov = new ArrayList<MovieInterface>();
hashRating.put(j+"", mov);
}
List<MovieInterface> mov = hashRating.get(rate);
mov.add(list.get(i));
hashRating.put(rate, mov);
}
//catch all exceptions
}catch(FileNotFoundException e1) {
System.out.println(e1.getMessage());
}catch(IOException e2) {
System.out.println(e2.getMessage());
}catch(DataFormatException e3) {
System.out.println(e3.getMessage());
}
}
/**
* Constructor that takes in a File path and parse the file into two hash tables, as well as
* initializing all the list, it also handles exceptions
* @param inputFileReader the file reader that contains data
*/
public Backend(String[] args){
Genres = new ArrayList<String>();
Ratings = new ArrayList<String>();
Movies = new ArrayList<MovieInterface>();
MovieDataReaderInterface reader = new MovieDataReader();
hashGenre = new HashTableMap<String, List<MovieInterface>>();
hashRating = new HashTableMap<String, List<MovieInterface>>();
AllGenre = new ArrayList<String>();
try {
//all same except we need to create File and FileReader ourselves
File f = new File(args[0]);
FileReader fReader =new FileReader(f);
List<MovieInterface> list = reader.readDataSet(fReader);
for(int i =0;i<list.size();i++) {
for(String genreName:list.get(i).getGenres()) {
if(!hashGenre.containsKey(genreName)) {
List<MovieInterface> mov = new ArrayList<MovieInterface>();
mov.add(list.get(i));
hashGenre.put(genreName, mov);
AllGenre.add(genreName);
}
else {
List<MovieInterface> mov = hashGenre.get(genreName);
mov.add(list.get(i));
hashGenre.put(genreName, mov);
}
}
double a = Math.floor(list.get(i).getAvgVote());
String rate = (int) a+"";
/*
if(!hashRating.containsKey(rate)) {
List<MovieInterface> mov = new ArrayList<MovieInterface>();
mov.add(list.get(i));
hashRating.put(rate, mov);
}
else {*/
for(int j =0;j<=10;j++) {
List<MovieInterface> mov = new ArrayList<MovieInterface>();
hashRating.put(j+"", mov);
}
List<MovieInterface> mov = hashRating.get(rate);
mov.add(list.get(i));
hashRating.put(rate, mov);
}
}catch(FileNotFoundException e1) {
System.out.println(e1.getMessage());
}catch(IOException e2) {
System.out.println(e2.getMessage());
}catch(DataFormatException e3) {
System.out.println(e3.getMessage());
}
}
/**
* Select a Genre
* @param genre selected genre type
*/
@Override
public void addGenre(String genre) {
Genres.add(genre);
//add all movie with selected genre first
for(int i =0;i<hashGenre.get(genre).size();i++) {
//ensure that each movie only added once
if(!Movies.contains(hashGenre.get(genre).get(i))) {
Movies.add(hashGenre.get(genre).get(i));
}
}
//remove not qualified movies
update();
}
/**
* Select a rating
* @param rating selected rating value
*/
@Override
public void addAvgRating(String rating) {
Ratings.add(rating);
for(int i =0;i<hashRating.get(rating).size();i++) {
if(!Movies.contains(hashRating.get(rating).get(i))) {
Movies.add(hashRating.get(rating).get(i));
}
}
update();
}
/**
* This method check if movies in Movies list still qualifies the selected genres and ratings
*/
private void update() {
ArrayList<MovieInterface> newMovies = new ArrayList<MovieInterface>();
//use a new arrayList to store all valid movies
//first, regain all the movies that has the indicated genre
for(int i =0;i<Genres.size();i++) {
for(int j =0;j<hashGenre.get(Genres.get(i)).size();j++) {
if(!Movies.contains(hashGenre.get(Genres.get(i)).get(j))) {
Movies.add(hashGenre.get(Genres.get(i)).get(j));
}
}
}
for(MovieInterface movie: Movies) {
boolean hasRating = false;
//check if its rating is selected
for(int i=0;i<Ratings.size();i++) {
if((Math.floor(movie.getAvgVote())+"").equals(Ratings.get(i)+".0")) {
hasRating = true;
}
}
boolean hasGenre = true;
for(int i =0;i<Genres.size();i++) {
//check if any of its genre is selected
if(!movie.getGenres().contains(Genres.get(i)))
hasGenre = false;
}
//if both are true, the movie can stay
if(Genres.size()!=0) {
if(hasRating && hasGenre)
newMovies.add(movie);
}
}
Movies = newMovies;
}
/**
* Not select a genre
* @param genre the type of genre need to be removed
*/
@Override
public void removeGenre(String genre) {
Genres.remove(genre);
update();
}
/**
* Not select a rating
* @param rating the type of rating need to be removed
*/
@Override
public void removeAvgRating(String rating) {
Ratings.remove(rating);
update();
}
/**
* Get all selected genres
* @return the list of selected genres
*/
@Override
public List<String> getGenres() {
return Genres;
}
/**
* Get all selected ratings
* @return the list of selected ratings
*/
@Override
public List<String> getAvgRatings() {
return Ratings;
}
/**
* Get size of selected movies
* @return the size of selected movies
*/
@Override
public int getNumberOfMovies() {
return Movies.size();
}
/**
* Get all possible genres
* @return the size of selected movies
*/
@Override
public List<String> getAllGenres() {
return AllGenre;
}
/**
* Get top 3 selected movies starting from given index
* @param statringIndex the index in the movies list to begin with
* @return the list of top movies
*/
@Override
public List<MovieInterface> getThreeMovies(int startingIndex) {
if(Movies.size()<=0 || startingIndex>= Movies.size())
return new ArrayList<MovieInterface>();
List<MovieInterface> sublist = Movies.subList(startingIndex, Movies.size());
Collections.sort(sublist);
return sublist.subList(0, Math.min(3, sublist.size()));
}
}