Java Cart .

Java Cart Cart java

package cart;

import cart.applicationBeans.ItemInventoryBean;
import java.util.HashMap;
import java.util.Iterator;

//Cart - holds all items prior to purchase
//author - Lawrence Truett - FluffyCat.com
//date - May 19, 2003 - San Diego, CA
public class Cart {
    private HashMap contents;
    private ItemInventoryBean itemInventoryBean;
    
    //instantiate with ItemInventoryBean to 
    //be certian it is present for finalize()
    public Cart(ItemInventoryBean itemInventoryBeanIn) {
        contents = new HashMap();
        itemInventoryBean = itemInventoryBeanIn;
    }
    
    private void addItem(Item itemIn) {
        contents.put(itemIn, new Long(0));
    }
    
    //updates ItemInventory - works for item type with inventory
    public boolean removeItem(Item itemIn) {
            
        if (!itemIn.getHasInventory()) {
            if (contents.remove(itemIn) == null) {
                return false;
            } else {
                return true;
            }
        } else {
            long amtInCart = getItemQuantity(itemIn);
            if (contents.remove(itemIn) != null) {
                long removed = 
                   itemInventoryBean.updateBeanReserved(itemIn.getKey(), 
                                                        amtInCart * -1);
                if (removed  == amtInCart * -1) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }
    
    public long reserve(Item itemIn, long amount) {
        if (itemIn.getOnePerCart()) {           
            if (contents.containsKey(itemIn)) {
                if (getItemQuantity(itemIn) > 1) {
                    amount = getItemQuantity(itemIn) - 1;
                } else {
                    amount = 0;
                }
            } else {
                if (amount != 0) {
                    amount = 1;
                }
            }    
        }

        if (!itemIn.getHasInventory()) {
            long amountInCart = amount;
            if (contents.containsKey(itemIn)) {
                amountInCart = getItemQuantity(itemIn) + amount;
            }
            contents.put(itemIn, new Long(amountInCart));
            return amount;
        }

        if (!contents.containsKey(itemIn)) {
            addItem(itemIn);
        }

        long reservedAmount = 
            itemInventoryBean.updateBeanReserved(itemIn.getKey(),
                                                 amount);
        contents.put(itemIn, 
                     new Long(getItemQuantity(itemIn) + reservedAmount));
        return reservedAmount;
    }
    
    
    public int getItemCount() {
        return contents.size();
    }
    
    public long getItemQuantity(Item itemIn) {
        if (contents.containsKey(itemIn)) {
            return ((Long)contents.get(itemIn)).longValue();
        } else {
            return 0;
        }
    }
    
    public double getItemTotal(Item itemIn) {
        return itemIn.getPriceForQuantity(getItemQuantity(itemIn));
    }
    
    public double getCartTotal() {
        double cartTotal = 0;
        Iterator iterator = contents.keySet().iterator();
        while (iterator.hasNext()) {
            Item item = (Item)iterator.next();
            cartTotal += getItemTotal(item);
        }
        return cartTotal;
    }
    
    public Object[] getItemArray() {
        return contents.keySet().toArray();
    }
    
    public void emptyCart() {
        Item[] items = ((Item[])contents.keySet().toArray());
        for (int i = items.length - 1; i < items.length; i++) {
            removeItem(items[i]);
        }
    }
    
    protected void finalize() throws Throwable {
        super.finalize();
        emptyCart();
    }
}
To download source right-click here and "Save As...".
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by cuong_map on 2007-11-07 Rate this Comment

not run

Comment by cuong_map on 2007-11-07 Rate this Comment

Be the first to comment on Java Cart Cart java.

 
Sign in to comment on Java Cart Cart java.