11package com .baeldung .axon .coreapi .queries ;
22
3+ import java .util .HashMap ;
4+ import java .util .Map ;
35import java .util .Objects ;
46
57public class OrderedProduct {
68
79 private final String orderId ;
8- private final String product ;
10+ private final Map < String , Integer > products ;
911 private OrderStatus orderStatus ;
1012
11- public OrderedProduct (String orderId , String product ) {
13+ public OrderedProduct (String orderId ) {
1214 this .orderId = orderId ;
13- this .product = product ;
15+ this .products = new HashMap <>() ;
1416 orderStatus = OrderStatus .PLACED ;
1517 }
1618
1719 public String getOrderId () {
1820 return orderId ;
1921 }
2022
21- public String getProduct () {
22- return product ;
23+ public Map < String , Integer > getProducts () {
24+ return products ;
2325 }
2426
2527 public OrderStatus getOrderStatus () {
2628 return orderStatus ;
2729 }
2830
31+ public void addProduct (String productId ) {
32+ products .putIfAbsent (productId , 1 );
33+ }
34+
35+ public void incrementProductInstance (String productId ) {
36+ products .computeIfPresent (productId , (id , count ) -> ++count );
37+ }
38+
39+ public void decrementProductInstance (String productId ) {
40+ products .computeIfPresent (productId , (id , count ) -> --count );
41+ }
42+
43+
44+ public void removeProduct (String productId ) {
45+ products .remove (productId );
46+ }
47+
2948 public void setOrderConfirmed () {
3049 this .orderStatus = OrderStatus .CONFIRMED ;
3150 }
@@ -35,29 +54,28 @@ public void setOrderShipped() {
3554 }
3655
3756 @ Override
38- public int hashCode () {
39- return Objects .hash (orderId , product , orderStatus );
40- }
41-
42- @ Override
43- public boolean equals (Object obj ) {
44- if (this == obj ) {
57+ public boolean equals (Object o ) {
58+ if (this == o ) {
4559 return true ;
4660 }
47- if (obj == null || getClass () != obj .getClass ()) {
61+ if (o == null || getClass () != o .getClass ()) {
4862 return false ;
4963 }
50- final OrderedProduct other = (OrderedProduct ) obj ;
51- return Objects .equals (this .orderId , other .orderId )
52- && Objects .equals (this .product , other .product )
53- && Objects .equals (this .orderStatus , other .orderStatus );
64+ OrderedProduct that = (OrderedProduct ) o ;
65+ return Objects .equals (orderId , that .orderId ) && Objects .equals (products , that .products )
66+ && orderStatus == that .orderStatus ;
67+ }
68+
69+ @ Override
70+ public int hashCode () {
71+ return Objects .hash (orderId , products , orderStatus );
5472 }
5573
5674 @ Override
5775 public String toString () {
5876 return "OrderedProduct{" +
5977 "orderId='" + orderId + '\'' +
60- ", product=' " + product + '\'' +
78+ ", products= " + products +
6179 ", orderStatus=" + orderStatus +
6280 '}' ;
6381 }
0 commit comments