buycart.java

来自「巴巴运动网源码 传智博客出品 不全 一部分代码 可以参考」· Java 代码 · 共 75 行

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