Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. package com.es.orders;
  2.  
  3. import com.es.orders.model.Customer;
  4. import com.es.orders.model.Order;
  5. import com.es.orders.model.OrderLine;
  6. import com.es.orders.model.Product;
  7.  
  8. import java.math.BigDecimal;
  9. import java.time.Month;
  10. import java.util.Comparator;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Optional;
  14. import java.util.stream.Collectors;
  15. import java.util.stream.Stream;
  16.  
  17. public class OrdersAnalyzer {
  18.  
  19.   /**
  20.    * Should return at most three most popular products. Most popular product is the product that have the most occurrences
  21.    * in given orders (ignoring product quantity).
  22.    * If two products have the same popularity, then products should be ordered by name
  23.    *
  24.    * @param orders orders stream
  25.    * @return list with up to three most popular products
  26.    */
  27.   public List<Product> findThreeMostPopularProducts(Stream<Order> orders) {
  28.     Map<Product, Long> productsToOccurences = orders
  29.             .flatMap(order -> order.getOrderLines().stream())
  30.             .collect(
  31.                     Collectors.groupingBy(
  32.                             OrderLine::getProduct,
  33.                             Collectors.counting()
  34.                     )
  35.             );
  36.  
  37.     return productsToOccurences.entrySet().stream()
  38.             .sorted(Comparator
  39.                     .comparing(Map.Entry<Product, Long>::getValue).reversed()
  40.                     .thenComparing(entry -> entry.getKey().getName()))
  41.             .map(Map.Entry::getKey)
  42.             .limit(3)
  43.             .collect(Collectors.toList());
  44.   }
  45.  
  46.   /**
  47.    * Should return the most valuable customer, that is the customer that has the highest value of all placed orders.
  48.    * If two customers have the same orders value, then any of them should be returned.
  49.    *
  50.    * @param orders orders stream
  51.    * @return Optional of most valuable customer
  52.    */
  53.   public Optional<Customer> findMostValuableCustomer(Stream<Order> orders) {
  54.     Map<Customer, BigDecimal> customersToOrderValues = orders
  55.             .collect(
  56.                     Collectors.groupingBy(
  57.                             Order::getCustomer,
  58.                             Collectors.reducing(
  59.                                     BigDecimal.ZERO,
  60.                                     this::countOrderValue,
  61.                                     BigDecimal::add
  62.                             )
  63.                     )
  64.             );
  65.  
  66.     return customersToOrderValues.entrySet().stream()
  67.             .max(Comparator.comparing(Map.Entry::getValue))
  68.             .map(Map.Entry::getKey);
  69.   }
  70.  
  71.   /**
  72.    * Should return the least busy month in current year to know when Petrovich can go on vacation.
  73.    * The least busy month is the month in which the least number of possible repair works.
  74.    *
  75.    * @param orders orders stream
  76.    * @return Optional of the least busy month in current year
  77.    */
  78.   public Optional<Month> findLeastBusyMonthInCurrentYear(Stream<Order> orders) {
  79.     return null;
  80.   }
  81.  
  82.   private BigDecimal countOrderValue(Order order) {
  83.     return order.getOrderLines().stream()
  84.             .map(line -> line.getProduct().getPrice().multiply(BigDecimal.valueOf(line.getQuantity())))
  85.             .reduce(BigDecimal.ZERO, BigDecimal::add);
  86.   }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement