JavaWeb.15.购物车项目二

今天要分享的还是购物车系统,相对于昨天分分享的系统又完善了一部分功能,还是一样,直接上代码吧
car.jsp:购物车页面

<%@ page import="java.util.List" %>
<%@ page import="com.zking.vo.CarItem" %><%--
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        td:nth-child(4)::before,
        small {
            content: "$";
        }

        * {
            outline: none !important;
        }

        td,
        th {
            text-align: center;
        }

        input {
            text-align: center;
        }
    </style>
</head>

<body>
<div class="jumbotron">
    <div class="container">
        <h1>欢迎光临胡阿玛购物车</h1>
        <p>尊贵的xxx</p>
        <p>
            <a href="doExit.jsp" class="btn btn-warning">退出登录</a>
        </p>
    </div>
</div>

<table class="table">
    <tr>
        <th>商品序号</th>
        <th>商品名称</th>
        <th>商品个数</th>
        <th>商品总价</th>
        <th>操作</th>
    </tr>
    <%
        int sum=0;
        List<CarItem> car = (List<CarItem>) session.getAttribute("car");
        for (CarItem carItem : car) {
            //每个购物条目都有自己的价格
            sum+= carItem.getSum();
    %>
    <tr>
        <td style="line-height: 30.5px;"><%=carItem.getGoods().getId()%>
        </td>
        <td style="line-height: 30.5px;"><%=carItem.getGoods().getName()%>
        </td>
        <td>
            <div class="input-group" style="width: 120px;margin: auto;">
                    <span class="input-group-btn">
                        <a href="doUpdCar.jsp?id=<%=carItem.getGoods().getId()%>&type=0" class="btn btn-default" type="button">-</a>
                    </span>
                <input type="number" onblur="location.href='doUpdCar.jsp?id=<%=carItem.getGoods().getId()%>&count='+this.value" value="<%=carItem.getCount()%>" class="form-control">
                <span class="input-group-btn">
                        <a href="doUpdCar.jsp?id=<%=carItem.getGoods().getId()%>&type=1" class="btn btn-default" type="button">+</a>
                    </span>
            </div>
        </td>
        <td style="line-height: 30.5px;"><%=carItem.getSum()%></td>
        <td style="line-height: 30.5px;">
            <a href="doDelCar.jsp?id=<%=carItem.getGoods().getId()%>" class="btn btn-primary">删除</a>
        </td>
    </tr>
    <%
        }
    %>
</table>

<h1 class="alert alert-info">
    当前购物车总价
    <small><%=sum%></small>
    <a href="doClear.jsp" class="btn btn-danger">点我结算</a>
</h1>
</body>
</html>

doAddcar.jsp:购物车操作页面

<%@ page import="com.zking.vo.CarItem" %>
<%@ page import="com.zking.biz.IGoodsBiz" %>
<%@ page import="com.zking.biz.impl.GoodsBizImpl" %>
<%@ page import="java.util.List" %><%--
  
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    //拿购物车
    List<CarItem> car=(List<CarItem>)session.getAttribute("car");

    IGoodsBiz goodsBiz=new GoodsBizImpl();

    //添加购物车的页面

    //1.得知道是那件商品吧
    String str = request.getParameter("id");
    int id = -1;
    if (str != null) {
        id = Integer.parseInt(str);
    }

    //2-1.判断该商品是否存在
    boolean f=true;
    for (CarItem item : car) {
        // item.getGoods().getId() 条目的商品id
        if(id==item.getGoods().getId()){
            //商品应该是已经被添加购物车了 [购物车中某个条目的商品id和你需要添加的商品id相同了]
            item.setCount(item.getCount()+1);//数量+1
            item.setSum(item.getCount()*item.getGoods().getPrice());
            f=false;
            break;
        }
    }

    //只要判断f是否发生了改变
    if(f){
        //2-2.生成一个CarItem [如果购物车没有该商品]
        CarItem carItem=new CarItem();
        //设置对应的商品数据
        carItem.setGoods(goodsBiz.getOne(id));
        //商品加车的数量
        carItem.setCount(1);
        //carItem.getCount() 商品加车的数量
        //carItem.getGoods().getPrice() 商品的单价
        //加车数量*商品单价
        carItem.setSum(carItem.getCount()*carItem.getGoods().getPrice());
        //将 购物条目[carItem] 绑定到 购物车[car] 中
        car.add(carItem);
    }

    //更新购物车
    session.setAttribute("car", car);
    //跳回首页
    response.sendRedirect("/index.jsp");
%>

doDelCar.jsp:购物车清空操作

<%@ page import="com.zking.vo.CarItem" %>
<%@ page import="java.util.List" %><%--
  
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    List<CarItem> car = (List<CarItem>) session.getAttribute("car");
    //商品的id
    Integer id = Integer.parseInt(request.getParameter("id"));
    //去删除购物车中对应的选项
    for (CarItem item : car) {
        if(item.getGoods().getId()==id){
            car.remove(item);
            break;
        }
    }
    /**
    for (int i = 0; i < car.size(); i++) {
        if(car.get(i).getGoods().getId()==id){
            car.remove(car.get(i));
            break;
        }
    }
     **/

    //更新购物车
    session.setAttribute("car", car);
    //跳回首页
    response.sendRedirect("/car.jsp");
%>

doExit.jsp:推出登陆操作

<%--
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    session.invalidate();//让session失效
    response.sendRedirect("/login.jsp");
%>

doLogin.jsp:登陆页面

<%@ page import="com.zking.biz.IUserBiz" %>
<%@ page import="com.zking.biz.impl.UserBizImpl" %>
<%@ page import="com.zking.pojo.User" %>
<%@ page import="java.util.List" %>
<%@ page import="com.zking.vo.CarItem" %>
<%@ page import="java.util.ArrayList" %><%--
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    String account = request.getParameter("account");
    String password = request.getParameter("password");

    IUserBiz userBiz = new UserBizImpl();
    User user = userBiz.login(new User(0, account, password));

    System.out.println(user + "---jsp");

    if (user == null) {
        response.sendRedirect("/login.jsp");
    } else {
        //首页需要登录数据
        session.setAttribute("user", user);
        //找来一个购物车
        List<CarItem> car = new ArrayList<>();
        //放到session中(把购物车给我)
        session.setAttribute("car", car);
        response.sendRedirect("/index.jsp");
    }

%>

doUpdCar.jsp:购物车操作

<%@ page import="com.zking.vo.CarItem" %>
<%@ page import="java.util.List" %><%----%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    List<CarItem> car = (List<CarItem>) session.getAttribute("car");
    //商品的id
    Integer id = Integer.parseInt(request.getParameter("id"));
    //类型
    String type = request.getParameter("type");

    //查询对应的条目
    CarItem i=null;
    for (CarItem item : car) {
        if (item.getGoods().getId() == id) {
            i=item;
            break;
        }
    }

    if(type!=null){
        //去修改购物车中对应的选项
        i.setCount(i.getCount() + (type.equals("0") ? -1 : 1));
    }
    else{
        i.setCount((int)Double.parseDouble(request.getParameter("count").equals("")?"1":request.getParameter("count")));
    }
    i.setCount(i.getCount() > 0 ? i.getCount() : 1);
    i.setSum(i.getCount() * i.getGoods().getPrice());

    //更新购物车
    session.setAttribute("car", car);
    //跳回首页
    response.sendRedirect("/car.jsp");
%>

index.jsp:主页

%@ page import="com.zking.pojo.User" %>
<%@ page import="com.zking.biz.IGoodsBiz" %>
<%@ page import="com.zking.biz.impl.GoodsBizImpl" %>
<%@ page import="com.zking.pojo.Goods" %><%--
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
  <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
  <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
  <style>

    td:nth-child(3)::before{
      content: "$";
    }

  </style>
</head>

<body>
<%
  Object obj = session.getAttribute("user");
  if(obj==null){
    response.sendRedirect("/login.jsp");
    return;
  }
%>
<div class="jumbotron">
  <div class="container">
    <h1>欢迎光临胡阿玛SuperMarket</h1>
    <p>尊贵的<%=((User)obj).getAccount()%></p>
    <p>
      <a href="car.jsp" class="btn btn-primary"></a></p>
  </div>
</div>

<div class="container">
  <table class="table">
    <tr>
      <th>商品序号</th>
      <th>商品名称</th>
      <th>商品单价</th>
      <th>商品描述</th>
      <th>操作</th>
    </tr>
    <%
      IGoodsBiz goodsBiz=new GoodsBizImpl();
      for (Goods goods : goodsBiz.getAll()) {
    %>
    <tr>
      <td><%=goods.getId()%></td>
      <td><%=goods.getName()%></td>
      <td><%=goods.getPrice()%></td>
      <td><%=goods.getInfo()%></td>
      <td>
        <div class="btn-group btn-group-xs">
          <a href="doAddCar.jsp?id=<%=goods.getId()%>" class="btn btn-primary">添加购物车</a>
        </div>
      </td>
    </tr>
    <%}%>
  </table>
</div>
</body></html>

login.jsp:登陆操作


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }

        html,
        body {
            background: #1abe9c;
        }

        form {
            width: 300px;
            background: #ebeff2;
            box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);
            border-radius: 5px;
            padding: 20px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }

        .btn-group {
            width: 100%;
        }

        .btn-group button {
            width: 50%;
        }
    </style>
</head>

<body>
<form action="doLogin.jsp" method="post">
    <h3 class="text-center" style="text-shadow: 2px 2px 1px #ed3f3f;">欢迎光临胡阿玛超市</h3>
    <div class="form-group">
        <input name="account" type="text" class="form-control" placeholder="请输入您的用户名">
    </div>
    <div class="form-group">
        <input name="password" type="password" class="form-control" placeholder="请输入您的密码">
    </div>
    <div class="btn-group">
        <button type="submit" class="btn btn-primary">登录</button>
        <button type="button" class="btn btn-danger">没有账号?</button>
    </div>
</form>
</body></html>

你可能感兴趣的:(web)