@@ -13,6 +13,7 @@ public class SecondRatings {
13
13
private final ArrayList <Rater > myRaters ;
14
14
private final FirstRatings firstRatings ;
15
15
private final HashMap <String , ArrayList <Double >> moviesAndRatings ;
16
+ private final HashMap <String , String > moviesIdAndTitles ;
16
17
17
18
public SecondRatings () {
18
19
// default constructor
@@ -27,6 +28,18 @@ public SecondRatings(String moviesFileName, String ratingsFileName) {
27
28
myMovies = firstRatings .getMovieArrayList ();
28
29
myRaters = firstRatings .getRaterArrayList ();
29
30
moviesAndRatings = getMoviesAndRatingsMap ();
31
+ moviesIdAndTitles = getAllMoviesAndTitlesMap ();
32
+ }
33
+
34
+ // Get all movies and titles map
35
+ private HashMap <String , String > getAllMoviesAndTitlesMap () {
36
+ HashMap <String , String > map = new HashMap <>();
37
+ for (Movie movie : myMovies ) {
38
+ String movieID = movie .getID ();
39
+ String movieTitle = movie .getTitle ();
40
+ map .put (movieID , movieTitle );
41
+ }
42
+ return map ;
30
43
}
31
44
32
45
// Returns a Map with movieID and all its ratings
@@ -104,4 +117,18 @@ public ArrayList<Rating> getAverageRatings(int minimalRaters) {
104
117
}
105
118
return list ;
106
119
}
120
+
121
+ /*
122
+ * This method returns the title of the movie with that ID.
123
+ * If the movie ID does not exist, then this method should return a String
124
+ * indicating the ID was not found.
125
+ * */
126
+ String getTitle (String id ) {
127
+ String title = moviesIdAndTitles .get (id );
128
+ if (title != null ) {
129
+ return title ;
130
+ }
131
+ // No title found
132
+ return "ID " + id + " NOT FOUND" ;
133
+ }
107
134
}
0 commit comments