Filip_Markoski

[NP] MoviesList

Dec 23rd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class MoviesTest {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         MoviesList moviesList = new MoviesList();
  8.         int n = scanner.nextInt();
  9.         scanner.nextLine();
  10.         for (int i = 0; i < n; ++i) {
  11.             String title = scanner.nextLine();
  12.             int x = scanner.nextInt();
  13.             int[] ratings = new int[x];
  14.             for (int j = 0; j < x; ++j) {
  15.                 ratings[j] = scanner.nextInt();
  16.             }
  17.             scanner.nextLine();
  18.             moviesList.addMovie(title, ratings);
  19.         }
  20.         scanner.close();
  21.         List<Movie> movies = moviesList.top10ByAvgRating();
  22.         System.out.println("=== TOP 10 BY AVERAGE RATING ===");
  23.         for (Movie movie : movies) {
  24.             System.out.println(movie);
  25.         }
  26.         movies = moviesList.top10ByRatingCoef();
  27.         System.out.println("=== TOP 10 BY RATING COEFFICIENT ===");
  28.         for (Movie movie : movies) {
  29.             System.out.println(movie);
  30.         }
  31.     }
  32. }
  33.  
  34. class MoviesList {
  35.     List<Movie> movies;
  36.  
  37.     MoviesList() {
  38.         movies = new ArrayList<>();
  39.     }
  40.  
  41.     public void addMovie(String title, int[] ratings) {
  42.         Movie movie = new Movie(title, ratings);
  43.         movies.add(movie);
  44.     }
  45.  
  46.     public List<Movie> top10ByAvgRating() {
  47.         return movies.stream()
  48.                 .sorted(
  49.                         Comparator.comparing(Movie::getAvgRating)
  50.                                 .reversed()
  51.                                 .thenComparing(Movie::getTitle)
  52.                 )
  53.                 .limit(10)
  54.                 .collect(Collectors.toList());
  55.     }
  56.  
  57.     public List<Movie> top10ByRatingCoef() {
  58.         return movies.stream()
  59.                 .sorted(
  60.                         /*new Comparator<Movie>() {
  61.                             @Override
  62.                             public int compare(Movie one, Movie two) {
  63.                                 double oneCoef = one.getAvgRating() * one.getTotalNumOfRatings() / movies.size();
  64.                                 double twoCoef = two.getAvgRating() * two.getTotalNumOfRatings() / movies.size();
  65.                                 return Double.compare(oneCoef, twoCoef);
  66.                             }
  67.                         }*/
  68.                         Comparator.comparing(Movie::getCoef)
  69.                         .reversed()
  70.                         .thenComparing(Movie::getTitle)
  71.                 )
  72.                 .limit(10)
  73.                 .collect(Collectors.toList());
  74.     }
  75. }
  76.  
  77. class Movie {
  78.     String title;
  79.     List<Integer> ratings;
  80.  
  81.     Movie(String title, int[] ratings) {
  82.         this.title = title;
  83.         this.ratings = Arrays.stream(ratings)
  84.                 .boxed()
  85.                 .collect(Collectors.toList());
  86.     }
  87.  
  88.     public String getTitle() {
  89.         return title;
  90.     }
  91.  
  92.     int getMaxRating() {
  93.         return ratings.stream()
  94.                 .max(Integer::compare)
  95.                 .get();
  96.     }
  97.  
  98.     int getTotalNumOfRatings() {
  99.         return ratings.size();
  100.     }
  101.  
  102.     double getAvgRating() {
  103.         return ratings.stream()
  104.                 .mapToDouble(Integer::doubleValue)
  105.                 .average()
  106.                 .orElse(0);
  107.     }
  108.  
  109.     @Override
  110.     public String toString() {
  111.         return String.format("%s (%.2f) of %d ratings", title, getAvgRating(), ratings.size());
  112.     }
  113.  
  114.     public double getCoef() {
  115.         return this.getAvgRating() * ratings.size();
  116.     }
  117. }
Add Comment
Please, Sign In to add comment