From 9d3d41b01daf5f2ba54b4948ce6625214a92d3c8 Mon Sep 17 00:00:00 2001 From: Andrzej Ludwikowski Date: Sat, 23 Nov 2024 11:00:16 +0100 Subject: [PATCH] chore: remove static methods from the quickstart domain model (#25) --- .../java/shoppingcart/domain/ShoppingCart.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/samples/shopping-cart-quickstart/src/main/java/shoppingcart/domain/ShoppingCart.java b/samples/shopping-cart-quickstart/src/main/java/shoppingcart/domain/ShoppingCart.java index 57c4984d8..42f79227e 100644 --- a/samples/shopping-cart-quickstart/src/main/java/shoppingcart/domain/ShoppingCart.java +++ b/samples/shopping-cart-quickstart/src/main/java/shoppingcart/domain/ShoppingCart.java @@ -27,21 +27,21 @@ public LineItem withQuantity(int quantity) { // tag::itemAdded[] public ShoppingCart onItemAdded(ShoppingCartEvent.ItemAdded itemAdded) { var item = itemAdded.item(); - var lineItem = updateItem(item, this); // <1> - List lineItems = removeItemByProductId(this, item.productId()); // <2> + var lineItem = updateItem(item); // <1> + List lineItems = removeItemByProductId(item.productId()); // <2> lineItems.add(lineItem); // <3> lineItems.sort(Comparator.comparing(LineItem::productId)); return new ShoppingCart(cartId, lineItems, checkedOut); // <4> } - private static LineItem updateItem(LineItem item, ShoppingCart cart) { - return cart.findItemByProductId(item.productId()) + private LineItem updateItem(LineItem item) { + return findItemByProductId(item.productId()) .map(li -> li.withQuantity(li.quantity() + item.quantity())) .orElse(item); } - private static List removeItemByProductId(ShoppingCart cart, String productId) { - return cart.items().stream() + private List removeItemByProductId(String productId) { + return items().stream() .filter(lineItem -> !lineItem.productId().equals(productId)) .collect(Collectors.toList()); } @@ -56,7 +56,7 @@ public Optional findItemByProductId(String productId) { public ShoppingCart onItemRemoved(ShoppingCartEvent.ItemRemoved itemRemoved) { List updatedItems = - removeItemByProductId(this, itemRemoved.productId()); + removeItemByProductId(itemRemoved.productId()); updatedItems.sort(Comparator.comparing(LineItem::productId)); return new ShoppingCart(cartId, updatedItems, checkedOut); }