將商品添加到購物車


流程圖

封裝一個購物車對象

  1、Cart:購物車對象

    CartItem的集合(選擇Map集合,移除購物車比較方便)

    total:總計

    功能

      將購物項添加到購物車

      從購物車中移除購物項

      清空購物車

  2、CartItem:購物項對象

    Product:商品信息

    count:購買某種商品數量

    subtotal:購買某種商品的小計

 

實體類

1、購物項類

package cn.itcast.shop.cart.vo;

import cn.itcast.shop.product.vo.Product;

/**
 * 購物項對象
 * @author 郭賢達
 *
 */
public class CartItem {
    private Product product;    // 購物項中商品信息
    private int count;            // 購買某種商品數量
    private double subtotal;    // 購買某種商品小計
    public Product getProduct() {
        return product;
    }
    public void setProduct(Product product) {
        this.product = product;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    // 小計自動計算的.
    public double getSubtotal() {
        return count * product.getShop_price();
    }
    /*public void setSubtotal(double subtotal) {
        this.subtotal = subtotal;
    }
    */
    
}

2、購物車類

package cn.itcast.shop.cart.vo;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/*
 * 購物車對象
 */
public class Cart implements Serializable {
    //購物車屬性
    //購物箱集合:Map的key就是商品pid,value:購物項
    private Map<Integer,CartItem> map = new LinkedHashMap<Integer,CartItem>();
    
    //Cart對象中有一個叫CartItem屬性
    public Collection<CartItem> getCartItems(){
        return map.values();
        
    }
    //購物總計
    private double total;
    
    public double getTotal() {
        return total;
    }

    //購物車的功能
    //1.將購物項添加到購物車
    public void addCart(CartItem cartItem){
        //判斷購物車中是否已經存在該購物項
        /*
         * 如果存在,數量增加
         *         總計=總計+購物項小計
         * 如果不存在
         *         向map中添加購物項
         *         總計=總計+購物項小計
         */
        //獲得商品id
        Integer pid=cartItem.getProduct().getPid();
        //判斷購物車中是否已經存在該購物項
        if(map.containsKey(pid)){
            //存在
            CartItem _cartItem=map.get(pid);//獲得購物車中原來的購物項
            _cartItem.setCount(cartItem.getCount()+cartItem.getCount());
        }else{
            //不存在
            map.put(pid, cartItem);
        }
        //設置總計的值
        total += cartItem.getSubtotal();
    }
    //2.從購物車移除購物項
    public void removeCart(Integer pid){
        //將購物項移除購物車
        CartItem cartItem=map.remove(pid);
        //總計=總計-移除的購物項小計
        total -= cartItem.getSubtotal();
    }
    //3.清空購物車
    public void clearCart(){
        //將所有購物項清空
        map.clear();
        //將總設計設置為0
        total=0;
    }
}

jsp中提交表單

<script>
    function saveCart(){
        document.getElementById("cartForm").submit();
    }
</script>

<form id="cartForm" action="${pageContext.request.contextPath }/cart_addCart.action" method="post">
                <input type="hidden" name="pid" value="<s:property value="model.pid"/>"/>
                <div class="action">                    
                        <dl class="quantity">
                            <dt>購買數量:</dt>
                            <dd>
                                <input id="count" name="count" value="1" maxlength="4" onpaste="return false;" type="text"/>
                                <div>
                                    <span id="increase" class="increase">&nbsp;</span>
                                    <span id="decrease" class="decrease">&nbsp;</span>
                                </div>
                            </dd>
                            <dd></dd>
                        </dl>
                    <div class="buy">
                            <input id="addCart" class="addCart" value="加入購物車" type="button" onclick="saveCart()"/>
                
                    </div>
                </div>
            </form>

action

package cn.itcast.shop.cart.action;

import org.apache.struts2.ServletActionContext;

import cn.itcast.shop.cart.vo.Cart;
import cn.itcast.shop.cart.vo.CartItem;
import cn.itcast.shop.product.service.ProductService;
import cn.itcast.shop.product.vo.Product;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 購物車Action
 * 
 * @author 傳智.郭嘉
 * 
 */
public class CartAction extends ActionSupport {
    // 接收pid
    private Integer pid;
    // 接收數量count
    private Integer count;
    // 注入商品的Service
    private ProductService productService;

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    // 將購物項添加到購物車:執行的方法
    public String addCart() {
        // 封裝一個CartItem對象.
        CartItem cartItem = new CartItem();
        // 設置數量:
        cartItem.setCount(count);
        // 根據pid進行查詢商品:
        Product product = productService.findByPid(pid);
        // 設置商品:
        cartItem.setProduct(product);
        // 將購物項添加到購物車.
        // 購物車應該存在session中.
        Cart cart = getCart();
        cart.addCart(cartItem);

        return "addCart";
    }

    // 清空購物車的執行的方法:
    public String clearCart(){
        // 獲得購物車對象.
        Cart cart = getCart();
        // 調用購物車中清空方法.
        cart.clearCart();
        return "clearCart";
    }
    
    // 從購物車中移除購物項的方法:
    public String removeCart(){
        // 獲得購物車對象
        Cart cart = getCart();
        // 調用購物車中移除的方法:
        cart.removeCart(pid);
        // 返回頁面:
        return "removeCart";
    }
    
    // 我的購物車:執行的方法
    public String myCart(){
        return "myCart";
    }
    
    /**
     * 獲得購物車的方法:從session中獲得購物車.
     * @return
     */
    private Cart getCart() {
        Cart cart = (Cart) ServletActionContext.getRequest().getSession()
                .getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            ServletActionContext.getRequest().getSession()
                    .setAttribute("cart", cart);
        }
        return cart;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM