⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcart.java

📁 音乐网站涉及到很多数据库查询
💻 JAVA
字号:
/**
 * 
 */
package serverBag;

import java.util.*;

/**
 * @author Administrator
 *
 */
public class ShoppingCart {

	/**
	 * 购物车属性
	 */
	private Orders orderObj;
	private ArrayList orderDetail;
	
	
	public Orders getOrderObj(){return orderObj;}
	public void setOrderObj(Orders inOrder){orderObj = inOrder;}
	public ArrayList getOrderDetail(){return orderDetail;}
	public void setOrderDetail(ArrayList inOrderDetail){orderDetail = inOrderDetail;}
	
	public ShoppingCart() {
		//初始化变量			
	}
	public ArrayList addCD(OrderDetails ord)
	{
		int flag = 0;     //用来标志原来购物车是否有相同的CD	
		if(this.orderDetail != null)
		{
			Iterator it = this.orderDetail.iterator();
			while(it.hasNext())
			{
				OrderDetails od = (OrderDetails)it.next();
				int oldID = od.getCdID(); 
				int newID = ord.getCdID();			
				if(newID == oldID)
				{
					int oldQty = od.getQuantity();
					int quantity = ord.getQuantity();
					int newQty = oldQty + quantity;
					od.setQuantity(newQty);
					flag = 1;
					break;
				}
			}
		}		
		if(flag == 0)
		{			
			this.orderDetail.add(ord);
		}
		//刷新订单总价
		double addPrice = ord.getPrice() * ord.getQuantity();
		this.orderObj.setTotalPrice(this.orderObj.getTotalPrice() + addPrice);
		return this.orderDetail;
	}
	public ArrayList deleteCD(int cdID)
	{
		Iterator it = this.orderDetail.iterator();
		int quantity = 0;
		double price = 0;
		while(it.hasNext())
		{
			OrderDetails od = (OrderDetails)it.next();
			int tempID = od.getCdID(); 
			if(tempID == cdID)
			{
				quantity = od.getQuantity();
				price = od.getPrice();
				this.orderDetail.remove(od);
				break;
			}
		}
		//刷新订单总价
		double deletePrice = quantity * price;
		this.orderObj.setTotalPrice(this.orderObj.getTotalPrice() - deletePrice);
		return this.orderDetail;
	}
	public void deleteAllCD()
	{
		if(this.orderDetail != null) this.orderDetail.removeAll(this.orderDetail);
		//刷新订单总价		
		this.orderObj.setTotalPrice(0);
	}
	public boolean createOrder()
	{
		//将订单记录插入到数据库
		boolean res = false;
		int orderID = this.orderObj.insertInfo();
		if(orderID == 0) res = false;
		//将订单明细插入数据库
		Iterator it = this.orderDetail.iterator();
		while(it.hasNext())
		{
			OrderDetails od = (OrderDetails)it.next();
			od.setOrderID(orderID);
			res = od.insertInfo();
			if(!res) return res;
		}
		return res;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -