求大神指教。为什么不能算出购物车结果

package com.test;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import shopcart.dto.*;


/**
 *
 * @author MR-XIE
 */
public class shopcart extends HttpServlet {

    /**
     *
     * @throws ServletException
     */
        @Override
        public void init()
        throws ServletException {  
            Map products=new HashMap();
            products.put("001",new Product("001","相机",2000));
            products.put("002",new Product("002","手机",3999));
            products.put("003",new Product("003","冰箱",6000));
            ServletContext context = getServletContext();
            context.setAttribute("products", products);    
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       response.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       response.setContentType("text/html;charset=UTF-8");
       out.println("<html><head><title>产品显示</title></head><body>");
       out.println("<h1>产品显示</h1><p><a href=showcart>查看购物车</a><br/></p>");
       out.println(" <form method=post action=shopping>");
       out.println("<input type=hidden name=action value=purchase>");
       out.println("<table border=1>" +"<tr bgcolor=#996699>" +"<td>序号</td>" +"<td>物品名称</td>"+ "<td>产品价格(¥)</td>" +"<td>添加到购物车</td></tr>");
       Map products = (Map) getServletContext().getAttribute("products");
       Set productIdSet = products.keySet();
       Iterator it = productIdSet.iterator();
       int number = 1;
       while(it.hasNext()){
           String id = (String)it.next();
           Product product = (Product) products.get(id);
           out.println("<tr><td>"+(number++)+"</td>" +"<td>"+product.getName()+"</td>" + "<td>"+product.getPrice()+"</td>"+"<td> <input type=checkbox name=productId" + "value="+product.getId()+"></td></tr>");             
       }
           out.println("</table><p><input type=reset value=全部取消>" +"<input type=submit value=确定></p></form></body></html>");
           out.close();
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.test;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import shopcart.dto.*;

/**
 *
 * @author MR-XIE
 */
public class shopping extends HttpServlet {
    @Override
    @SuppressWarnings("empty-statement")
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
             doPost(request, response);
          }
  
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        ServletContext context = getServletContext();
        Map products = (Map) context.getAttribute("products");
        String productIds[] = request.getParameterValues("productId");
        String action = (String)request.getParameter("action");
        ShopCart shopCart = (ShopCart)request.getSession().getAttribute("shopCart");
        if(shopCart == null)
        {
            shopCart = new ShopCart();
            getServletContext().setAttribute("shopCart", shopCart);
        }
        if(action.equals("purchase")&&productIds!=null)
        {
            for (int i=0;i<productIds.length;i++){
             Product product = (Product) products.get(productIds[i]);          
             shopCart.addProductToCart(product);  
            }
        }
        else if(action.equals("delete")){
             String productId = (String)request.getParameter("productId");
             shopCart.removeProductFromCart(productId);
        }
        request.getSession().setAttribute("shopCart", shopCart);
        RequestDispatcher dispatcher =request.getRequestDispatcher("showcart");
        dispatcher.forward(request, response);
    }

    }


    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.test;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import shopcart.dto.Product;
import shopcart.dto.ShopCart;

/**
 *
 * @author MR-XIE
 */
public class showcart extends HttpServlet {

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            doPost(request, response);
        }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            response.setContentType("text/html;charset=UTF-8");
            out.print("<html><head><title>购买产品显示</title></head><body>");
            HttpSession session=request.getSession();
            ShopCart shopCart = (ShopCart)session.getAttribute("shopCart");
            out.print("<h1>您目前购买的产品为:</h1>");
            out.print("<table border=1>" +"<tr bgcolor=#996699>" +"<td>序号</td>"+"<td>产品名称</td>" +"<td>价格</td>"+"<td>操作</td></tr>");
            if(shopCart !=null)
            {
                List list = shopCart.getAllProductsFromCart();
                for(int i=0;list!=null&&i<list.size();i++)
                {
                    Product product = (Product)list.get(i);
                    out.print("<tr><td>"+i+"<td>"+product.getName() + "<td>"+product.getPrice()+"</td>"+"<td> <a href=\"shopping?productId= +product.getId()+ &action=delete\">删除</a></td></tr>"); 
            }
            }
            else
            {
                shopCart = new ShopCart();
                request.getSession().setAttribute("shopCart", shopCart);
            }
            out.print("</table>");
            out.print("<p>目前您购物车的总价格为:"+shopCart.getAllProductPrice()+"元人民币。</p>");
            out.print("<a href=shopcart>返回产品显示页</a>");
            out.close();
    }
}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package shopcart.dto;
import java.io.Serializable;
/**
 *
 * @author MR-XIE
 */
public class Product implements Serializable {
    private String id;
    private String name;
    private double price;
    Product()
    {
    }
    public Product(String id,String name,double price)
    {
        this.id=id;
        this.name=name;
        this.price=price;
    }
    public void setId(String id)
    {
        this.id=id;
    }
    public void setName(String name)
    {
      this.name=name;  
    }
    public void setPrice(double price)
    {
        this.price=price;
    }
    public String getId()
    {
        return id;
    }
    public String getName()
    {
        return name;
    }
    public double getPrice()
    {
        return price;
    }

}
    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package shopcart.dto;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author MR-XIE
 */
public class ShopCart implements Serializable  {
    public ShopCart()
    {
        
    }
    private List cart=null;
    public void addProductToCart(Product product)
    {
        if(cart==null)
            cart=new ArrayList();
        Iterator it=cart.iterator();
        while(it.hasNext())
        {
            Product item=(Product) it.next();
            if(item.getId().equals(product.getId()))
            {
                return;
            }
        }
        cart.add(product);
    }
        public void removeProductFromCart(String productId)
    {
        if (cart == null)
        {
            return;
        }
        Iterator it = cart.iterator();
        while (it.hasNext())
        {
            Product item = (Product) it.next();
            if (item.getId().equals(productId))
            {
                it.remove();
                return;
            }
        }
    }
    public double getAllProductPrice()
    {
        if(cart==null)
            return 0;
        double totalPrice=0;
        Iterator it=cart.iterator();
        while(it.hasNext())
        {
            Product item=(Product) it.next();
            totalPrice +=item.getPrice();
        }
        return totalPrice;
    }
    public List getAllProductsFromCart()
        {
            return cart;
        }
    }




    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值