Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 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. import org.javamoney.moneta.Money;
  6.  
  7. import javax.money.MonetaryAmount;
  8. import java.util.Collections;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.stream.Collectors;
  12.  
  13. import static java.util.Objects.isNull;
  14. import static java.util.Objects.nonNull;
  15.  
  16. /**
  17. * The type Discount.
  18. */
  19. abstract class Discount implements ChainDiscount {
  20.  
  21. private final ShoppingCart shoppingCart;
  22. private final Discount successor;
  23. /**
  24. * The constant ZERO_EURO.
  25. */
  26. protected static final MonetaryAmount ZERO_EURO = Money.of(0, "EUR");
  27. /**
  28. * The constant CURRENCY_CODE.
  29. */
  30. protected static final String CURRENCY_CODE = "EUR";
  31.  
  32. /**
  33. * Gets grouping items.
  34. *
  35. * @return the grouping items
  36. */
  37. protected Map<String, List<Item>> getGroupingItems() {
  38. if (isNull(shoppingCart)) {
  39. return Collections.emptyMap();
  40. }
  41. return shoppingCart.getItems().stream().collect(Collectors.groupingBy(Item::getName));
  42. }
  43.  
  44. /**
  45. * Instantiates a new Discount.
  46. *
  47. * @param successor the successor
  48. * @param shoppingCart the shopping cart
  49. */
  50. public Discount(Discount successor, ShoppingCart shoppingCart) {
  51. this.successor = successor;
  52. this.shoppingCart = shoppingCart;
  53. }
  54.  
  55. /**
  56. * Next monetary amount.
  57. *
  58. * @return the monetary amount
  59. */
  60. MonetaryAmount next() {
  61. if (nonNull(successor)) {
  62. return successor.handleRequest();
  63. }
  64. return ZERO_EURO;
  65. }
  66.  
  67. /**
  68. * Gets shopping cart.
  69. *
  70. * @return the shopping cart
  71. */
  72. public ShoppingCart getShoppingCart() {
  73. return shoppingCart;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement