-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSecondRatings.java
99 lines (80 loc) · 2.52 KB
/
SecondRatings.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* SecondRatings class contains various methods to extract information from the corresponding
* ArrayLists.
*
* @ Konstantin Krumin
* @ Version: 1.0 (February 18, 2020)
*/
import java.util.*;
public class SecondRatings {
private ArrayList<Movie> myMovies;
private ArrayList<Rater> myRaters;
public SecondRatings() {
// default constructor
this("ratedmoviesfull.csv", "ratings.csv");
}
public SecondRatings (String moviefile, String ratingsfile) {
FirstRatings firstRatings = new FirstRatings ();
myMovies = firstRatings.loadMovies(moviefile);
myRaters = firstRatings.loadRaters(ratingsfile);
}
public int getMovieSize () {
return myMovies.size();
}
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<Rating> averageRatings = new ArrayList<Rating> ();
for (Movie movie : myMovies) {
String movieID = movie.getID();
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 String getTitle (String id) {
String title = null;
for (Movie movie : myMovies) {
if (movie.getID().equals(id)) {
title = movie.getTitle();
}
}
if (title != null) {
return title;
} else {
return "No movie with such ID was found.";
}
}
public String getID (String title) {
String movieID = null;
for (Movie movie : myMovies) {
if (movie.getTitle().equals(title)) {
movieID = movie.getID();
}
}
if (movieID != null) {
return movieID;
} else {
return "NO SUCH TITLE.";
}
}
}