Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class MoviesTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- MoviesList moviesList = new MoviesList();
- int n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String title = scanner.nextLine();
- int x = scanner.nextInt();
- int[] ratings = new int[x];
- for (int j = 0; j < x; ++j) {
- ratings[j] = scanner.nextInt();
- }
- scanner.nextLine();
- moviesList.addMovie(title, ratings);
- }
- scanner.close();
- List<Movie> movies = moviesList.top10ByAvgRating();
- System.out.println("=== TOP 10 BY AVERAGE RATING ===");
- for (Movie movie : movies) {
- System.out.println(movie);
- }
- movies = moviesList.top10ByRatingCoef();
- System.out.println("=== TOP 10 BY RATING COEFFICIENT ===");
- for (Movie movie : movies) {
- System.out.println(movie);
- }
- }
- }
- class MoviesList {
- List<Movie> movies;
- MoviesList() {
- movies = new ArrayList<>();
- }
- public void addMovie(String title, int[] ratings) {
- Movie movie = new Movie(title, ratings);
- movies.add(movie);
- }
- public List<Movie> top10ByAvgRating() {
- return movies.stream()
- .sorted(
- Comparator.comparing(Movie::getAvgRating)
- .reversed()
- .thenComparing(Movie::getTitle)
- )
- .limit(10)
- .collect(Collectors.toList());
- }
- public List<Movie> top10ByRatingCoef() {
- return movies.stream()
- .sorted(
- /*new Comparator<Movie>() {
- @Override
- public int compare(Movie one, Movie two) {
- double oneCoef = one.getAvgRating() * one.getTotalNumOfRatings() / movies.size();
- double twoCoef = two.getAvgRating() * two.getTotalNumOfRatings() / movies.size();
- return Double.compare(oneCoef, twoCoef);
- }
- }*/
- Comparator.comparing(Movie::getCoef)
- .reversed()
- .thenComparing(Movie::getTitle)
- )
- .limit(10)
- .collect(Collectors.toList());
- }
- }
- class Movie {
- String title;
- List<Integer> ratings;
- Movie(String title, int[] ratings) {
- this.title = title;
- this.ratings = Arrays.stream(ratings)
- .boxed()
- .collect(Collectors.toList());
- }
- public String getTitle() {
- return title;
- }
- int getMaxRating() {
- return ratings.stream()
- .max(Integer::compare)
- .get();
- }
- int getTotalNumOfRatings() {
- return ratings.size();
- }
- double getAvgRating() {
- return ratings.stream()
- .mapToDouble(Integer::doubleValue)
- .average()
- .orElse(0);
- }
- @Override
- public String toString() {
- return String.format("%s (%.2f) of %d ratings", title, getAvgRating(), ratings.size());
- }
- public double getCoef() {
- return this.getAvgRating() * ratings.size();
- }
- }
Add Comment
Please, Sign In to add comment