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

📄 shoppingcart.java

📁 《基于Eclipse的开源框架技术与实战》[第5章]随书源码
💻 JAVA
字号:
package com.free.struts.storefront.framework;

import java.util.List;
import java.util.LinkedList;

/**
 * <p>Title: Eclipse Plugin Development</p>
 * <p>Description: Free download</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: Free</p>
 * @author gan.shu.man
 * @version 1.0
 */

public class ShoppingCart {
	/**
	 * 添加商品到购物车
	 * */
	public void addItem(ShoppingCartItem newItem) {

		// Check to see if this item is already present, if so, inc the qty
		int size = getSize();
		ShoppingCartItem cartItem = findItem(newItem.getId());
		if (cartItem != null) {
			cartItem
					.setQuantity(cartItem.getQuantity() + newItem.getQuantity());
		} else {

			// Must have been a different item, so add it to the cart
			items.add(newItem);
		}
	}

	public void setItems(List otherItems) {
		items.addAll(otherItems);
	}

	public ShoppingCart() {
		items = new LinkedList();
	}

	public void setSize(int size) {

		// The size is really determined by the list.

		// This is needed so that it can act as a JavaBean with a get/set.
	}

	public void empty() {
		items.clear();
	}

	/**
	 * 获得购物车中所有商品的价格
	 * */
	public double getTotalPrice() {
		double total = 0.0;
		int size = items.size();
		for (int i = 0; i < size; i++) {
			total += ((ShoppingCartItem) items.get(i)).getExtendedPrice();
		}
		return total;
	}

	/**
	 * 根据ID删除商品
	 * */
	public void removeItem(String itemId) {
		int size = getSize();
		ShoppingCartItem item = findItem(itemId);
		if (item != null) {
			items.remove(item);
		}
	}

	public void removeItems(List itemIds) {
		if (itemIds != null) {
			int size = itemIds.size();
			for (int i = 0; i < size; i++) {
				removeItem((String) itemIds.get(i));
			}
		}
	}

	/**
	 * 根据ID更新商品的数量
	 * */
	public void updateQuantity(String itemId, int newQty) {
		ShoppingCartItem item = findItem(itemId);
		if (item != null) {
			item.setQuantity(newQty);
		}
	}

	public int getSize() {
		return items.size();
	}

	public List getItems() {
		return items;
	}

	/**
	 * 根据ID找到特定的ShoppingCartItem
	 * */
	private ShoppingCartItem findItem(String itemId) {
		ShoppingCartItem item = null;
		int size = getSize();
		for (int i = 0; i < size; i++) {
			ShoppingCartItem cartItem = (ShoppingCartItem) items.get(i);
			if (itemId.equals(cartItem.getId())) {
				item = cartItem;
				break;
			}
		}
		return item;
	}

	private List items = null;
}

⌨️ 快捷键说明

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