Java Cart .

Java Cart ItemInventoryBean java

package cart.applicationBeans;

import cart.ItemInventory;
import cart.dbUtils.ItemInventoryDBUtil;
import cart.dbUtils.DBConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ListIterator;

//ItemInventoryBean - loads item quantities into a HashMap,
//                 keeping the most recently ones called

//author - Lawrence Truett - FluffyCat.com
//date - July 14, 2003 - San Diego, CA
public class ItemInventoryBean {
    private HashMap itemInventoryMap = new HashMap();
    
    //must have empty constructor to be a bean
    public ItemInventoryBean() {
    }
    
    //only a good idea to load all items 
    //    if you have a limited number of items
    public ItemInventoryBean(DBConnection dbc) {
        ListIterator li = 
            (ItemInventoryDBUtil.getAllItemInventory(dbc)).listIterator();
        while (li.hasNext()) {
            ItemInventory itemInventory = (ItemInventory)li.next();
            itemInventoryMap.put(
                new Long(itemInventory.getItemKey()), itemInventory);
        }
    }
    
    //returns item only if in hashMap
    public ItemInventory getItemInventory(long key) {
        Long lKey = new Long(key);
        return this.getItemInventory(lKey);
    }
    
    //returns item only if in hashMap
    public ItemInventory getItemInventory(Long key) {
        if (itemInventoryMap.containsKey(key)) {
            return (ItemInventory)itemInventoryMap.get(key);
        } else {
            return null;
        }
    }
    
    public ItemInventory getItemInventory(long itemKey, 
                                          DBConnection dbc) {
        Long lItemKey = new Long(itemKey);
        if (itemInventoryMap.containsKey(lItemKey)) {
            return (ItemInventory)itemInventoryMap.get(lItemKey);
        } else {
            ItemInventory itemInventory = 
                ItemInventoryDBUtil.getItemInventoryForItemKey(itemKey,
                                                              dbc);
            if (null != itemInventory) {
                itemInventoryUpdateAlgo(itemInventory);
            }
            return itemInventory;
        }
    }
    
    //puts new items into items HashMap,
    //  with some algorythm to delete lesser used entries
    private void itemInventoryUpdateAlgo(ItemInventory itemInventory) {
        itemInventoryMap.put(
            new Long(itemInventory.getItemKey()), itemInventory);
    }
    
    //Assumes that any item that is being reserved will 
    //    have been used recently enough to still be in the bean.
    //Returns acutal number reserved
    //To keep all manipulation of reserved in this in
    //    one synchronized method,
    //    negative numbers can be passed in
    //    when items are un-reserved.
    public synchronized long updateBeanReserved(long itemKey, 
                                                long reserved) {
        Long lItemKey = new Long(itemKey);
        long longToReturn;
        ItemInventory itemInventory = 
            (ItemInventory)itemInventoryMap.get(lItemKey);
        long previousReserved = itemInventory.getReserved();
        long currentInventory = itemInventory.getInventory();
        
        if (reserved > 0) {
            if ((reserved + previousReserved) > currentInventory) {
                itemInventory.setReserved(currentInventory);
                longToReturn = currentInventory - previousReserved;
            } else {
                itemInventory.setReserved(reserved + previousReserved);
                longToReturn = reserved;
            }
        } else {
            if ((previousReserved) > (reserved * -1)) {
                itemInventory.setReserved(previousReserved + reserved);
                longToReturn = reserved;
            } else {
                itemInventory.setReserved(0);
                longToReturn = (previousReserved * -1);
            }
        }
        itemInventoryMap.put(lItemKey, itemInventory);
        return longToReturn;
    }
    
    //assumes that any item that is being reserved will have 
    //    been used recently enough to still be in the bean.
    //used after actual sale
    public synchronized long 
        updateItemInventory(long itemKey, 
                            long inventoryToReduce, 
                            long reservedToReduce, 
                            DBConnection dbc) {
        Long lItemKey = new Long(itemKey);
        long longToReturn;
        ItemInventory itemInventory = 
            (ItemInventory)itemInventoryMap.get(lItemKey);
        long previousReserved = itemInventory.getReserved();
        long previousInventory = itemInventory.getInventory();
        if (reservedToReduce > previousReserved) {
            itemInventory.setReserved(0);
        } else {
            itemInventory.setReserved(previousReserved - 
                                      reservedToReduce);
        }
        if (inventoryToReduce > previousInventory) {
            itemInventory.setInventory(0);
            longToReturn = previousInventory;
        } else {
            itemInventory.setInventory(previousInventory - 
                                       inventoryToReduce);
            longToReturn = inventoryToReduce;
        }
        itemInventoryMap.put(lItemKey, itemInventory);
        ItemInventoryDBUtil.updateInventoryReserved(itemInventory, dbc);
        return longToReturn;
    }
}
To download source right-click here and "Save As...".
Comments
Sign in to be the first to comment on Java Cart ItemInventoryBean java.