-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathThirdRatings.java
80 lines (65 loc) · 2.47 KB
/
ThirdRatings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* ThirdRatings class contains 2 methods that can be used to get average ratings
* (with/without filters) as well as additional helper methods.
*
* * getAverageRatings and getAverageRatingsByFilter are used to find movies that have required
* * number of ratings, and in case with getAverageRatingsByFilter can be filtered using a range
* * of filters provided in this program.
*
* @ Konstantin Krumin
* @ Version: 1.0 (February 19, 2020)
*/
import java.util.*;
public class ThirdRatings {
private ArrayList<Rater> myRaters;
public ThirdRatings () {
// default constructor
this("ratings.csv");
}
public ThirdRatings (String ratingsfile) {
FirstRatings firstRatings = new FirstRatings ();
myRaters = firstRatings.loadRaters(ratingsfile);
}
public int getRaterSize () {
return myRaters.size();
}
private double getAverageByID (String id, int minimalRaters) {
double sum = 0.0;
int count = 0;
for (Rater rater : myRaters) {
if (rater.hasRating(id)) {
sum += rater.getRating(id);
count += 1;
}
}
if (count >= minimalRaters) {
return sum / count;
} else {
return 0.0;
}
}
public ArrayList<Rating> getAverageRatings (int minimalRaters) {
ArrayList<String> movies = MovieDatabase.filterBy(new TrueFilter());
ArrayList<Rating> averageRatings = new ArrayList<Rating> ();
for (String movieID : movies) {
double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0;
if (average != 0.0) {
Rating rating = new Rating (movieID, average);
averageRatings.add(rating);
}
}
return averageRatings;
}
public ArrayList<Rating> getAverageRatingsByFilter (int minimalRaters, Filter filterCriteria) {
ArrayList<Rating> averageRatings = new ArrayList<Rating> ();
ArrayList<String> filteredMovies = MovieDatabase.filterBy(filterCriteria);
for (String movieID : filteredMovies) {
double average = Math.round(getAverageByID(movieID, minimalRaters) * 100.0) / 100.0;
if (average != 0.0) {
Rating rating = new Rating (movieID, average);
averageRatings.add(rating);
}
}
return averageRatings;
}
}