Skip to content

Commit 75dbc73

Browse files
committed
Week3. Create a new class named ThirdRatings.
1 parent 7e3a8b2 commit 75dbc73

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Week3/src/FirstRatings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private int totalComedy() {
212212
}
213213

214214
// Make full filename for any OS types
215-
private String getFullFileName(String fileName) {
215+
private static String getFullFileName(String fileName) {
216216
if (!(new File(fileName).exists())) {
217217
fileName =
218218
System.getProperty("user.dir") + File.separator + "data" + File.separator + fileName;
@@ -346,7 +346,7 @@ private void printAllRatingsByRater(String raterID) {
346346
* This method should process every record from the CSV file whose name is
347347
* filename, a file of raters and their ratings
348348
*/
349-
private ArrayList<Rater> loadRaters(String fileName) {
349+
static ArrayList<Rater> loadRaters(String fileName) {
350350
ArrayList<Rater> ratersList = new ArrayList<>();
351351
ArrayList<String> idsList = new ArrayList<>();
352352

Week3/src/ThirdRatings.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.ArrayList;
2+
3+
public class ThirdRatings {
4+
5+
private final ArrayList<Rater> myRaters;
6+
private ArrayList<String> allMoviesIDs;
7+
8+
public ThirdRatings() {
9+
this("ratings.csv");
10+
allMoviesIDs = MovieDatabase.filterBy(new TrueFilter());
11+
}
12+
13+
public ThirdRatings(String ratingsFileName) {
14+
this.myRaters = FirstRatings.loadRaters(ratingsFileName);
15+
}
16+
17+
public ArrayList<Rating> getAverageRatings(int minimalRaters) {
18+
ArrayList<Rating> list = new ArrayList<>();
19+
for (String movieID : allMoviesIDs) {
20+
Double averageRating = getAverageByID(movieID, minimalRaters);
21+
if (averageRating != 0.0) {
22+
list.add(new Rating(movieID, averageRating));
23+
}
24+
}
25+
return list;
26+
}
27+
28+
/*
29+
This method returns a double representing the average movie rating for this ID
30+
if there are at least minimalRaters ratings.
31+
If there are not minimalRaters ratings, then it returns 0.0.
32+
*/
33+
Double getAverageByID(String movieID, Integer minimalRaters) {
34+
long numOfRatings = myRaters.stream().filter(rater -> rater.hasRating(movieID)).count();
35+
36+
if (numOfRatings >= minimalRaters) {
37+
return myRaters.stream()
38+
.filter(rater -> rater.hasRating(movieID))
39+
.mapToDouble(rating -> rating.getRating(movieID))
40+
.average()
41+
.orElse(0.0);
42+
}
43+
return 0.0;
44+
}
45+
} // class

0 commit comments

Comments
 (0)