Java Cart .

Java Cart CartScreenWrangler java

package cart.screenWranglers;

import cart.Cart;
import cart.Category;
import cart.Item;
import cart.ItemInventory;
import cart.applicationBeans.CategoryBean;
import cart.applicationBeans.ItemBean;
import cart.applicationBeans.ItemCategoryBean;
import cart.applicationBeans.ItemInventoryBean;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ListIterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;

//CartScreenWrangler
//author - Lawrence Truett - FluffyCat.com
//date - July 15, 2003 - San Diego, CA
public class CartScreenWrangler extends BaseScreenWrangler {
    static final DecimalFormat decimalFormat = 
        new DecimalFormat("0.00");
    
    public CartScreenWrangler() {this("/cart/jsps/Cart.jsp");}
    
    public CartScreenWrangler(String nextScreenIn) {
        super(nextScreenIn);
    }
    
    public void wrangleScreen(HttpServletRequest request, 
                              ServletContext servletContext) {
        
        char action = baseNextScreenSwitch(request, 
                                           servletContext); 
               
        if (action != 'R') {
            getNextWrangler().prepFirstScreen(request, 
                                              servletContext);
        } else {
            StringBuffer cartSubs = allScreens(request, 
                                               servletContext);
            request.setAttribute("jspCart", cartSubs);
            jspCatAttribute(request);
            jspItemAttribute(request);
        }
    }
    
    void prepFirstScreen(HttpServletRequest request, 
                         ServletContext servletContext) {
        StringBuffer cartSubs = allScreens(request, servletContext);
        request.setAttribute("jspCart", cartSubs);
        jspCatAttribute(request);
        jspItemAttribute(request);
    }
    
    StringBuffer allScreens(HttpServletRequest request, 
                            ServletContext servletContext) {
        ItemBean itemBean = 
            (ItemBean)servletContext.getAttribute("itemBean");
        ItemInventoryBean itemInventoryBean = 
            (ItemInventoryBean)servletContext.getAttribute(
                "itemInventoryBean");
        StringBuffer cartSubs = new StringBuffer();        
        
        Cart cart = 
            (Cart)(request.getSession().getAttribute("cart"));
      
        if (null == cart) {
            cart = new Cart(itemInventoryBean);
            request.getSession().setAttribute("cart", cart);
        }

        char subAction;
        String stringSubAction = 
            request.getParameter("subaction");
        if (null == stringSubAction) {
            subAction = ' ';
        } else {
            subAction = stringSubAction.charAt(0);
        }
        String stringItem = request.getParameter("item");
        if (stringItem == null) {stringItem = "0";}
        long longItem = Long.parseLong(stringItem);
        long amount = 0;
        String stringAmount = request.getParameter("amount");
        if (null != stringAmount) {
            amount = new Long(stringAmount).longValue();
        }
        
        if (subAction == 'A') {
            Item item = itemBean.getItem(longItem);
            long reserved = cart.reserve(item, amount);
            if (reserved == amount) {
                cartSubs.append("item " + 
                                item.getDescription() + 
                                " reserved");
            } else {
                if (reserved > 0) {
                    cartSubs.append("only " + 
                                     reserved + 
                                     " of item " + 
                                     item.getDescription() + 
                                     " reserved");
                } else {
                    cartSubs.append("
item " + item.getDescription() + " not reserved"); } } } if (subAction == 'R') { Item item = itemBean.getItem(longItem); boolean removed = cart.removeItem(item); if (removed) { cartSubs.append("
item " + item.getDescription() + " removed"); } else { cartSubs.append("
item " + item.getDescription() + " not removed"); } } Object items[] = cart.getItemArray(); for (int i = 0; items.length > i; i++) { cartSubs.append(""); Item item = (Item)items[i]; long itemKey = item.getKey(); cartSubs.append("Key: " + itemKey); cartSubs.append("Price: " + decimalFormat.format(item.getPrice())); cartSubs.append("Description: " + item.getDescription()); cartSubs.append("Type: " + item.getItemType()); long amountInCart = cart.getItemQuantity(item); cartSubs.append("Amount of " + item.getDescription() + " in Cart: " + amountInCart); cartSubs.append("
Total for item " + item.getDescription() + " in Cart: " + decimalFormat.format(cart.getItemTotal(item))); if (item.getHasInventory()) { ItemInventory itemInventory = itemInventoryBean.getItemInventory(itemKey); long inventory = itemInventory.getInventory(); long reserved = itemInventory.getReserved(); cartSubs.append("
Total Item Inventory: " + inventory); cartSubs.append("
Total Item Reserved: " + reserved); if ((inventory - reserved) > 0) { cartSubs.append("Add One More To Cart"); } else { cartSubs.append("
Item is out of Stock"); } } else { cartSubs.append("
Total Item Inventory: NA"); cartSubs.append("
Total Item Reserved: NA"); if (!item.getOnePerCart()) { cartSubs.append("Add One More To Cart"); } else if ((amountInCart < 1) && (item.getOnePerCart())) { cartSubs.append("Add To Cart"); } } if (amountInCart > 0) { if (item.getOnePerCart()) { cartSubs.append("Remove From Cart"); } else { cartSubs.append("Remove One From Cart"); cartSubs.append("Remove All From Cart"); } } cartSubs.append("
"); cartSubs.append(""); } cartSubs.append("

Cart Total: " + decimalFormat.format(cart.getCartTotal())); return cartSubs; } }
To download source right-click here and "Save As...".
Comments
Sign in to be the first to comment on Java Cart CartScreenWrangler java.