|
| 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 | +} |
0 commit comments