Skip to content

Commit f72214c

Browse files
committed
Week2. getTitle
1 parent ffcdc6c commit f72214c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Week2/src/SecondRatings.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class SecondRatings {
1313
private final ArrayList<Rater> myRaters;
1414
private final FirstRatings firstRatings;
1515
private final HashMap<String, ArrayList<Double>> moviesAndRatings;
16+
private final HashMap<String, String> moviesIdAndTitles;
1617

1718
public SecondRatings() {
1819
// default constructor
@@ -27,6 +28,18 @@ public SecondRatings(String moviesFileName, String ratingsFileName) {
2728
myMovies = firstRatings.getMovieArrayList();
2829
myRaters = firstRatings.getRaterArrayList();
2930
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;
3043
}
3144

3245
// Returns a Map with movieID and all its ratings
@@ -104,4 +117,18 @@ public ArrayList<Rating> getAverageRatings(int minimalRaters) {
104117
}
105118
return list;
106119
}
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+
}
107134
}

0 commit comments

Comments
 (0)