Niksi

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

Jun 17th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 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. // vashiot kod ovde
  35.  
  36. class Movie{
  37.     String name;
  38.     List<Integer> list;
  39.     static int max;
  40.     public Movie(String name, int [] rtg) {
  41.         this.name = name;
  42.         this.list = new ArrayList<>();
  43.         for(Integer pom: rtg) {
  44.             list.add(pom);
  45.         }
  46.        
  47.     }
  48.  
  49.     public String getName() {
  50.         return name;
  51.     }
  52.  
  53.     public List<Integer> getList() {
  54.         return list;
  55.     }
  56.    
  57.     public double averageRt() {
  58.         double sum=0;
  59.         sum=list.stream().mapToInt(o -> o).sum();
  60.         return sum/list.size();
  61.     }
  62.    
  63.     public double averageCoefRt() {
  64.         //double sum=0;
  65.         //sum=list.stream().mapToInt(o -> o).sum();
  66.         return averageRt() * list.size() / max;
  67.     }
  68.    
  69.    
  70.    
  71.    
  72.     public static void setMax(int max) {
  73.         Movie.max = max;
  74.     }
  75.  
  76.     @Override
  77.     public String toString() {
  78.         return String.format("%s (%.2f) of %d ratings", name,(float)averageRt(),list.size());
  79.     }
  80.    
  81.    
  82.    
  83.    
  84.    
  85. }
  86.  
  87. class MoviesList{
  88.     List<Movie> filmovi;
  89.     int max=0;
  90.     public MoviesList() {
  91.         filmovi=new ArrayList<>();
  92.     }
  93.    
  94.     public void addMovie(String title, int[] ratings) {
  95.         filmovi.add(new Movie(title,ratings));
  96.         if(max<ratings.length) {
  97.             max=ratings.length;
  98.         }
  99.     }
  100.    
  101.     public List<Movie> top10ByAvgRating(){
  102.         return filmovi.stream()
  103.         .sorted(Comparator.comparing(Movie::averageRt).reversed().thenComparing(Movie::getName))
  104.         .collect(Collectors.toList()).subList(0, 10);
  105.     }
  106.    
  107.     public List<Movie> top10ByRatingCoef(){
  108.         filmovi.get(0).setMax(max);
  109.         return filmovi.stream().sorted(Comparator
  110.                 .comparing(Movie::averageCoefRt).reversed().thenComparing(Movie::getName))
  111.         .collect(Collectors.toList()).subList(0, 10);
  112.     }
  113.    
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment