Skip to content

Week2 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 3, 2021
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Week1/src/FirstRatings.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,25 @@
* @version 1.0
*/
public class FirstRatings {
public ArrayList<Movie> getMovieArrayList() {
return movieArrayList;
}

public ArrayList<Rater> getRaterArrayList() {
return raterArrayList;
}

private ArrayList<Movie> movieArrayList;

/**
* This method returns number of unique Raters
*
* @return int number of unique raters
*/
public int getRatersNumber() {
return ratersWithIds.size();
}

private ArrayList<Rater> raterArrayList;

private HashMap<String, HashSet<String>> directorsAndItsMovies;
Expand Down
12 changes: 12 additions & 0 deletions Week2/Week2.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="Week1" />
</component>
</module>
51 changes: 51 additions & 0 deletions Week2/src/MovieRunnerAverage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.util.ArrayList;
import java.util.Collections;

public class MovieRunnerAverage {
private final SecondRatings secondRatings;

public MovieRunnerAverage() {
secondRatings = new SecondRatings("ratedmoviesfull.csv", "ratings.csv");
}

public void printAverageRatings(int minimalRatings) {
// SecondRatings secondRatings = new SecondRatings("ratedmovies_short.csv",
// "ratings_short.csv");
// System.out.printf(
// "Total number of movies %d, total number of raters %d\n",
// secondRatings.getMovieSize(), secondRatings.getRaterSize());

/*
* In the MovieRunnerAverage class in the printAverageRatings method,
* add code to print a list of movies and their average ratings,
* for all those movies that have at least a specified number of ratings,
* sorted by averages.
* Specifically, this method should print the list of movies,
* one movie per line (print its rating first, followed by its title)
* in sorted order by ratings, lowest rating to highest rating.
* For example, if printAverageRatings is called on the files
* ratings_short.csv and ratedmovies_short.csv with the argument 3,
* then the output will display two movies:
*
* 8.25 Her
* 9.0 The Godfather
* */
ArrayList<Rating> ratedList = secondRatings.getAverageRatings(minimalRatings);
Collections.sort(ratedList);
System.out.printf("Total movies with %d ratings is %d\n", minimalRatings, ratedList.size());
System.out.printf(
"The name of the movie that has the lowest rating is \"%s\"\n",
secondRatings.getTitle(ratedList.get(0).getItem()));
// for (Rating ratedObj : ratedList) {
// double rating = ratedObj.getValue();
// String movieID = ratedObj.getItem();
// String movieTitle = secondRatings.getTitle(movieID);
// System.out.println(rating + " " + movieTitle);
// }
}

Double getAverageRatingOneMovie(String movieTitle) {
String movieID = secondRatings.getID(movieTitle);
return secondRatings.getAverageByID(movieID, 1);
}
}
151 changes: 151 additions & 0 deletions Week2/src/SecondRatings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Week2. A new class to do many of the calculations focusing on computing averages on movie
* ratings.
*
* @author Stanislav Rakitov
* @version 1.0
*/
public class SecondRatings {
private final ArrayList<Movie> myMovies;
private final ArrayList<Rater> myRaters;
private final FirstRatings firstRatings;
private final HashMap<String, ArrayList<Double>> moviesAndRatings;
private final HashMap<String, String> moviesIdAndTitles;

public SecondRatings() {
// default constructor
this("ratedmoviesfull.csv", "ratings.csv");
}

// The constructor should create a FirstRatings object and then call the loadMovies and
// loadRaters methods in FirstRatings to read in all the movie and ratings data and store them
// in the two private ArrayList variables of the SecondRatings class, myMovies and myRaters.
public SecondRatings(String moviesFileName, String ratingsFileName) {
firstRatings = new FirstRatings(moviesFileName, ratingsFileName);
myMovies = firstRatings.getMovieArrayList();
myRaters = firstRatings.getRaterArrayList();
moviesAndRatings = getMoviesAndRatingsMap();
moviesIdAndTitles = getAllMoviesAndTitlesMap();
}

// Get all movies and titles map
private HashMap<String, String> getAllMoviesAndTitlesMap() {
HashMap<String, String> map = new HashMap<>();
for (Movie movie : myMovies) {
String movieID = movie.getID();
String movieTitle = movie.getTitle();
map.put(movieID, movieTitle);
}
return map;
}

// Returns a Map with movieID and all its ratings
private HashMap<String, ArrayList<Double>> getMoviesAndRatingsMap() {
HashMap<String, ArrayList<Double>> map = new HashMap<>();

for (Movie movie : myMovies) {
map.putIfAbsent(movie.getID(), new ArrayList<>());
}

for (Rater rater : myRaters) {
ArrayList<String> ratings = rater.getItemsRated();
for (String movieID : ratings) {
// String movieID, double rating
ArrayList<Double> list = map.get(movieID);
list.add(rater.getRating(movieID));
}
}

return map;
}

/**
* A public method which returns the number of movies that were read in and stored in the
* ArrayList of type Movie
*
* @return int number of movies
*/
public int getMovieSize() {
return this.myMovies.size();
}

/**
* A public method which returns the number of raters that were read in and stored in the
* ArrayList of type Rater.
*
* @return int number of movies
*/
public int getRaterSize() {
return this.firstRatings.getRatersNumber();
}

/*
This method returns a double representing the average movie rating for this ID
if there are at least minimalRaters ratings.
If there are not minimalRaters ratings, then it returns 0.0.
*/
Double getAverageByID(String id, Integer minimalRaters) {
double result = 0.0;
ArrayList<Double> ratings = moviesAndRatings.get(id);
if (ratings != null && ratings.size() >= minimalRaters) {
result = ratings.stream().mapToDouble(d -> d).average().orElse(0.0);
}

// If there are no ratings returns 0.0
return result;
}

/**
* This method should find the average rating for every movie that has been rated by at least
* minimalRaters raters.
*
* @param minimalRaters int the minimal number of raters supplying a rating
* @return an ArrayList of all the Rating objects for movies that have at least the minimal number
* of raters supplying a rating
*/
public ArrayList<Rating> getAverageRatings(int minimalRaters) {
ArrayList<Rating> list = new ArrayList<>();
for (Movie movie : myMovies) {
String movieID = movie.getID();
Double averageRating = getAverageByID(movieID, minimalRaters);
if (averageRating != 0.0) {
list.add(new Rating(movieID, averageRating));
}
}
return list;
}

/*
* This method returns the title of the movie with that ID.
* If the movie ID does not exist, then this method should return a String
* indicating the ID was not found.
* */
String getTitle(String id) {
String title = moviesIdAndTitles.get(id);
if (title != null) {
return title;
}
// No title found
return "ID " + id + " NOT FOUND";
}
/*
* This method returns the movie ID of this movie.
* If the title is not found, return “NO SUCH TITLE.
* */
String getID(String title) {
String result = "NO SUCH TITLE";

for (Map.Entry<String, String> entry : moviesIdAndTitles.entrySet()) {
if (Objects.equals(title, entry.getValue())) {
return entry.getKey();
}
}

return result;
}
}
32 changes: 32 additions & 0 deletions Week2/src/Week2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Week2 {
public static void main(String[] args) {
MovieRunnerAverage average = new MovieRunnerAverage();

System.out.printf(
"Q5. What is the rating of the movie “The Maze Runner”? - %.4f\n",
average.getAverageRatingOneMovie("The Maze Runner"));

System.out.printf(
"Q6. What is the rating of the movie “Moneyball”? - %.4f\n",
average.getAverageRatingOneMovie("Moneyball"));

System.out.printf(
"Q7. what is the rating of the movie “Vacation”? - %.4f\n",
average.getAverageRatingOneMovie("Vacation"));

// Q8. Using the files ratedmoviesfull.csv and ratings.csv, how many movies have 50 or more
// ratings?
average.printAverageRatings(50);

// Q9. Using the files ratedmoviesfull.csv and ratings.csv, of those movies that have at least
// 20 ratings,
// what is the name of the movie that has the lowest rating?

average.printAverageRatings(20);

// Using the files ratedmoviesfull.csv and ratings.csv,
// of those movies that have at least 12 ratings,
// what is the name of the movie that has the lowest rating?
average.printAverageRatings(12);
}
}