📄 shoppingcartimpl.java
字号:
package cn.com.tarena.ecport.biz.impl;import java.util.Collection;import java.util.Iterator;import java.util.LinkedHashSet;import java.util.Set;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 orders = new Orders(); public void addProduct(Product product, int amount) { Long id = product.getProductid(); Set orderLines = orders.getOrderlines(); Iterator<OrderLine> it = orderLines.iterator(); while (it.hasNext()) { OrderLine line = it.next(); if (line.getProduct().getProductid() == id) { line.setAmount(line.getAmount() + 1); getTotalPrice(); return; } } OrderLine orderLine = new OrderLine(); orderLine.setProduct(product); orderLine.setAmount(amount); orderLine.setOrders(orders);// orderLine.setLineid(id); orders.getOrderlines().add(orderLine); getTotalPrice(); } public double getTotalPrice() { Set orderLines = orders.getOrderlines(); Iterator<OrderLine> it=orderLines.iterator(); double cost=0; while(it.hasNext()){ OrderLine line=it.next(); cost+=(line.getAmount())*(line.getProduct().getBaseprice()); } orders.setCost(cost); return cost; } public void modifyProductAmountById(Long productid, int amount) { Set orderLines = orders.getOrderlines(); Iterator<OrderLine> it=orderLines.iterator(); while(it.hasNext()){ OrderLine line=it.next(); if(line.getProduct().getProductid()==productid){ line.setAmount(amount); getTotalPrice(); } } } public void removeAllProducts() { Set orderLines = orders.getOrderlines(); orderLines.removeAll(orderLines); getTotalPrice(); } public void removeProductById(Long productId) { Set orderLines = orders.getOrderlines(); OrderLine o=null; Iterator<OrderLine> it=orderLines.iterator(); while(it.hasNext()){ OrderLine line=it.next(); if(line.getProduct().getProductid()==productId){ o=line; } } orderLines.remove(o); } public Orders getOrders() { return this.orders; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -