?? shoppingcart.java
字號(hào):
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 {
/**
* 添加商品到購(gòu)物車(chē)
* */
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();
}
/**
* 獲得購(gòu)物車(chē)中所有商品的價(jià)格
* */
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;
}
/**
* 根據(jù)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));
}
}
}
/**
* 根據(jù)ID更新商品的數(shù)量
* */
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;
}
/**
* 根據(jù)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;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -