?? cart.java
字號:
package bo;
import java.util.*;
import vo.Products;
public class Cart extends ArrayList {
private Double totalPrice;
public Cart() {
}
public void setTotalPrice(Double totalPrice) {
this.totalPrice = totalPrice;
}
/**
* 添加一個商品到購物車
* @param product Products
* @param quantity Integer
*/
public void addProduct(Products product, Integer quantity) {
if (quantity == null) {
quantity = new Integer(1);
}
CartItem item = this.getCartItem(product.getProductId());
if (item != null) {
item.setQuantity(new Integer(item.getQuantity().intValue() +
quantity.intValue()));
} else {
item = new CartItem();
item.setProduct(product);
item.setQuantity(quantity);
this.add(item);
}
}
/**
* 計算當前購物車所有商品的總金額
* @return Double
*/
public Double getTotalPrice() {
double totalPrice = 0;
for (int i = 0; i < this.size(); i++) {
CartItem item = (CartItem)this.get(i);
totalPrice += item.getQuantity().intValue() *
item.getProduct().getPrice().doubleValue();
}
return new Double(totalPrice);
}
/**
* 從購物車中刪除指定的商品
* @param product Products
*/
public void removeProduct(Products product) {
CartItem item = this.getCartItem(product.getProductId());
if (item != null) {
this.remove(item);
}
}
/**
* 獲得指定商品id的購買記錄,如果不存在,則返回Null
* @param productId String
* @return CartItem
*/
public CartItem getCartItem(String productId) {
for (int i = 0; i < this.size(); i++) {
CartItem item = (CartItem)this.get(i);
if (item.getProduct().getProductId().equals(productId)) {
System.out.println("getCartItem ==>" + item.getProduct().getProductId());
return item;
}
}
return null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -