Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MoviesCollection {
- List<Movie> movies;
- public MoviesCollection() {
- this.movies = new ArrayList<Movie>();
- }
- public void printByGenre(String genre) {
- movies.stream().filter(i->i.getGenre().equals(genre))
- .sorted(Comparator.comparing(Movie::getTitle).thenComparing(Movie::getRating))
- .forEach(System.out::println);
- }
- public List<Movie> getTopRatedN(int printN) {
- return movies.stream().sorted(Comparator.comparing(Movie::getRating).reversed())
- .limit(Math.min(printN,movies.size())).collect(Collectors.toList());
- }
- public Map<String,Long> getCountByDirector(int n) {
- return movies.stream().collect(Collectors.groupingBy(
- i->i.getDirector(),
- TreeMap::new,
- Collectors.counting()
- ));
- }
- public void addMovie(Movie movie) {
- movies.add(movie);
- }
- }
- public class Movie {
- String title;
- String director;
- String genre;
- float rating;
- public Movie(String title, String director, String genre, float rating) {
- this.title = title;
- this.director = director;
- this.genre = genre;
- this.rating = rating;
- }
- public String getTitle() {
- return title;
- }
- public String getDirector() {
- return director;
- }
- public String getGenre() {
- return genre;
- }
- public float getRating() {
- return rating;
- }
- @Override
- public String toString() {
- return String.format("%s (%s, %s) %.2f",
- getTitle(),
- getDirector(),
- getGenre(),
- getRating());
- }
- }
- public class MoviesTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- int printN = scanner.nextInt();
- scanner.nextLine();
- MoviesCollection moviesCollection = new MoviesCollection();
- Set<String> genres = fillCollection(scanner, moviesCollection);
- System.out.println("=== PRINT BY GENRE ===");
- for (String genre : genres) {
- System.out.println("GENRE: " + genre);
- moviesCollection.printByGenre(genre);
- }
- System.out.println("=== TOP N BY RATING ===");
- printList(moviesCollection.getTopRatedN(printN));
- System.out.println("=== COUNT BY DIRECTOR ===");
- printMap(moviesCollection.getCountByDirector(n));
- }
- static void printMap(Map<String,Long> countByDirector) {
- countByDirector.entrySet().stream()
- .forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
- }
- static void printList(List<Movie> movies) {
- for (Movie movie : movies) {
- System.out.println(movie);
- }
- }
- static TreeSet<String> fillCollection(Scanner scanner,
- MoviesCollection collection) {
- TreeSet<String> categories = new TreeSet<String>();
- while (scanner.hasNext()) {
- String line = scanner.nextLine();
- String[] parts = line.split(":");
- Movie movie = new Movie(parts[0], parts[1], parts[2], Float.parseFloat(parts[3]));
- collection.addMovie(movie);
- categories.add(parts[2]);
- }
- return categories;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement