?? 50f7b2c91c46001c194efe7b2844a31e
字號:
package hall;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import javax.servlet.http.*;
public class opBasket {
private HttpSession session; //頁面的session;
private Vector purchaselist; //顯示商品列表向量數組
private float all_price = 0; //購物總價錢
private String orderId = ""; //用戶訂單號
private DBWrapper myConnection = null;
private String sqlStr = "";
public opBasket() throws Exception{
try{
myConnection = DBWrapper.Instance();
}catch(Exception e){
System.out.println(e);
}
}
public void setSession(HttpSession see){
session = see;
}
public Vector getPurchaselist() {
return purchaselist;
}
public float getAll_price(){
return all_price;
}
public void setOrderId(String newId) {
orderId = newId;
}
public String getOrderId() {
return orderId;
}
/**
* 往購物車中添加選購的商品
*/
public boolean addnew(int inProItem, int inQuantitr) throws Exception {
int prodid = inProItem;
int quantity = inQuantitr;
if (quantity < 0)
return false;
purchaselist = (Vector) session.getAttribute("basket");
if(quantity==0)return true;
if(prodid==-2)return true;
sqlStr = "select quantity from products where producItem = " + prodid;
try {
ResultSet rs = myConnection.runQuery(sqlStr);
if (rs.next()) {
if (quantity > rs.getInt(1)) {
return false;
}
}
rs.close();
} catch (SQLException e) {
System.out.println(e);
return false;
}
orderItem iList = new orderItem();
iList.setProducItem(inProItem);
iList.setQuantity(quantity);
boolean match = false; //是否購買過該商品
if (purchaselist == null) { //第一次購買
purchaselist = new Vector();
purchaselist.addElement(iList);
}
else { // 不是第一次購買
for (int i = 0; i < purchaselist.size(); i++) {
orderItem itList = (orderItem) purchaselist.elementAt(i);
if (iList.getProItem() == itList.getProItem()) {
itList.setQuantity(itList.getQuantity() + iList.getQuantity());
purchaselist.setElementAt(itList, i);
match = true;
break;
}
}
if (!match)
purchaselist.addElement(iList);
}
session.setAttribute("basket", purchaselist);
for(int i = 0; i < purchaselist.size(); i++){
orderItem temp = new orderItem();
temp = (orderItem)purchaselist.elementAt(i);
System.out.println("***************");
System.out.println(temp.getQuantity());
}
return true;
}
/**
* 修改已經放進購物車的數據
* @param newrequest
* @return
*/
public boolean modifBasket(int inProItem, int inQuantity) throws Exception {
int prodid = inProItem;
int quantity = inQuantity;
if (quantity < 1)
return false;
purchaselist = (Vector) session.getAttribute("basket");
if (purchaselist == null) {
return false;
}
sqlStr = "select quantity from products where producItem = " + prodid;
try {
ResultSet rs = myConnection.runQuery(sqlStr);
if (rs.next()) {
if (quantity > rs.getInt(1)) {
return false;
}
}
rs.close();
} catch (SQLException e) {
return false;
}
for (int i = 0; i < purchaselist.size(); i++) {
orderItem itList = (orderItem) purchaselist.elementAt(i);
if (prodid == itList.getProItem()) {
itList.setQuantity(quantity);
purchaselist.setElementAt(itList, i);
break;
}
}
return true;
}
/**
*刪除購物車中數據
* @param newrequest
* @return
*/
public boolean delShoper(int inProItem) {
int prodid = inProItem;
purchaselist = (Vector) session.getAttribute("basket");
if (purchaselist == null) {
return false;
}
for (int i = 0; i < purchaselist.size(); i++) {
orderItem itList = (orderItem) purchaselist.elementAt(i);
if (prodid == itList.getProItem()) {
purchaselist.removeElementAt(i);
break;
}
}
return true;
}
/*
* 得到購物車中貨物總價
*/
public boolean getTotalPrice(Vector inVector) throws Exception{
try{
float tempAmount = 0;
for (int i = 0; i < inVector.size(); i++) {
orderItem iList = (orderItem) purchaselist.elementAt(i);
sqlStr = "select price from products where producItem = ";
sqlStr = sqlStr + iList.getProItem() ;
ResultSet rs = myConnection.runQuery(sqlStr);
rs.next();
tempAmount = rs.getFloat(1);
all_price += tempAmount * iList.getQuantity();
}
return true;
}catch(Exception e){
System.out.println(e);
return false;
}
}
/**
* 提交購物車
*/
public boolean payout(String inUser,String inAddress,String inCode,double total)
throws Exception {
String userName = inUser;
purchaselist = (Vector) session.getAttribute("basket");
if (purchaselist == null || purchaselist.size() < 0) {
return false;
}
try{
for (int i = 0; i < purchaselist.size(); i++) {
orderItem iList = (orderItem) purchaselist.elementAt(i);
sqlStr = "select quantity from products where producItem = "
+ iList.getProItem();
ResultSet rs = myConnection.runQuery(sqlStr);
System.out.println(sqlStr);
rs.next();
if(rs.getInt(1) < iList.getQuantity()){
return false;
}
}
}catch(Exception e){
System.out.println(e);
}
sqlStr = "update customers set account = account - "
+ total + " where name = '" + userName +"'";
try{
myConnection.runUpdate(sqlStr);
}catch(Exception e){
System.out.println(e);
return false;
}
String address = inAddress;
String code = inCode;
Date tempDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
while(true){
long timeInMillis = System.currentTimeMillis();
orderId = "" + timeInMillis;//以系統時間產生位移的訂單編號
sqlStr = "select * from orders where orderId = '" + orderId + "'";
try{
System.out.println(sqlStr);
ResultSet rs1 = myConnection.runQuery(sqlStr);
if(!rs1.next()){
break;
}
}catch(Exception e){
System.out.println(e);
return false;
}
}
sqlStr = "insert into [orders]([orderId],[customerName]," +
"[address],[code],[orderTime],[amount],[state]) values ('";
sqlStr = sqlStr + orderId + "','";
sqlStr = sqlStr + userName + "','";
sqlStr = sqlStr + address + "','";
sqlStr = sqlStr + code + "','";
sqlStr = sqlStr + sdf.format(tempDate) + "'," ;
sqlStr = sqlStr + total + "," ;
sqlStr = sqlStr + 1 + " )";
try {
System.out.println(sqlStr);
myConnection.runUpdate(sqlStr);
for (int i = 0; i < purchaselist.size(); i++) {
orderItem iList = (orderItem) purchaselist.elementAt(i);
sqlStr = "insert into orderItem (orderId,producItem,quantity) values ('";
sqlStr = sqlStr + orderId + "',";
sqlStr = sqlStr + iList.getProItem() + ",";
sqlStr = sqlStr + iList.getQuantity() + ")";
System.out.println(sqlStr);
myConnection.runUpdate(sqlStr);
sqlStr = "update products set quantity = quantity - "
+ iList.getQuantity() + " where producItem = "
+ iList.getProItem();
System.out.println(sqlStr);
myConnection.runUpdate(sqlStr);
}
return true;
} catch (SQLException e) {
System.out.print(e.getMessage());
return false;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -