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 public class CartItem implements Serializable {
26
27 /** The serial version uid. */
28 private static final long serialVersionUID = 6620528781626504362L;
29
30 /** The item. */
31 private Item item;
32 /** The quantity. */
33 private int quantity;
34 /** The in stock. */
35 private boolean inStock;
36 /** The total. */
37 private BigDecimal total;
38
39 /**
40 * Checks if is in stock.
41 *
42 * @return true, if successful
43 */
44 public boolean isInStock() {
45 return inStock;
46 }
47
48 /**
49 * Sets the in stock.
50 *
51 * @param inStock
52 * the in stock
53 */
54 public void setInStock(boolean inStock) {
55 this.inStock = inStock;
56 }
57
58 /**
59 * Gets the total.
60 *
61 * @return the total
62 */
63 public BigDecimal getTotal() {
64 return total;
65 }
66
67 /**
68 * Gets the item.
69 *
70 * @return the item
71 */
72 public Item getItem() {
73 return item;
74 }
75
76 /**
77 * Sets the item.
78 *
79 * @param item
80 * the item
81 */
82 public void setItem(Item item) {
83 this.item = item;
84 calculateTotal();
85 }
86
87 /**
88 * Gets the quantity.
89 *
90 * @return the quantity
91 */
92 public int getQuantity() {
93 return quantity;
94 }
95
96 /**
97 * Sets the quantity.
98 *
99 * @param quantity
100 * the quantity
101 */
102 public void setQuantity(int quantity) {
103 this.quantity = quantity;
104 calculateTotal();
105 }
106
107 /**
108 * Increment quantity.
109 */
110 public void incrementQuantity() {
111 quantity++;
112 calculateTotal();
113 }
114
115 /**
116 * Calculate total.
117 */
118 private void calculateTotal() {
119 total = Optional.ofNullable(item).map(Item::getListPrice).map(v -> v.multiply(new BigDecimal(quantity)))
120 .orElse(null);
121 }
122
123 }