Skip to content

Commit 52689e0

Browse files
committed
Week2. Complete.
1 parent 26aa027 commit 52689e0

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

Week2/src/MovieRunnerAverage.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
import java.util.Collections;
33

44
public class MovieRunnerAverage {
5+
private final SecondRatings secondRatings;
56

6-
public MovieRunnerAverage() {}
7+
public MovieRunnerAverage() {
8+
secondRatings = new SecondRatings("ratedmoviesfull.csv", "ratings.csv");
9+
}
710

8-
public void printAverageRatings() {
9-
SecondRatings secondRatings = new SecondRatings("ratedmovies_short.csv", "ratings_short.csv");
10-
System.out.printf(
11-
"Total number of movies %d, total number of raters %d\n",
12-
secondRatings.getMovieSize(), secondRatings.getRaterSize());
11+
public void printAverageRatings(int minimalRatings) {
12+
// SecondRatings secondRatings = new SecondRatings("ratedmovies_short.csv",
13+
// "ratings_short.csv");
14+
// System.out.printf(
15+
// "Total number of movies %d, total number of raters %d\n",
16+
// secondRatings.getMovieSize(), secondRatings.getRaterSize());
1317

1418
/*
1519
* In the MovieRunnerAverage class in the printAverageRatings method,
@@ -26,13 +30,22 @@ public void printAverageRatings() {
2630
* 8.25 Her
2731
* 9.0 The Godfather
2832
* */
29-
ArrayList<Rating> ratedList = secondRatings.getAverageRatings(3);
33+
ArrayList<Rating> ratedList = secondRatings.getAverageRatings(minimalRatings);
3034
Collections.sort(ratedList);
31-
for (Rating ratedObj : ratedList) {
32-
double rating = ratedObj.getValue();
33-
String movieID = ratedObj.getItem();
34-
String movieTitle = secondRatings.getTitle(movieID);
35-
System.out.println(rating + " " + movieTitle);
36-
}
35+
System.out.printf("Total movies with %d ratings is %d\n", minimalRatings, ratedList.size());
36+
System.out.printf(
37+
"The name of the movie that has the lowest rating is \"%s\"\n",
38+
secondRatings.getTitle(ratedList.get(0).getItem()));
39+
// for (Rating ratedObj : ratedList) {
40+
// double rating = ratedObj.getValue();
41+
// String movieID = ratedObj.getItem();
42+
// String movieTitle = secondRatings.getTitle(movieID);
43+
// System.out.println(rating + " " + movieTitle);
44+
// }
45+
}
46+
47+
Double getAverageRatingOneMovie(String movieTitle) {
48+
String movieID = secondRatings.getID(movieTitle);
49+
return secondRatings.getAverageByID(movieID, 1);
3750
}
3851
}

Week2/src/SecondRatings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public int getRaterSize() {
8989
if there are at least minimalRaters ratings.
9090
If there are not minimalRaters ratings, then it returns 0.0.
9191
*/
92-
private Double getAverageByID(String id, Integer minimalRaters) {
92+
Double getAverageByID(String id, Integer minimalRaters) {
9393
double result = 0.0;
9494
ArrayList<Double> ratings = moviesAndRatings.get(id);
9595
if (ratings != null && ratings.size() >= minimalRaters) {

Week2/src/Week2.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
public class Week2 {
22
public static void main(String[] args) {
33
MovieRunnerAverage average = new MovieRunnerAverage();
4-
average.printAverageRatings();
4+
5+
System.out.printf(
6+
"Q5. What is the rating of the movie “The Maze Runner”? - %.4f\n",
7+
average.getAverageRatingOneMovie("The Maze Runner"));
8+
9+
System.out.printf(
10+
"Q6. What is the rating of the movie “Moneyball”? - %.4f\n",
11+
average.getAverageRatingOneMovie("Moneyball"));
12+
13+
System.out.printf(
14+
"Q7. what is the rating of the movie “Vacation”? - %.4f\n",
15+
average.getAverageRatingOneMovie("Vacation"));
16+
17+
// Q8. Using the files ratedmoviesfull.csv and ratings.csv, how many movies have 50 or more
18+
// ratings?
19+
average.printAverageRatings(50);
20+
21+
// Q9. Using the files ratedmoviesfull.csv and ratings.csv, of those movies that have at least
22+
// 20 ratings,
23+
// what is the name of the movie that has the lowest rating?
24+
25+
average.printAverageRatings(20);
26+
27+
// Using the files ratedmoviesfull.csv and ratings.csv,
28+
// of those movies that have at least 12 ratings,
29+
// what is the name of the movie that has the lowest rating?
30+
average.printAverageRatings(12);
531
}
632
}

0 commit comments

Comments
 (0)