Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. package com.gof.behavioral.chainofresponsibility.handler;
  2.  
  3. import com.gof.behavioral.chainofresponsibility.request.Item;
  4. import com.gof.behavioral.chainofresponsibility.request.ShoppingCart;
  5.  
  6. import javax.money.MonetaryAmount;
  7.  
  8. /**
  9. * The type Buy three pay two.
  10. */
  11. public class BuyThreePayTwo extends Discount {
  12.  
  13. private final int amountToDiscount = 3;
  14.  
  15. /**
  16. * Instantiates a new Buy three pay two.
  17. *
  18. * @param successor the successor
  19. * @param shoppingCart the shopping cart
  20. */
  21. public BuyThreePayTwo(Discount successor, ShoppingCart shoppingCart) {
  22. super(successor, shoppingCart);
  23. }
  24.  
  25. @Override
  26. public MonetaryAmount handleRequest() {
  27. final MonetaryAmount monetaryAmount = getGroupingItems().entrySet().stream()
  28. .filter(entryItems -> entryItems.getValue().size() >= 3)
  29. .map(entryItems -> {
  30. Item item = entryItems.getValue().stream().findFirst().orElse(null);
  31. return item.getAmount().multiply(entryItems.getValue().size() / amountToDiscount);
  32. })
  33. .findFirst().orElse(ZERO_EURO);
  34. return monetaryAmount.add(next());
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement