FileDocCategorySizeDatePackage
CartBean.javaAPI DocExample1477Thu Jun 28 16:14:16 BST 2001com.ora.jsp.beans.shopping

CartBean.java

package com.ora.jsp.beans.shopping;

import java.io.*;
import java.util.*;

/**
 * This class represents a shopping cart. It holds a list of products.
 *
 * @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
 * @version 1.0
 */
public class CartBean implements Serializable {
    private Vector cart = new Vector();

    /**
     * Adds a product to the cart, if it's not already there.
     *
     * @param product the ProductBean
     */
    public void setProduct(ProductBean product) {
        if (product != null && cart.indexOf(product) == -1) {
            cart.addElement(product);
        }
    }

    /**
     * Returns the product list.
     *
     * @return an Enumeration of ProductBeans
     */
    public Enumeration getProducts() {
        return cart.elements();
    }

    /**
     * Returns the total price for all products in the cart
     *
     * @return the total price
     */
    public float getTotal() {
        float total = 0;
        Enumeration prods = getProducts();
        while (prods.hasMoreElements()) {
            ProductBean product = (ProductBean) prods.nextElement();
            float price = product.getPrice();
            total += price;
        }
        return total;
    }
    
    /**
     * Returns true if the cart is empty
     *
     * @return true if the cart is empty
     */
    public boolean isEmpty() {
        return cart.size() == 0;
    }
}