iLLcheff

DataProcessor

Dec 16th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. package com.telerikacademy;
  2.  
  3. import com.telerikacademy.models.Customer;
  4. import com.telerikacademy.models.Genre;
  5. import com.telerikacademy.models.Movie;
  6. import java.util.Comparator;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9.  
  10. @SuppressWarnings("ForLoopReplaceableByForEach")
  11. public class DataProcessor {
  12.  
  13.     public static long countCustomersAboveTargetAge(List<Customer> customers, int targetAge) {
  14.        return customers
  15.                 .stream()
  16.                 .filter(c -> c.getAge() > targetAge)
  17.                 .count();
  18.  
  19.     }
  20.  
  21.     public static boolean findIfAllCustomersAreAboveTargetAge(List<Customer> customers, int targetAge) {
  22.          return customers
  23.                  .stream()
  24.                  .allMatch(c -> c.getAge() > targetAge);
  25.     }
  26.  
  27.     public static boolean findIfAnyCustomersHasTargetName(List<Customer> customers, String targetName) {
  28.         return customers
  29.                 .stream()
  30.                 .anyMatch(c -> c.getName().equals(targetName));
  31.     }
  32.  
  33.     public static boolean findIfAllCustomersDislikeMovie(List<Customer> customers, Movie targetMovie) {
  34.         boolean result = customers
  35.                 .stream()
  36.                 .allMatch(c -> c.getDislikedMovies().contains(targetMovie));
  37.         return result;
  38.     }
  39.  
  40.     public static long findHowManyPeopleLikeMove(List<Customer> customers, Movie targetMovie) {
  41.  
  42.         return customers
  43.                 .stream()
  44.                 .filter(c -> c.getLikedMovies().contains(targetMovie))
  45.                 .count();
  46.     }
  47.  
  48.     public static double findTheAverageAgeOfPeopleWhoDislikeMovies(List<Customer> customers, Movie targetMovie) {
  49.         List<Customer> customersWhoDislikeMovie = customers
  50.                 .stream()
  51.                 .filter(c -> c.getDislikedMovies().contains(targetMovie))
  52.                 .collect(Collectors.toList());
  53.         return customersWhoDislikeMovie
  54.                 .stream()
  55.                 .collect(Collectors.averagingDouble(Customer::getAge));
  56.     }
  57.  
  58.     public static double findAverageAgeOfAllCustomers(List<Customer> customers) {
  59.         return customers
  60.                 .stream()
  61.                 .collect(Collectors.averagingDouble(Customer::getAge));
  62.     }
  63.  
  64.     public static List<Customer> findAllCustomersAboveTargetAgeThatLikeGenre(List<Customer> customers, int targetAge, Genre targetGenre) {
  65.         List<Customer> result =
  66.                 customers
  67.                         .stream()
  68.                         .filter(x -> x.getAge() > targetAge)
  69.                         .filter(x -> x.getLikedMovies().stream().anyMatch(g -> g.getGenre().equals(targetGenre)))
  70.                         .collect(Collectors.toList());
  71.         return result;
  72.     }
  73.  
  74.     public static List<Customer> findAllCustomersUnderTargetAge(List<Customer> customers, int targetAge) {
  75.         List<Customer> result =
  76.                 customers
  77.                         .stream()
  78.                         .filter(x -> x.getAge() < targetAge)
  79.                         .collect(Collectors.toList());
  80.         return result;
  81.     }
  82.  
  83.     public static Customer findTheCustomerWithTheLongestName(List<Customer> customers) {
  84. //        return customers.stream().max(Comparator.comparing(v -> v.getName().length())).get();
  85.         return customers.stream()
  86.                 .reduce(customers.get(0), (s1, s2) -> s1.getName().length() > s2.getName().length() ? s1 : s2);
  87.     }
  88.  
  89.     public static List<Customer> findAllCustomersWhoLikeOnlyMoviesWithGenre(List<Customer> customers, Genre targetGenre) {
  90.         return customers
  91.                 .stream()
  92.                 .filter(x -> x.getLikedMovies().stream().allMatch(g -> g.getGenre().equals(targetGenre)))
  93.                 .collect(Collectors.toList());
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment