Advertisement
Martina312

[НП] - Најдобри филмови

Aug 21st, 2020
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. class Movie implements Comparable<Movie>{
  5.     private String title;
  6.     private int [] ratings;
  7.  
  8.     public Movie(String title, int[] ratings) {
  9.         this.title = title;
  10.         this.ratings = ratings;
  11.     }
  12.  
  13.     public String getTitle() {
  14.         return title;
  15.     }
  16.  
  17.     public int getRatingsNum() {
  18.         return ratings.length;
  19.     }
  20.  
  21.     public double getAverageRating(){
  22.         return Arrays.stream(ratings).sum() / (ratings.length*1.0);
  23.     }
  24.  
  25.     public double getRatingCoef(int maxRatings){
  26.         return getAverageRating() * ratings.length / maxRatings;
  27.     }
  28.  
  29.     @Override
  30.     public String toString() {
  31.         return String.format("%s (%.2f) of %d ratings", title, getAverageRating(), ratings.length);
  32.     }
  33.  
  34.     @Override
  35.     public int compareTo(Movie o) {
  36.         if (getRatingCoef(MoviesList.maxRatings) == o.getRatingCoef(MoviesList.maxRatings))
  37.             return title.compareTo(o.getTitle());
  38.         return -Double.compare(getRatingCoef(MoviesList.maxRatings), o.getRatingCoef(MoviesList.maxRatings));
  39.     }
  40. }
  41.  
  42. class MoviesList{
  43.     List<Movie> movies;
  44.     static int maxRatings;
  45.  
  46.     public MoviesList() {
  47.         this.movies = new ArrayList<>();
  48.     }
  49.  
  50.     public void addMovie(String title, int [] ratings){
  51.         movies.add(new Movie(title,ratings));
  52.     }
  53.  
  54.     public List<Movie> top10ByAvgRating(){
  55.         movies.sort(Comparator.comparing(Movie::getAverageRating).reversed().thenComparing(Movie::getTitle));
  56.         return movies.stream().limit(10).collect(Collectors.toList());
  57.     }
  58.  
  59.     public List<Movie> top10ByRatingCoef(){
  60.         maxRatings = movies.stream().mapToInt(Movie::getRatingsNum).max().getAsInt();
  61.         movies.sort(Movie::compareTo);
  62.         return movies.stream().limit(10).collect(Collectors.toList());
  63.     }
  64.  
  65.  
  66. }
  67. public class MoviesTest {
  68.     public static void main(String[] args) {
  69.         Scanner scanner = new Scanner(System.in);
  70.         MoviesList moviesList = new MoviesList();
  71.         int n = scanner.nextInt();
  72.         scanner.nextLine();
  73.         for (int i = 0; i < n; ++i) {
  74.             String title = scanner.nextLine();
  75.             int x = scanner.nextInt();
  76.             int[] ratings = new int[x];
  77.             for (int j = 0; j < x; ++j) {
  78.                 ratings[j] = scanner.nextInt();
  79.             }
  80.             scanner.nextLine();
  81.             moviesList.addMovie(title, ratings);
  82.         }
  83.         scanner.close();
  84.         List<Movie> movies = moviesList.top10ByAvgRating();
  85.         System.out.println("=== TOP 10 BY AVERAGE RATING ===");
  86.         for (Movie movie : movies) {
  87.             System.out.println(movie);
  88.         }
  89.         movies = moviesList.top10ByRatingCoef();
  90.         System.out.println("=== TOP 10 BY RATING COEFFICIENT ===");
  91.         for (Movie movie : movies) {
  92.             System.out.println(movie);
  93.         }
  94.     }
  95. }
  96.  
  97. // vashiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement