求商城购物车具体代码,HTML5+CSS+php的,有没有大神具体给一下或者思路

2025-05-11 10:04:47
推荐回答(1个)
回答1:

package com.xxx.service.impl;    
     
import java.sql.SQLException;    
import java.util.ArrayList;    
import java.util.List;    
     
import com.xxx.dao.CartDAO;    
import com.xxx.dao.impl.JdbcCartDAO;    
import com.xxx.entity.CartItem;    
import com.xxx.entity.Product;    
import com.xxx.service.CartService;    
     
public class CartServiceImpl implements CartService{    
    List items = new ArrayList();    
    //增加商品    
    public boolean add(int id) throws SQLException {    
        for(CartItem item: items) {    
            if(item.getPro().getId() == id) {    
                                     //判断是否购买过    
                if(item.isBuy()) {    
                    item.setQty(item.getQty()+1);    
                    return true;    
                }    
            }    
            return false;//数据库出现错误,没有该商品    
        }    
        CartItem item = new CartItem();    
        CartDAO dao = new JdbcCartDAO();    
        Product pro = dao.findById(id);    
        item.setPro(pro);    
        item.setQty(1);    
        item.setBuy(true);    
        return true;    
    }    
    //删除商品    
    public void delete(int id) throws SQLException {    
        for(CartItem item: items) {    
            if(item.getPro().getId() == id) {    
                item.setBuy(false);    
            }    
        }    
    }    
    //恢复删除的商品    
    public void recovery(int id) throws SQLException {    
        for(CartItem item: items) {    
            if(item.getPro().getId() == id) {    
                item.setBuy(true);    
            }    
        }    
    }    
    //更新商品数量    
    public void update(int id, int pnum) throws SQLException {    
        for(CartItem item: items) {    
            if(item.getPro().getId() == id) {    
                if(item.getQty() == 0) {    
                    delete(id);    
                } else {    
                    item.setQty(pnum);    
                }    
            }    
        }    
    }    
    //获取购买过的商品    
    public List getBuyPros() throws SQLException {    
        List list = new ArrayList();    
        for(CartItem item: items) {    
            if(item.isBuy()) {    
                list.add(item);    
            }    
        }    
        if(list.size() > 0) {    
            return list;    
        }    
        return null;    
    }    
    //获取已经删除的商品(可以恢复)    
    public List getDelPros() throws SQLException {    
        List list = new ArrayList();    
        for(CartItem item: items) {    
            if(!item.isBuy()) {    
                list.add(item);    
            }    
        }    
        if(list.size() > 0) {    
            return list;    
        }    
        return null;    
    }    
    //商品消费总额    
    public double cost() throws SQLException {    
        double total = 0;    
        for(CartItem item: items) {    
            if(item.isBuy()) {    
                total += item.getQty()*item.getPro().getPrice();    
            }    
        }    
        return total;    
    }    
    //清空购物车    
    public void clear() throws Exception {    
        items.clear();    
    }    
         
}