Skip to content

Commit cad6891

Browse files
author
Stanislav Rakitov
authored
Merge pull request #1 from ProsperousRF/Week2
Week2 Complete
2 parents 8b56a73 + 52689e0 commit cad6891

File tree

6 files changed

+265
-0
lines changed

6 files changed

+265
-0
lines changed

.idea/modules.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Week1/src/FirstRatings.java

+18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,25 @@
1515
* @version 1.0
1616
*/
1717
public class FirstRatings {
18+
public ArrayList<Movie> getMovieArrayList() {
19+
return movieArrayList;
20+
}
21+
22+
public ArrayList<Rater> getRaterArrayList() {
23+
return raterArrayList;
24+
}
25+
1826
private ArrayList<Movie> movieArrayList;
27+
28+
/**
29+
* This method returns number of unique Raters
30+
*
31+
* @return int number of unique raters
32+
*/
33+
public int getRatersNumber() {
34+
return ratersWithIds.size();
35+
}
36+
1937
private ArrayList<Rater> raterArrayList;
2038

2139
private HashMap<String, HashSet<String>> directorsAndItsMovies;

Week2/Week2.iml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module" module-name="Week1" />
11+
</component>
12+
</module>

Week2/src/MovieRunnerAverage.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
4+
public class MovieRunnerAverage {
5+
private final SecondRatings secondRatings;
6+
7+
public MovieRunnerAverage() {
8+
secondRatings = new SecondRatings("ratedmoviesfull.csv", "ratings.csv");
9+
}
10+
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());
17+
18+
/*
19+
* In the MovieRunnerAverage class in the printAverageRatings method,
20+
* add code to print a list of movies and their average ratings,
21+
* for all those movies that have at least a specified number of ratings,
22+
* sorted by averages.
23+
* Specifically, this method should print the list of movies,
24+
* one movie per line (print its rating first, followed by its title)
25+
* in sorted order by ratings, lowest rating to highest rating.
26+
* For example, if printAverageRatings is called on the files
27+
* ratings_short.csv and ratedmovies_short.csv with the argument 3,
28+
* then the output will display two movies:
29+
*
30+
* 8.25 Her
31+
* 9.0 The Godfather
32+
* */
33+
ArrayList<Rating> ratedList = secondRatings.getAverageRatings(minimalRatings);
34+
Collections.sort(ratedList);
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);
50+
}
51+
}

Week2/src/SecondRatings.java

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
import java.util.Objects;
5+
6+
/**
7+
* Week2. A new class to do many of the calculations focusing on computing averages on movie
8+
* ratings.
9+
*
10+
* @author Stanislav Rakitov
11+
* @version 1.0
12+
*/
13+
public class SecondRatings {
14+
private final ArrayList<Movie> myMovies;
15+
private final ArrayList<Rater> myRaters;
16+
private final FirstRatings firstRatings;
17+
private final HashMap<String, ArrayList<Double>> moviesAndRatings;
18+
private final HashMap<String, String> moviesIdAndTitles;
19+
20+
public SecondRatings() {
21+
// default constructor
22+
this("ratedmoviesfull.csv", "ratings.csv");
23+
}
24+
25+
// The constructor should create a FirstRatings object and then call the loadMovies and
26+
// loadRaters methods in FirstRatings to read in all the movie and ratings data and store them
27+
// in the two private ArrayList variables of the SecondRatings class, myMovies and myRaters.
28+
public SecondRatings(String moviesFileName, String ratingsFileName) {
29+
firstRatings = new FirstRatings(moviesFileName, ratingsFileName);
30+
myMovies = firstRatings.getMovieArrayList();
31+
myRaters = firstRatings.getRaterArrayList();
32+
moviesAndRatings = getMoviesAndRatingsMap();
33+
moviesIdAndTitles = getAllMoviesAndTitlesMap();
34+
}
35+
36+
// Get all movies and titles map
37+
private HashMap<String, String> getAllMoviesAndTitlesMap() {
38+
HashMap<String, String> map = new HashMap<>();
39+
for (Movie movie : myMovies) {
40+
String movieID = movie.getID();
41+
String movieTitle = movie.getTitle();
42+
map.put(movieID, movieTitle);
43+
}
44+
return map;
45+
}
46+
47+
// Returns a Map with movieID and all its ratings
48+
private HashMap<String, ArrayList<Double>> getMoviesAndRatingsMap() {
49+
HashMap<String, ArrayList<Double>> map = new HashMap<>();
50+
51+
for (Movie movie : myMovies) {
52+
map.putIfAbsent(movie.getID(), new ArrayList<>());
53+
}
54+
55+
for (Rater rater : myRaters) {
56+
ArrayList<String> ratings = rater.getItemsRated();
57+
for (String movieID : ratings) {
58+
// String movieID, double rating
59+
ArrayList<Double> list = map.get(movieID);
60+
list.add(rater.getRating(movieID));
61+
}
62+
}
63+
64+
return map;
65+
}
66+
67+
/**
68+
* A public method which returns the number of movies that were read in and stored in the
69+
* ArrayList of type Movie
70+
*
71+
* @return int number of movies
72+
*/
73+
public int getMovieSize() {
74+
return this.myMovies.size();
75+
}
76+
77+
/**
78+
* A public method which returns the number of raters that were read in and stored in the
79+
* ArrayList of type Rater.
80+
*
81+
* @return int number of movies
82+
*/
83+
public int getRaterSize() {
84+
return this.firstRatings.getRatersNumber();
85+
}
86+
87+
/*
88+
This method returns a double representing the average movie rating for this ID
89+
if there are at least minimalRaters ratings.
90+
If there are not minimalRaters ratings, then it returns 0.0.
91+
*/
92+
Double getAverageByID(String id, Integer minimalRaters) {
93+
double result = 0.0;
94+
ArrayList<Double> ratings = moviesAndRatings.get(id);
95+
if (ratings != null && ratings.size() >= minimalRaters) {
96+
result = ratings.stream().mapToDouble(d -> d).average().orElse(0.0);
97+
}
98+
99+
// If there are no ratings returns 0.0
100+
return result;
101+
}
102+
103+
/**
104+
* This method should find the average rating for every movie that has been rated by at least
105+
* minimalRaters raters.
106+
*
107+
* @param minimalRaters int the minimal number of raters supplying a rating
108+
* @return an ArrayList of all the Rating objects for movies that have at least the minimal number
109+
* of raters supplying a rating
110+
*/
111+
public ArrayList<Rating> getAverageRatings(int minimalRaters) {
112+
ArrayList<Rating> list = new ArrayList<>();
113+
for (Movie movie : myMovies) {
114+
String movieID = movie.getID();
115+
Double averageRating = getAverageByID(movieID, minimalRaters);
116+
if (averageRating != 0.0) {
117+
list.add(new Rating(movieID, averageRating));
118+
}
119+
}
120+
return list;
121+
}
122+
123+
/*
124+
* This method returns the title of the movie with that ID.
125+
* If the movie ID does not exist, then this method should return a String
126+
* indicating the ID was not found.
127+
* */
128+
String getTitle(String id) {
129+
String title = moviesIdAndTitles.get(id);
130+
if (title != null) {
131+
return title;
132+
}
133+
// No title found
134+
return "ID " + id + " NOT FOUND";
135+
}
136+
/*
137+
* This method returns the movie ID of this movie.
138+
* If the title is not found, return “NO SUCH TITLE.
139+
* */
140+
String getID(String title) {
141+
String result = "NO SUCH TITLE";
142+
143+
for (Map.Entry<String, String> entry : moviesIdAndTitles.entrySet()) {
144+
if (Objects.equals(title, entry.getValue())) {
145+
return entry.getKey();
146+
}
147+
}
148+
149+
return result;
150+
}
151+
}

Week2/src/Week2.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Week2 {
2+
public static void main(String[] args) {
3+
MovieRunnerAverage average = new MovieRunnerAverage();
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);
31+
}
32+
}

0 commit comments

Comments
 (0)