-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecondRatings.java
120 lines (90 loc) · 2.26 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Write a description of SecondRatings here.
*
* @author (your name)
* @version (a version number or a date)
*/
import edu.duke.*;
import java.util.*;
public class SecondRatings {
private ArrayList<Movie> myMovies;
private ArrayList<Rater> myRaters;
public SecondRatings() {
// default constructor
this("ratedmovies", "ratings.csv");
}
public SecondRatings(String moviefile, String ratingsfile) {
//Create a FirstRatings Object
System.out.println("I am in Second rating");
FirstRatings fr = new FirstRatings();
myMovies=fr.loadMovies(moviefile);
myRaters=fr.loadRaters(ratingsfile);
}
public int getMovieSize(){
return myMovies.size();
}
public int getRaterSize(){
return myRaters.size();
}
private double getAveragebyId(String movieId,int minimalRaters){
int numOfRaters=0;
double sum =0.0;
for (Rater rater:myRaters){
if(rater.hasRating(movieId)){
numOfRaters++;
}
}
if (numOfRaters>=minimalRaters){
for (Rater rater1:myRaters){
if(rater1.hasRating(movieId)){
sum+=rater1.getRating(movieId);
}
}
double ave= sum/numOfRaters;
return ave;
}
else{
return 0.0;
}
}
public ArrayList<Rating> getAverageRatings (int minimalRaters){
ArrayList<Rating> rating = new ArrayList<Rating>();
for (Movie movie:myMovies){
String movie_Id= movie.getID();
if (getAveragebyId(movie_Id,minimalRaters)!=0.0){
Rating rat = new Rating(movie_Id,getAveragebyId(movie_Id,minimalRaters));
rating.add(rat);
}
}
Collections.sort(rating);
return rating;
}
public String getTitle(String movie_Id){
for (Movie movie:myMovies){
if (movie.getID().equals(movie_Id)){
return movie.getTitle();
}
}
return "Movie not Found";
}
public String getId(String movieTitle){
for (Movie movie:myMovies){
if (movie.getTitle().equals(movieTitle)){
return movie.getID();
}
}
return "No Such Title";
}
public double averageRatingMovie(String movie_id){
double numOfRaters=0.0;
double sum =0.0;
for (Rater rater:myRaters){
if(rater.hasRating(movie_id)){
numOfRaters++;
sum+= rater.getRating(movie_id);
}
}
double ave= sum/numOfRaters;
return ave;
}
}