1 /*
2 * Copyright 2010-2026 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.mybatis.jpetstore.domain;
17
18 import java.io.Serializable;
19 import java.math.BigDecimal;
20 import java.util.Optional;
21
22 /**
23 * The Class CartItem.
24 *
25 * @author Eduardo Macarron
26 */
27 public class CartItem implements Serializable {
28
29 /** The serial version uid. */
30 private static final long serialVersionUID = 6620528781626504362L;
31
32 /** The item. */
33 private Item item;
34 /** The quantity. */
35 private int quantity;
36 /** The in stock. */
37 private boolean inStock;
38 /** The total. */
39 private BigDecimal total;
40
41 /**
42 * Checks if is in stock.
43 *
44 * @return true, if successful
45 */
46 public boolean isInStock() {
47 return inStock;
48 }
49
50 /**
51 * Sets the in stock.
52 *
53 * @param inStock
54 * the in stock
55 */
56 public void setInStock(boolean inStock) {
57 this.inStock = inStock;
58 }
59
60 /**
61 * Gets the total.
62 *
63 * @return the total
64 */
65 public BigDecimal getTotal() {
66 return total;
67 }
68
69 /**
70 * Gets the item.
71 *
72 * @return the item
73 */
74 public Item getItem() {
75 return item;
76 }
77
78 /**
79 * Sets the item.
80 *
81 * @param item
82 * the item
83 */
84 public void setItem(Item item) {
85 this.item = item;
86 calculateTotal();
87 }
88
89 /**
90 * Gets the quantity.
91 *
92 * @return the quantity
93 */
94 public int getQuantity() {
95 return quantity;
96 }
97
98 /**
99 * Sets the quantity.
100 *
101 * @param quantity
102 * the quantity
103 */
104 public void setQuantity(int quantity) {
105 this.quantity = quantity;
106 calculateTotal();
107 }
108
109 /**
110 * Increment quantity.
111 */
112 public void incrementQuantity() {
113 quantity++;
114 calculateTotal();
115 }
116
117 /**
118 * Calculate total.
119 */
120 private void calculateTotal() {
121 total = Optional.ofNullable(item).map(Item::getListPrice).map(v -> v.multiply(new BigDecimal(quantity)))
122 .orElse(null);
123 }
124
125 }