?? 504b5973e146001c1160e657ddb92502
字號:
package hall;
/**
* Project: NWPU online shop
* JDK version used: jdk1.5.0
* Version: 1.01
* class opBasket 用來處理關(guān)于購物車內(nèi)的貨物進行添加,修改與刪除和購物車的提交
*/
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; // 顯示商品列表向量數(shù)組
private float all_price = 0; // 購物總價錢
private String orderId = ""; // 用戶訂單號
private DBWrapper myConnection = null;
private String sqlStr = "";
private String errorMsg;//出錯信息
/**
* 默認構(gòu)造函數(shù)
*/
public opBasket() throws Exception {
try {
myConnection = DBWrapper.Instance();
} catch (Exception e) {
System.out.println(e);
}
}
/**
* String getErrorMsg()
* Description :得到出錯信息
* @return Vector
*/
public String getErrorMsg() {
return errorMsg;
}
/**
* void setSession(HttpSession see)
* Description :修改頁面Session
* @param HttpSession
*/
public void setSession(HttpSession see) {
session = see;
}
/**
* Vector getPurchaselist()
* Description :得到存儲orderItem實例的Vector
* @return Vector
*/
public Vector getPurchaselist() {
return purchaselist;
}
/**
* float getAll_price()
* Description :得到購物車內(nèi)貨物總價
* @return float
*/
public float getAll_price() {
return all_price;
}
/**
* void setOrderId(String newId)
* Description :修改訂單號
* @return String
*/
public void setOrderId(String newId) {
orderId = newId;
}
/**
* String getOrderId()
* Description :得到訂單號
* @return String
*/
public String getOrderId() {
return orderId;
}
/**
* boolean addnew(String inUser, int inProItem, int inQuantity)
* Description :往購物車中添加選購的商品
* @param String 選購商品的用戶名
* @param int 貨物號
* @param int 貨物量
* * @return boolean 返回操作是否成功信息
*/
public boolean addnew(String inUser, int inProItem, int inQuantity)
throws Exception {
int prodid = inProItem;
int quantity = inQuantity;
String userName = inUser;
if (quantity < 0)
return false;
if (quantity == 0)
return true;
if (prodid == -2)
return true;
// 得到貨物的價格
sqlStr = "select price from products where producItem = " + prodid;
double proPrice = 0;
int proQuantity = 0;
try {
ResultSet rs = myConnection.runQuery(sqlStr);
if (rs.next()) {
proPrice = rs.getDouble(1);
}
rs.close();
} catch (SQLException e) {
System.out.println(e);
errorMsg = "訪問數(shù)據(jù)庫失敗!";
return false;
}
// 得到顧客的余額
sqlStr = "select account from customers where name = '" + userName + "'";
double userAccount = 0;
try {
ResultSet rs = myConnection.runQuery(sqlStr);
if (rs.next()) {
userAccount = rs.getDouble(1);
}
rs.close();
} catch (SQLException e) {
System.out.println(e);
errorMsg = "訪問數(shù)據(jù)庫失敗!";
return false;
}
purchaselist = (Vector) session.getAttribute("basket");
// 得到商品的剩余量
sqlStr = "select quantity from products where producItem = " + prodid;
try {
ResultSet rs = myConnection.runQuery(sqlStr);
if (rs.next()) {
proQuantity = rs.getInt(1);
if (quantity > proQuantity) {
errorMsg = "貨物剩余量不足";
return false;
}
}
rs.close();
} catch (SQLException e) {
System.out.println(e);
errorMsg = "訪問數(shù)據(jù)庫失敗!";
return false;
}
orderItem iList = new orderItem();
iList.setProducItem(inProItem);
iList.setQuantity(quantity);
boolean match = false; // 是否購買過該商品
if (purchaselist == null) { // 第一次購買
if (userAccount < proPrice * quantity) {
errorMsg = "你的余額不足";
return false;
} else {
purchaselist = new Vector();
purchaselist.addElement(iList);
return true;
}
}
else { // 不是第一次購買
getTotalPrice(purchaselist);
if (userAccount < (all_price + proPrice * quantity)) {
errorMsg = "你的余額不足";
return false;
} 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);
for(int i =0 ;i < purchaselist.size();i++)
System.out.println(i);
session.setAttribute("basket", purchaselist);
return true;
}
}
}
/**
* boolean modifBasket(int inProItem, int inQuantity)
* 修改已經(jīng)放進購物車的數(shù)據(jù)
* @param int 貨物號
* @param int 貨物量
* @return boolean 返回操作是否成功信息
*/
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;
}
/**
* boolean delShoper(int inProItem)
* 刪除購物車中數(shù)據(jù)
*
* @param int 貨物號
*
* @return boolean 返回操作是否成功信息
*/
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;
}
/**
* boolean getTotalPrice(Vector inVector)
* 得到購物車中貨物總價
*
* @param Vector
*
* @return boolean 返回操作是否成功信息
*/
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;
}
}
/**
* boolean payout(String inUser, String inAddress, String inCode,
* double total)
* 提交購物車,產(chǎn)生訂單,并將訂單和訂單項寫入數(shù)據(jù)庫
* @param String 用戶名
* @param String 客戶地址
* @param String 客戶郵政編碼
* @param Double 總價
* @return boolean 返回操作是否成功信息
*/
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()) {
errorMsg = "貨物剩余量不足";
return false;
}
}
} catch (Exception e) {
System.out.println(e);
errorMsg = "訪問數(shù)據(jù)庫失敗!";
return false;
}
sqlStr = "update customers set account = account - " + total
+ " where name = '" + userName + "'";
try {
myConnection.runUpdate(sqlStr);
} catch (Exception e) {
System.out.println(e);
errorMsg = "訪問數(shù)據(jù)庫失敗!";
return false;
}
String address = inAddress;
String code = inCode;
Date tempDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
//產(chǎn)生獨一無二的序列號
while (true) {
long timeInMillis = System.currentTimeMillis();
orderId = "" + timeInMillis;// 以系統(tǒng)時間產(chǎn)生位移的訂單編號
sqlStr = "select * from orders where orderId = '" + orderId + "'";
try {
ResultSet rs1 = myConnection.runQuery(sqlStr);
if (!rs1.next()) {
break;
}
} catch (Exception e) {
System.out.println(e);
errorMsg = "訪問數(shù)據(jù)庫失敗!";
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 {
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();
myConnection.runUpdate(sqlStr);
}
return true;
} catch (SQLException e) {
System.out.print(e.getMessage());
errorMsg = "訪問數(shù)據(jù)庫失敗!";
return false;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -