网上购物系统用SSH实现的
源代码在线查看: shoppingcartimpl.java
package cn.com.tarena.ecport.biz.impl;
import java.util.Iterator;
import cn.com.tarena.ecport.biz.IShoppingCart;
import cn.com.tarena.ecport.pojo.OrderLine;
import cn.com.tarena.ecport.pojo.Orders;
import cn.com.tarena.ecport.pojo.Product;
public class ShoppingCartImpl implements IShoppingCart {
private Orders order = new Orders();
public void addProduct(Product product, int amount) {
// 得到order中的所有orderLine比较与实参product的id是否相等,相等则在原有基础加上数量
Iterator it = order.getOrderlines().iterator();
while(it.hasNext()){
OrderLine orderLine = (OrderLine)it.next();
if(orderLine!=null&&product.getProductid()==orderLine.getProduct().getProductid()){
orderLine.setAmount(orderLine.getAmount()+amount);
return;
}
}
// 执行到这里说明,没有匹配项。所以新增一条
OrderLine orderLine = new OrderLine();
orderLine.setAmount(amount);
orderLine.setProduct(product);
orderLine.setOrders(order);
order.getOrderlines().add(orderLine);
}
public double getTotalPrice() {
Iterator it = order.getOrderlines().iterator();
double sum = 0;
while(it.hasNext()){
OrderLine orderLine = (OrderLine)it.next();
sum = sum + orderLine.getProduct().getBaseprice()*orderLine.getAmount();
}
order.setCost(sum);
return sum;
}
public void modifyProductAmountById(Long productid, int amount) {
// 得到order中的所有orderLine比较与实参product的id是否相等,相等则在原有基础加上数量
Iterator it = order.getOrderlines().iterator();
while(it.hasNext()){
OrderLine orderLine = (OrderLine)it.next();
if(orderLine!=null&&productid==orderLine.getProduct().getProductid()){
orderLine.setAmount(amount);
return;
}
}
}
public void removeAllProducts() {
order.getOrderlines().clear();
}
public void removeProductById(Long productid) {
Iterator it = order.getOrderlines().iterator();
OrderLine orderLine = null;
while(it.hasNext()){
orderLine = (OrderLine)it.next();
if(orderLine!=null&&productid==orderLine.getProduct().getProductid()){
break;
}
}
if(orderLine!=null){
order.getOrderlines().remove(orderLine);
}
}
public Orders getOrder() {
return order;
}
}