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

📄 shoppingcart.java

📁 beginJsp2.0外文书籍源代码
💻 JAVA
字号:
package com.wrox.shop;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;/** * Models a shopping cart holding a number of Books. */public class ShoppingCart extends ActionForm {        private final List items = new ArrayList();        /**     * Retrieves the items in the cart.     * @return The cart items.     */    public List getItems() {        return items;    }        /**     * Retrieves a specific item.     * @param index The number in the list.     * @return The item.     */        public ShoppingCartItem getItem(int index) {        return (ShoppingCartItem)items.get(index);    }        /**     * Sets a specific item.     * @param index The number in the list.     * @param item The item.     */        public void setItem(int index, ShoppingCartItem item) {        items.set(index, item);    }        /**     * Retrieves the total cost for the cart.     * @return The total cost.     */        public double getTotal() {        double total = 0.0;        for (Iterator i = items.iterator(); i.hasNext(); ) {            ShoppingCartItem item = (ShoppingCartItem)i.next();            total += item.getBook().getPrice() * item.getQuantity();        }        return total;    }        /**     * Adds a Book to the Cart. If the book is already in the cart, its      * quantity is increased by one.     * @param book The Book to add.     */        public void addBook(Book book) {        ShoppingCartItem newItem = new ShoppingCartItem(book);        if (items.contains(newItem)) {            ShoppingCartItem item = getItem(items.indexOf(newItem));            item.setQuantity(item.getQuantity() + 1);        } else {            items.add(newItem);        }    }        /**     * Validates the book and removes any zero (or negative) quantity     * books from the cart. Called by the Struts controller.     * @param mapping The ActionMapping     * @param request The current request     * @return <code>null</code> (always valid)     */        public ActionErrors validate(ActionMapping mapping,                                  HttpServletRequest request) {        for (Iterator i = items.iterator(); i.hasNext(); ) {            ShoppingCartItem item = (ShoppingCartItem)i.next();            if (item.getQuantity() <= 0) { i.remove(); }        }        return null;    }}

⌨️ 快捷键说明

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