View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
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    *      http://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 com.ibatis.jpetstore.domain;
17  
18  import java.io.Serializable;
19  import java.math.BigDecimal;
20  
21  
22  public class CartItem implements Serializable {
23  
24    /* Private Fields */
25  
26    private Item item;
27    private int quantity;
28    private boolean inStock;
29    private BigDecimal total;
30  
31    /* JavaBeans Properties */
32  
33    public boolean isInStock() {
34      return inStock;
35    }
36  
37    public void setInStock(boolean inStock) {
38      this.inStock = inStock;
39    }
40  
41    public BigDecimal getTotal() {
42      return total;
43    }
44  
45    public Item getItem() {
46      return item;
47    }
48  
49    public void setItem(Item item) {
50      this.item = item;
51      calculateTotal();
52    }
53  
54    public int getQuantity() {
55      return quantity;
56    }
57  
58    public void setQuantity(int quantity) {
59      this.quantity = quantity;
60      calculateTotal();
61    }
62  
63    /* Public methods */
64  
65    public void incrementQuantity() {
66      quantity++;
67      calculateTotal();
68    }
69  
70    /* Private methods */
71  
72    private void calculateTotal() {
73      if (item != null && item.getListPrice() != null) {
74        total = item.getListPrice().multiply(new BigDecimal(quantity));
75      } else {
76        total = null;
77      }
78    }
79  
80  }