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

📄 shoppingbo.java

📁 bs_网上购物系统每个例子文件夹都附有数据库表、程序源文件和一个war包(或者jar包)。如果是cs结构的
💻 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 + -