View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package com.ibatis.jpetstore.domain;
    18
    19  import com.ibatis.common.util.PaginatedArrayList;
    20  import com.ibatis.common.util.PaginatedList;
    21
    22  import java.io.Serializable;
    23  import java.math.BigDecimal;
    24  import java.util.*;
    25
    26  public class Cart implements Serializable {
    27
    28    /* Private Fields */
    29
    30    private final Map itemMap = Collections.synchronizedMap(new HashMap());
    31    private final PaginatedList itemList = new PaginatedArrayList(4);
    32
    33    /* JavaBeans Properties */
    34
    35    public Iterator getCartItems() {
    36      return itemList.iterator();
    37    }
    38
    39    public PaginatedList getCartItemList() {
    40      return itemList;
    41    }
    42
    43    public int getNumberOfItems() {
    44      return itemList.size();
    45    }
    46
    47    /* Public Methods */
    48
    49    public Iterator getAllCartItems() {
    50      List allItems = new ArrayList();
    51      itemList.gotoPage(0);
    52      allItems.addAll(itemList);
    53      while (itemList.nextPage()) {
    54        allItems.addAll(itemList);
    55      }
    56      return allItems.iterator();
    57    }
    58
    59    public boolean containsItemId(String itemId) {
    60      return itemMap.containsKey(itemId);
    61    }
    62
    63    public void addItem(Item item, boolean isInStock) {
    64      CartItem cartItem = (CartItem) itemMap.get(item.getItemId());
    65      if (cartItem == null) {
    66        cartItem = new CartItem();
    67        cartItem.setItem(item);
    68        cartItem.setQuantity(0);
    69        cartItem.setInStock(isInStock);
    70        itemMap.put(item.getItemId(), cartItem);
    71        itemList.add(cartItem);
    72      }
    73      cartItem.incrementQuantity();
    74    }
    75
    76
    77    public Item removeItemById(String itemId) {
    78      CartItem cartItem = (CartItem) itemMap.remove(itemId);
    79      if (cartItem == null) {
    80        return null;
    81      } else {
    82        itemList.remove(cartItem);
    83        return cartItem.getItem();
    84      }
    85    }
    86
    87    public void incrementQuantityByItemId(String itemId) {
    88      CartItem cartItem = (CartItem) itemMap.get(itemId);
    89      cartItem.incrementQuantity();
    90    }
    91
    92    public void setQuantityByItemId(String itemId, int quantity) {
    93      CartItem cartItem = (CartItem) itemMap.get(itemId);
    94      cartItem.setQuantity(quantity);
    95    }
    96
    97    public BigDecimal getSubTotal() {
    98      BigDecimal subTotal = new BigDecimal("0");
    99      Iterator items = getAllCartItems();
    100     while (items.hasNext()) {
    101       CartItem cartItem = (CartItem) items.next();
    102       Item item = cartItem.getItem();
    103       BigDecimal listPrice = item.getListPrice();
    104       BigDecimal quantity = new BigDecimal(String.valueOf(cartItem.getQuantity()));
    105       subTotal = subTotal.add(listPrice.multiply(quantity));
    106     }
    107     return subTotal;
    108   }
    109
    110 }