Advertisement
dzocesrce

[NP] Movie Directors

Apr 30th, 2025
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. public class MoviesCollection {
  2.     List<Movie> movies;
  3.  
  4.     public MoviesCollection() {
  5.  
  6.         this.movies = new ArrayList<Movie>();
  7.     }
  8.  
  9.     public void printByGenre(String genre) {
  10.         movies.stream().filter(i->i.getGenre().equals(genre))
  11.                 .sorted(Comparator.comparing(Movie::getTitle).thenComparing(Movie::getRating))
  12.                 .forEach(System.out::println);
  13.     }
  14.  
  15.     public List<Movie> getTopRatedN(int printN) {
  16.         return movies.stream().sorted(Comparator.comparing(Movie::getRating).reversed())
  17.                 .limit(Math.min(printN,movies.size())).collect(Collectors.toList());
  18.     }
  19.  
  20.     public Map<String,Long> getCountByDirector(int n) {
  21.         return movies.stream().collect(Collectors.groupingBy(
  22.                 i->i.getDirector(),
  23.                 TreeMap::new,
  24.                 Collectors.counting()
  25.         ));
  26.     }
  27.  
  28.     public void addMovie(Movie movie) {
  29.         movies.add(movie);
  30.     }
  31. }
  32.  
  33. public class Movie {
  34.     String title;
  35.     String director;
  36.     String genre;
  37.     float rating;
  38.  
  39.     public Movie(String title, String director, String genre, float rating) {
  40.         this.title = title;
  41.         this.director = director;
  42.         this.genre = genre;
  43.         this.rating = rating;
  44.     }
  45.  
  46.     public String getTitle() {
  47.         return title;
  48.     }
  49.  
  50.     public String getDirector() {
  51.         return director;
  52.     }
  53.  
  54.     public String getGenre() {
  55.         return genre;
  56.     }
  57.  
  58.     public float getRating() {
  59.         return rating;
  60.     }
  61.  
  62.     @Override
  63.     public String toString() {
  64.         return String.format("%s (%s, %s) %.2f",
  65.                 getTitle(),
  66.                 getDirector(),
  67.                 getGenre(),
  68.                 getRating());
  69.     }
  70. }
  71.  
  72. public class MoviesTest {
  73.     public static void main(String[] args) {
  74.         Scanner scanner = new Scanner(System.in);
  75.         int n = scanner.nextInt();
  76.         int printN = scanner.nextInt();
  77.         scanner.nextLine();
  78.         MoviesCollection moviesCollection = new MoviesCollection();
  79.         Set<String> genres = fillCollection(scanner, moviesCollection);
  80.         System.out.println("=== PRINT BY GENRE ===");
  81.         for (String genre : genres) {
  82.             System.out.println("GENRE: " + genre);
  83.             moviesCollection.printByGenre(genre);
  84.         }
  85.         System.out.println("=== TOP N BY RATING ===");
  86.         printList(moviesCollection.getTopRatedN(printN));
  87.  
  88.         System.out.println("=== COUNT BY DIRECTOR ===");
  89.         printMap(moviesCollection.getCountByDirector(n));
  90.  
  91.     }
  92.  
  93.     static void printMap(Map<String,Long> countByDirector) {
  94.         countByDirector.entrySet().stream()
  95.                 .forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
  96.     }
  97.  
  98.     static void printList(List<Movie> movies) {
  99.         for (Movie movie : movies) {
  100.             System.out.println(movie);
  101.         }
  102.     }
  103.  
  104.     static TreeSet<String> fillCollection(Scanner scanner,
  105.                                           MoviesCollection collection) {
  106.         TreeSet<String> categories = new TreeSet<String>();
  107.         while (scanner.hasNext()) {
  108.             String line = scanner.nextLine();
  109.             String[] parts = line.split(":");
  110.             Movie movie = new Movie(parts[0], parts[1], parts[2], Float.parseFloat(parts[3]));
  111.             collection.addMovie(movie);
  112.             categories.add(parts[2]);
  113.         }
  114.         return categories;
  115.     }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement