当前位置: 移动技术网 > IT编程>开发语言>Java > javaweb图书商城设计之购物车模块(3)

javaweb图书商城设计之购物车模块(3)

2019年07月22日  | 移动技术网IT编程  | 我要评论
本文继续为大家分享了javaweb图书商城中购物车模块,供大家参考,具体内容如下 购物车存储 保存在session中 保存在cookie中 保存在数据库中 1

本文继续为大家分享了javaweb图书商城中购物车模块,供大家参考,具体内容如下

购物车存储

保存在session中
保存在cookie中
保存在数据库中

1、创建相关类

购物车的结构:

cartitem:购物车条目,包含图书和数量
cart:购物车,包含一个map

/**
 * 购物车类
 */
public class cart {
  private map<string,cartitem> map = new linkedhashmap<string,cartitem>();

  /**
   * 计算合计
   * @return
   */
  public double gettotal() {
    // 合计=所有条目的小计之和
    bigdecimal total = new bigdecimal("0");
    for(cartitem cartitem : map.values()) {
      bigdecimal subtotal = new bigdecimal("" + cartitem.getsubtotal());
      total = total.add(subtotal);
    }
    return total.doublevalue();
  }

  /**
   * 添加条目到车中
   * @param cartitem
   */
  public void add(cartitem cartitem) {
    if(map.containskey(cartitem.getbook().getbid())) {//判断原来车中是否存在该条目
      cartitem _cartitem = map.get(cartitem.getbook().getbid());//返回原条目
      _cartitem.setcount(_cartitem.getcount() + cartitem.getcount());//设置老条目的数量为,其自己数量+新条目的数量
      map.put(cartitem.getbook().getbid(), _cartitem);
    } else {
      map.put(cartitem.getbook().getbid(), cartitem);
    }
  }

  /**
   * 清空所有条目
   */
  public void clear() {
    map.clear();
  }

  /**
   * 删除指定条目
   * @param bid
   */
  public void delete(string bid) {
    map.remove(bid);
  }

  /**
   * 获取所有条目
   * @return
   */
  public collection<cartitem> getcartitems() {
    return map.values();
  }
}

/**
 * 购物车条目类
 * 
 */
public class cartitem {
  private book book;// 商品
  private int count;// 数量

  /**
   * 小计方法
   * @return
   * 处理了二进制运算误差问题
   */
  public double getsubtotal() {//小计方法,但它没有对应的成员!
    bigdecimal d1 = new bigdecimal(book.getprice() + "");
    bigdecimal d2 = new bigdecimal(count + "");
    return d1.multiply(d2).doublevalue();
  }

  public book getbook() {
    return book;
  }

  public void setbook(book book) {
    this.book = book;
  }

  public int getcount() {
    return count;
  }

  public void setcount(int count) {
    this.count = count;
  }
}

2、添加购物车条目

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>


<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <title>购物车列表</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="this is my page">
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
<style type="text/css">
  * {
    font-size: 11pt;
  }
  div {
    margin:20px;
    border: solid 2px gray;
    width: 150px;
    height: 150px;
    text-align: center;
  }
  li {
    margin: 10px;
  }

  #buy {
    background: url(<c:url value='/images/all.png'/>) no-repeat;
    display: inline-block;

    background-position: 0 -902px;
    margin-left: 30px;
    height: 36px;
    width: 146px;
  }
  #buy:hover {
    background: url(<c:url value='/images/all.png'/>) no-repeat;
    display: inline-block;

    background-position: 0 -938px;
    margin-left: 30px;
    height: 36px;
    width: 146px;
  }
</style>
 </head>

 <body>
<h1>购物车</h1>
<c:choose>
  <%-- 如果没有车,或车的内容集合为0长 --%>
  <c:when test="${empty sessionscope.cart or fn:length(sessionscope.cart.cartitems) eq 0}">
    <img src="<c:url value='/images/cart.png'/>" width="300"/>
  </c:when>
  <c:otherwise>
<table border="1" width="100%" cellspacing="0" background="black">
  <tr>
    <td colspan="7" align="right" style="font-size: 15pt; font-weight: 900">
      <a href="<c:url value='/cartservlet?method=clear'/>">清空购物车</a>
    </td>
  </tr>
  <tr>
    <th>图片</th>
    <th>书名</th>
    <th>作者</th>
    <th>单价</th>
    <th>数量</th>
    <th>小计</th>
    <th>操作</th>
  </tr>

<c:foreach items="${sessionscope.cart.cartitems }" var="cartitem">
  <tr>
    <td><div><img src="<c:url value='/${cartitem.book.image }'/>"/></div></td>
    <td>${cartitem.book.bname }</td>
    <td>${cartitem.book.author }</td>
    <td>${cartitem.book.price }元</td>
    <td>${cartitem.count }</td>
    <td>${cartitem.subtotal }元</td>
    <td><a href="<c:url value='/cartservlet?method=delete&bid=${cartitem.book.bid }'/>">删除</a></td>
  </tr>
</c:foreach>

  <tr>
    <td colspan="7" align="right" style="font-size: 15pt; font-weight: 900">
      合计:${sessionscope.cart.total }元
    </td>
  </tr>
  <tr>
    <td colspan="7" align="right" style="font-size: 15pt; font-weight: 900">
      <a id="buy" href="<c:url value='/orderservlet?method=add'/>"></a>
    </td>
  </tr>
</table>
  </c:otherwise>
</c:choose>
 </body>
</html>

public class cartservlet extends baseservlet {
  /**
   * 添加购物条目
   * @param request
   * @param response
   * @return
   * @throws servletexception
   * @throws ioexception
   */
  public string add(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    /*
     * 1. 得到车
     * 2. 得到条目(得到图书和数量)
     * 3. 把条目添加到车中
     */
    /*
     * 1. 得到车
     */
    cart cart = (cart)request.getsession().getattribute("cart");
    /*
     * 表单传递的只有bid和数量
     * 2. 得到条目
     *  > 得到图书和数量
     *  > 先得到图书的bid,然后我们需要通过bid查询数据库得到book
     *  > 数量表单中有
     */
    string bid = request.getparameter("bid");
    book book = new bookservice().load(bid);
    int count = integer.parseint(request.getparameter("count"));
    cartitem cartitem = new cartitem();
    cartitem.setbook(book);
    cartitem.setcount(count);

    /*
     * 3. 把条目添加到车中
     */
    cart.add(cartitem);

    return "f:/jsps/cart/list.jsp";
  }

  /**
   * 清空购物条目
   * @param request
   * @param response
   * @return
   * @throws servletexception
   * @throws ioexception
   */
  public string clear(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    /**
     * 1. 得到车
     * 2. 设置车的clear
     */
    cart cart = (cart)request.getsession().getattribute("cart");
    cart.clear();
    return "f:/jsps/cart/list.jsp";
  }

  /**
   * 删除购物条目
   * @param request
   * @param response
   * @return
   * @throws servletexception
   * @throws ioexception
   */
  public string delete(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    /*
     * 1. 得到车
     * 2. 得到要删除的bid
     */
    cart cart = (cart)request.getsession().getattribute("cart");
    string bid = request.getparameter("bid");
    cart.delete(bid);
    return "f:/jsps/cart/list.jsp";
  }
}

3、清空条目

4、删除购物车条目

5、我的购物车

top.jsp中存在一个链接:我的购物车

我的购物车直接访问/jsps/cart/list.jsp,它会显示session中车的所有条目。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网