Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
1,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 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)
  40.                     .reversed()
  41.                     .thenComparing(entry -> entry.getKey().getName()))
  42.             .map(Map.Entry::getKey)
  43.             .limit(3)
  44.             .collect(Collectors.toList());
  45.   }
  46.  
  47.   /**
  48.    * Should return the most valuable customer, that is the customer that has the highest value of all placed orders.
  49.    * If two customers have the same orders value, then any of them should be returned.
  50.    *
  51.    * @param orders orders stream
  52.    * @return Optional of most valuable customer
  53.    */
  54.   public Optional<Customer> findMostValuableCustomer(Stream<Order> orders) {
  55.     Map<Customer, BigDecimal> customersToOrderValues = orders
  56.             .collect(
  57.                     Collectors.groupingBy(
  58.                             Order::getCustomer,
  59.                             Collectors.reducing(
  60.                                     BigDecimal.ZERO,
  61.                                     this::countOrderValue,
  62.                                     BigDecimal::add
  63.                             )
  64.                     )
  65.             );
  66.  
  67.     return customersToOrderValues.entrySet().stream()
  68.             .max(Comparator.comparing(Map.Entry::getValue))
  69.             .map(Map.Entry::getKey);
  70.   }
  71.  
  72.   /**
  73.    * Should return the least busy month in current year to know when Petrovich can go on vacation.
  74.    * The least busy month is the month in which the least number of possible repair works.
  75.    *
  76.    * @param orders orders stream
  77.    * @return Optional of the least busy month in current year
  78.    */
  79.   public Optional<Month> findLeastBusyMonthInCurrentYear(Stream<Order> orders) {
  80.     return null;
  81.   }
  82.  
  83.   private BigDecimal countOrderValue(Order order) {
  84.     return order.getOrderLines().stream()
  85.             .map(line -> line.getProduct().getPrice().multiply(BigDecimal.valueOf(line.getQuantity())))
  86.             .reduce(BigDecimal.ZERO, BigDecimal::add);
  87.   }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement