⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cartbean.java

📁 JSP设计(第三版)一书源代码 JSP设计(第三版)》得到了充分的修订和更新
💻 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 2.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 Iterator of ProductBeans
     */
    public ProductBean[]  getProductList() {
	ProductBean[] products = null;
	synchronized(cart) {
	    products = new ProductBean[cart.size()];
	    cart.toArray(products);
	}
        return products;
    }

    /**
     * Returns the total price for all products in the cart
     *
     * @return the total price
     */
    public float getTotal() {
        float total = 0;
        Iterator prods = cart.iterator();
        while (prods.hasNext()) {
            ProductBean product = (ProductBean) prods.next();
            float price = product.getPrice();
            total += price;
        }
        return total;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -