?? shoppingbo.java
字號:
package bo;
import myjdbc.DatabaseOperation;
import struts.actionform.ShoppingForm;
import vo.Products;
import java.util.List;
import util.uid.UUIDGener;
import javax.servlet.http.HttpServletRequest;
import util.Util;
import vo.Users;
public class ShoppingBo {
private static ShoppingBo bo = null;
private static DatabaseOperation db = null;
private ShoppingBo() {
db = DatabaseOperation.getInstance();
}
public static ShoppingBo getInstance() {
if (bo == null) {
bo = new ShoppingBo();
}
return bo;
}
/**
* 獲得所有的商品
* @param form ManagedProductForm
*/
public List getProductList() {
return db.executeSQL("select * from products");
}
/**
* 從購物車中刪除指定id的商品
* @param form ShoppingForm
*/
public void removeProduct(ShoppingForm form) {
Products product = null;
List list = form.getProductList();
for (int i = 0; i < list.size(); i++) {
product = (Products) list.get(i);
if (product.getProductId().equals(form.getId())) {
break;
}
}
if (product != null) {
form.getCart().removeProduct(product);
}
}
/**
* 添加一個商品到購物車
* @param form ShoppingForm
*/
public void addProduct(ShoppingForm form) {
Products product = null;
List list = form.getProductList();
System.out.println("product ==> " + list.size());
System.out.println("productid==>" + form.getId());
System.out.println("quantity==>" + form.getQuantity());
for (int i = 0; i < list.size(); i++) {
product = (Products) list.get(i);
if (product.getProductId().equals(form.getId())) {
break;
}
}
System.out.println("sdf==> " + product.getProductId());
if (product != null) {
Integer quantity;
try {
quantity = new Integer(form.getQuantity());
} catch (Exception e) {
quantity = new Integer(1);
}
form.getCart().addProduct(product, quantity);
}
System.out.println("a===> " + form.getCart().size());
}
/**
* 顧客結帳
* @param form ShoppingForm
* @param request HttpServletRequest
*/
public void checkOut(ShoppingForm form, HttpServletRequest request) {
String name = (String) request.getSession().getAttribute("username");
String orderid = UUIDGener.getUUID();
String sql1 = "insert into orders values ('" + orderid + "'," +
"'" + this.getUserIdByName(name) + "'," +
"getdate()," +
"0)";
db.executeSQL(sql1);
List list = form.getCart();
for (int i = 0; i < list.size(); i++) {
CartItem item = (CartItem) list.get(i);
db.executeSQL("insert into orderitem values ('" + UUIDGener.getUUID() +
"'," +
item.getQuantity().intValue() + ",'" +
item.getProduct().getProductId() + "'," +
"'" + orderid + "')");
}
list.clear(); //情況購物車
}
/**
* 通過用戶名獲的用戶編號
* @param name String
* @return String
*/
public String getUserIdByName(String name) {
String sql = "select * from users where name = '" + name + "'";
List list = db.executeSQL(sql);
if (list.size() != 0) {
return ((Users) list.get(0)).getUserId();
}
return "";
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -