?? shoppingcart.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; //用來標(biāo)志原來購物車是否有相同的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);
}
//刷新訂單總價(jià)
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;
}
}
//刷新訂單總價(jià)
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);
//刷新訂單總價(jià)
this.orderObj.setTotalPrice(0);
}
public boolean createOrder()
{
//將訂單記錄插入到數(shù)據(jù)庫
boolean res = false;
int orderID = this.orderObj.insertInfo();
if(orderID == 0) res = false;
//將訂單明細(xì)插入數(shù)據(jù)庫
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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -