shoppingcart.java

来自「jsf 控件的中ajax的支持 jsf控件的应用」· Java 代码 · 共 66 行

JAVA
66
字号
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 + =
减小字号Ctrl + -
显示快捷键?