📄 shoppingcartimpl.java
字号:
package cn.com.tarena.ecport.biz.impl;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import cn.com.tarena.ecport.biz.IShoppingCart;
import cn.com.tarena.ecport.pojo.OrderLine;
import cn.com.tarena.ecport.pojo.Product;
public class ShoppingCartImpl implements IShoppingCart{
private Map<Long, OrderLine> map = new TreeMap<Long, OrderLine>();
public void addProduct(Product product, int amount) {
Long productId = product.getProductid();
if (map.containsKey(productId)) {
OrderLine orderLine = map.get(productId);
int total = orderLine.getAmount()+amount;
orderLine.setAmount(total);
map.put(productId, orderLine);
} else {
OrderLine orderLine = new OrderLine(amount);
orderLine.setAmount(amount);
orderLine.setProduct(product);
map.put(productId, orderLine);
}
}
public double getTotalPrice() {
Set<Long> set = map.keySet();
double totalPrice = 0;
for(Long productid : set){
OrderLine orderLine = map.get(productid);
int num = orderLine.getAmount();
double basePrice = orderLine.getProduct().getBaseprice();
double price = basePrice*num;
totalPrice+=price;
}
return totalPrice;
}
public void modifyProductAmountById(Long productid, int amount) {
OrderLine orderLine = map.get(productid);
orderLine.setAmount(amount);
map.put(productid, orderLine);
}
public void removeAllProducts() {
map = new TreeMap<Long, OrderLine>();
}
public void removeProductById(Long productId) {
map.remove(productId);
}
public Map<Long, OrderLine> getOrderLineMap() {
return map;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -