-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7114a5b
commit 9414514
Showing
6 changed files
with
140 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 30 additions & 54 deletions
84
src/carts/lib/src/main/java/mushop/carts/entities/Cart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,58 @@ | ||
package mushop.carts.entities; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import jakarta.validation.constraints.NotNull; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import io.micronaut.core.annotation.Creator; | ||
import io.micronaut.data.annotation.Id; | ||
import io.micronaut.data.annotation.MappedEntity; | ||
|
||
@MappedEntity | ||
public class Cart { | ||
public record Cart(@Id String id, String customerId, Map<String, Item> items) { | ||
|
||
@Id | ||
private String id; | ||
|
||
private String customerId; | ||
|
||
private List<Item> items = new ArrayList<>(); | ||
|
||
public Cart() { | ||
id = UUID.randomUUID().toString(); | ||
} | ||
|
||
public Cart(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getCustomerId() { | ||
return customerId; | ||
} | ||
|
||
public List<Item> getItems() { | ||
return items; | ||
public static Cart of(String customerId, List<Item> items) { | ||
return new Cart(UUID.randomUUID().toString(), customerId, items); | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
public Cart(@Id String id, String customerId, List<Item> itemList) { | ||
this(id, customerId, itemList.stream().collect(Collectors.toMap(Item::itemId, Function.identity()))); | ||
} | ||
|
||
public void setCustomerId(String customerId) { | ||
this.customerId = customerId; | ||
public boolean removeItem(String itemId) { | ||
Item removed = items.remove(itemId); | ||
return removed != null; | ||
} | ||
|
||
public void setItems(List<Item> items) { | ||
this.items = items; | ||
public void updateItem(Item item) { | ||
items.put(item.itemId(), item); | ||
} | ||
|
||
public boolean removeItem(String itemId) { | ||
return items.removeIf(item -> itemId.equalsIgnoreCase(item.getItemId())); | ||
public Cart merge(Cart cart) { | ||
return new Cart(id, cart.customerId(), mergeItems(cart.items)); | ||
} | ||
|
||
public void merge(Cart cart) { | ||
customerId = cart.getCustomerId(); | ||
for (Item item : cart.items) { | ||
mergeItem(item); | ||
private Map<String, Item> mergeItems(Map<String, Item> newItems) { | ||
Map<String, Item> result = new HashMap<>(items); | ||
for (Item newItem : newItems.values()) { | ||
result.computeIfPresent(newItem.itemId(), (itemId, existing) -> new Item(existing.id(), itemId, existing.quantity() + newItem.quantity(), existing.unitPrice())); | ||
result.putIfAbsent(newItem.itemId(), newItem); | ||
} | ||
return result; | ||
} | ||
|
||
private void mergeItem(Item item) { | ||
for (Item existing : items) { | ||
if (existing.getItemId().equals(item.getItemId())) { | ||
existing.setQuantity(existing.getQuantity() + item.getQuantity()); | ||
return; | ||
} | ||
} | ||
items.add(item); | ||
public Cart withId(String cartId) { | ||
return new Cart(cartId, customerId, items); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Cart [customerId=" + customerId + | ||
", id=" + id + | ||
", items=" + items + | ||
"]"; | ||
public List<Item> getItems() { | ||
return Optional.ofNullable(items) | ||
.map(Map::values) | ||
.map(ArrayList::new) | ||
.orElseGet(ArrayList::new); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/carts/lib/src/test/java/mushop/carts/entities/CartTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package mushop.carts.entities; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class CartTest { | ||
|
||
@Test | ||
void testRemoveItem() { | ||
String itemId = "itemId"; | ||
|
||
Item item = Item.of(itemId, 0, new BigDecimal(0)); | ||
Cart cart = new Cart(null, null, List.of(item)); | ||
|
||
assertEquals(1, cart.items().size()); | ||
assertTrue(cart.removeItem(itemId)); | ||
assertEquals(0, cart.items().size()); | ||
assertFalse(cart.removeItem(itemId)); | ||
} | ||
|
||
@Test | ||
void testMergeCartsSameItems() { | ||
String itemId = "itemId"; | ||
|
||
Item cart1Item = Item.of(itemId, 1, new BigDecimal(10)); | ||
Cart cart1 = new Cart(null, null, List.of(cart1Item)); | ||
|
||
Item cart2Item = Item.of(itemId, 3, new BigDecimal(20)); | ||
Cart cart2 = new Cart(null, null, List.of(cart2Item)); | ||
|
||
Cart merged = cart1.merge(cart2); | ||
assertEquals(1, merged.items().size()); | ||
|
||
Item first = merged.getItems().getFirst(); | ||
assertEquals(4, first.quantity()); | ||
assertEquals(10, first.unitPrice().intValue()); | ||
} | ||
|
||
@Test | ||
void testMergeCartsMoreItems() { | ||
Item cart1item1 = Item.of("1", 1, new BigDecimal(1)); | ||
Item cart2item1 = Item.of("1", 2, new BigDecimal(1)); | ||
|
||
Item item2 = Item.of("2", 2, new BigDecimal(2)); | ||
|
||
Cart cart1 = new Cart(null, null, List.of(cart1item1)); | ||
Cart cart2 = new Cart(null, null, List.of(cart2item1, item2)); | ||
|
||
Cart merged = cart1.merge(cart2); | ||
assertEquals(2, merged.items().size()); | ||
|
||
Item mergedItem1 = merged.getItems().stream().filter(item -> item.itemId().equals("1")).findFirst().get(); | ||
assertEquals(3, mergedItem1.quantity()); | ||
|
||
Item mergedItem2 = merged.getItems().stream().filter(item -> item.itemId().equals("2")).findFirst().get(); | ||
assertEquals(2, mergedItem2.quantity()); | ||
} | ||
|
||
@Test | ||
void testMergeCartsLessItems() { | ||
Item cart1item1 = Item.of("1", 1, new BigDecimal(1)); | ||
Item cart2item1 = Item.of("1", 2, new BigDecimal(1)); | ||
|
||
Item item2 = Item.of("2", 2, new BigDecimal(2)); | ||
|
||
Cart cart1 = new Cart(null, null, List.of(cart1item1, item2)); | ||
Cart cart2 = new Cart(null, null, List.of(cart2item1)); | ||
|
||
Cart merged = cart1.merge(cart2); | ||
assertEquals(2, merged.items().size()); | ||
|
||
Item mergedItem1 = merged.getItems().stream().filter(item -> item.itemId().equals("1")).findFirst().get(); | ||
assertEquals(3, mergedItem1.quantity()); | ||
|
||
Item mergedItem2 = merged.getItems().stream().filter(item -> item.itemId().equals("2")).findFirst().get(); | ||
assertEquals(2, mergedItem2.quantity()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters