Skip to content

Commit ab89a19

Browse files
committed
Week3. printAverageRatings(int minimalRatings)
1 parent 75dbc73 commit ab89a19

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
4+
public class MovieRunnerWithFilters {
5+
6+
/**
7+
* Print a list of movies and their average ratings sorted by averages
8+
*
9+
* @param minimalRatings int specified number of ratings
10+
*/
11+
public static void printAverageRatings(int minimalRatings) {
12+
ThirdRatings thirdRatings = new ThirdRatings("ratings_short.csv");
13+
ArrayList<Rating> ratedList = thirdRatings.getAverageRatings(minimalRatings);
14+
15+
Collections.sort(ratedList);
16+
// Print the number of raters after creating a ThirdsRating object.
17+
System.out.printf("Total movies with %d ratings is %d\n", minimalRatings, ratedList.size());
18+
19+
// You’ll call the MovieDatabase initialize method with the moviefile to set up the movie
20+
// database.
21+
MovieDatabase.initialize("ratedmoviesfull.csv");
22+
23+
// Print the number of movies in the database.
24+
System.out.println("The number of movies in the database is " + MovieDatabase.size());
25+
26+
// You will call getAverageRatings with a minimal number of raters to return an ArrayList of
27+
// type Rating.
28+
29+
ArrayList<Rating> averageRatings = thirdRatings.getAverageRatings(minimalRatings);
30+
31+
// Print out how many movies with ratings are returned,
32+
// then sort them, and print out the rating and title of each movie
33+
System.out.printf(
34+
"How many movies with ratings %d are returned: %d\n",
35+
minimalRatings, averageRatings.size());
36+
37+
averageRatings.stream()
38+
.sorted()
39+
.forEach(
40+
rating ->
41+
System.out.printf(
42+
"%-4s %s%n", rating.getValue(), MovieDatabase.getTitle(rating.getItem())));
43+
// System.out.printf(
44+
// "The name of the movie that has the lowest rating is \"%s\"\n",
45+
// thirdRatings.getTitle(ratedList.get(0).getItem()));
46+
// for (Rating ratedObj : ratedList) {
47+
// double rating = ratedObj.getValue();
48+
// String movieID = ratedObj.getItem();
49+
// String movieTitle = secondRatings.getTitle(movieID);
50+
// System.out.println(rating + " " + movieTitle);
51+
// }
52+
}
53+
}

Week3/src/ThirdRatings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
public class ThirdRatings {
44

55
private final ArrayList<Rater> myRaters;
6-
private ArrayList<String> allMoviesIDs;
6+
private final ArrayList<String> allMoviesIDs;
77

88
public ThirdRatings() {
99
this("ratings.csv");
10-
allMoviesIDs = MovieDatabase.filterBy(new TrueFilter());
1110
}
1211

1312
public ThirdRatings(String ratingsFileName) {
1413
this.myRaters = FirstRatings.loadRaters(ratingsFileName);
14+
this.allMoviesIDs = MovieDatabase.filterBy(new TrueFilter());
1515
}
1616

1717
public ArrayList<Rating> getAverageRatings(int minimalRaters) {

Week3/src/Week3.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Week3 {
2+
public static void main(String[] args) {
3+
// ThirdRatings week3 = new ThirdRatings();
4+
// ArrayList<Rating> averageRatings = week3.getAverageRatings(1);
5+
// averageRatings.forEach(System.out::println);
6+
MovieRunnerWithFilters.printAverageRatings(1);
7+
}
8+
}

0 commit comments

Comments
 (0)