Skip to content

Commit 038369d

Browse files
committed
Week2. code to print a list of movies and their average ratings
1 parent f72214c commit 038369d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Week2/src/MovieRunnerAverage.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
14
public class MovieRunnerAverage {
25

36
public MovieRunnerAverage() {}
@@ -7,5 +10,29 @@ public void printAverageRatings() {
710
System.out.printf(
811
"Total number of movies %d, total number of raters %d\n",
912
secondRatings.getMovieSize(), secondRatings.getRaterSize());
13+
14+
/*
15+
* In the MovieRunnerAverage class in the printAverageRatings method,
16+
* add code to print a list of movies and their average ratings,
17+
* for all those movies that have at least a specified number of ratings,
18+
* sorted by averages.
19+
* Specifically, this method should print the list of movies,
20+
* one movie per line (print its rating first, followed by its title)
21+
* in sorted order by ratings, lowest rating to highest rating.
22+
* For example, if printAverageRatings is called on the files
23+
* ratings_short.csv and ratedmovies_short.csv with the argument 3,
24+
* then the output will display two movies:
25+
*
26+
* 8.25 Her
27+
* 9.0 The Godfather
28+
* */
29+
ArrayList<Rating> ratedList = secondRatings.getAverageRatings(3);
30+
Collections.sort(ratedList);
31+
for (Rating ratedObj : ratedList) {
32+
double rating = ratedObj.getValue();
33+
String movieID = ratedObj.getItem();
34+
String movieTitle = secondRatings.getTitle(movieID);
35+
System.out.println(rating + " " + movieTitle);
36+
}
1037
}
1138
}

0 commit comments

Comments
 (0)