📄 buycart.java
字号:
package com.itcast.bean;
import java.util.ArrayList;
import java.util.List;
public class BuyCart {
private List<BuyItem> items = new ArrayList<BuyItem>();
public List<BuyItem> getItems() {
return items;
}
/**
* 添加购物项
* @param item
*/
public void addItem(BuyItem item){
if(!items.contains(item)){
items.add(item);
}else{//如果已经存在该购物项,就累加其购买数量
for(BuyItem bi : items){
if(bi.equals(item)){
bi.setAmount(bi.getAmount()+1);
break;
}
}
}
}
/**
* 删除所有购物项
*/
public void removeAll(){
items.clear();
}
/**
* 删除购物项
* @param item
*/
public void removeBuyItem(BuyItem item){
if(items.contains(item)){
items.remove(item);
}
}
/**
* 获取应付总金额
* @return
*/
public float getTotalPrice(){
float total = 0f;
for(BuyItem bi : this.items){
total += bi.getProduct().getSellprice() * bi.getAmount();
}
return total;
}
/**
* 获取市场价总金额
* @return
*/
public float getTotalMarketPrice(){
float total = 0f;
for(BuyItem bi : this.items){
total += bi.getProduct().getMarketprice() * bi.getAmount();
}
return total;
}
/**
* 总节省金额
* @return
*/
public float getTotalSavedPrice(){
return getTotalMarketPrice()-getTotalPrice();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -