📄 shoppingcart.java
字号:
package beans;
import java.util.ArrayList;
import javax.faces.context.FacesContext;
public class ShoppingCart {
private ArrayList products;
public ShoppingCart() {
products = new ArrayList(5);
products.add(new CartItem(new Product("P01", "Hot air balloon", "Big and colorful hot air balloon. Comes inflated.", "/theme/img/baloon.jpg", 5.95)));
products.add(new CartItem(new Product("P02", "Park bench", "Freshly painted. Can be used in any park.", "/theme/img/bench.jpg", 2.95)));
products.add(new CartItem(new Product("P03", "Lawn chair", "Relax with style. Available in two colors -- red or yellow.", "/theme/img/chair.jpg", 29.95)));
products.add(new CartItem(new Product("P04", "Men's leather jacket", "Best leather there is.", "/theme/img/jacket.jpg", 17.95)));
products.add(new CartItem(new Product("P05", "Railway crossing sign", "Have a railroad crossing? Need a sign?", "/theme/img/sign.jpg", 35.95)));
}
public ArrayList getProducts() {
return products;
}
public void setProducts(ArrayList products) {
this.products = products;
}
public double getTotalPrice() {
double total = 0;
for (int i = 0; i < products.size(); i++) {
CartItem item = (CartItem) products.get(i);
Product product = item.getProduct();
total += product.getPrice() * item.getQuantity().doubleValue();
}
return total;
}
public double getTotal() {
return getTotalPrice() + getTax() + getShipping();
}
public double getTax() {
return getTotalPrice() * 0.05;
}
public double getShipping() {
double total = getTotalPrice();
if(total == 0.0 || total > 25.00) {
return 0.0;
}
return 4.95;
}
public Product getSelectedProduct() {
String id = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("itemid");
for (int i = 0; i < products.size(); i++) {
CartItem item = (CartItem) products.get(i);
Product product = item.getProduct();
if(product.getId().equals(id)) {
return product;
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -