购物车思维导图:
购物车首页:
订单页面:
index:
<%@page import="zzh.com.entity.Goods"%>
<%@page import="java.util.ArrayList"%>
<%@page import="zzh.com.dao.GoodsDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript">
function gm(bid) {
//跳转到订单页面
location.href="dogwc.jsp?bid="+bid;
}
</script>
</head>
<body>
<table class="table table-hover">
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品单价</td>
<td>商品介绍</td>
<td>商品图片</td>
<td>操作</td>
</tr>
<%
GoodsDao gd = new GoodsDao();
ArrayList<Goods> glist = gd.getAll();
for(Goods g:glist){
%>
<tr>
<td><%=g.getBid() %></td>
<td><%=g.getBname() %></td>
<td><%=g.getBprice() %></td>
<td><%=g.getBinfo() %></td>
<td>
<img alt="" src="<%=g.getBface() %>">
</td>
<td>
<button onclick="gm(<%=g.getBid() %>)" class="btn btn-info">添加购物车</button>
</td>
</tr>
<%} %>
</table>
</body>
</html>
订单删除:
<%@page import="zzh.com.entity.OrderItem"%>
<%@page import="zzh.com.dao.GoodsDao"%>
<%@page import="zzh.com.entity.Goods"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
int bid = Integer.valueOf(request.getParameter("bid"));
ArrayList<OrderItem> olist = (ArrayList<OrderItem>)session.getAttribute("olist");
for(int i = 0;i<olist.size();i++){
if(olist.get(i).getGoods().getBid()==bid){
olist.remove(i);
}
}
double sumPrice = 0;
for(int i = 0;i<olist.size();i++){
sumPrice = olist.get(i).getSumPrice();
}
session.setAttribute("sumPrice", sumPrice);
response.sendRedirect("gwc.jsp");
%>
订单数量和总数的计算:
<%@page import="java.util.ArrayList"%>
<%@page import="zzh.com.dao.GoodsDao"%>
<%@page import="zzh.com.entity.OrderItem"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//获取商品订单的数量
String number = request.getParameter("gn");
int count = 1;
if(number!=null){
count = Integer.valueOf(number);
}
//接收编号
String id = request.getParameter("bid");
int bid = Integer.valueOf(id);
//构造订单对象
OrderItem oi = new OrderItem();
//给属性赋值
oi.setGoods(new GoodsDao().getById(bid));
//商品数量
oi.setGnumber(1);
//订单总价
oi.setSumPrice();
//获取session中的订单集合
ArrayList<OrderItem> olist = (ArrayList<OrderItem>)session.getAttribute("olist");
if(olist==null){
//建立订单集合
olist = new ArrayList<OrderItem>();
}
boolean b = true;
//遍历订单集合
for(int i = 0 ;i<olist.size();i++){
if(bid==olist.get(i).getGoods().getBid()){
//number为空 说明是从index页面过来的
if(number==null){
//修改数量,+1
olist.get(i).setGnumber(olist.get(i).getGnumber()+1);
//修改总价
olist.get(i).setSumPrice();
}else{//number不为空 说明是从gwc页面过来的
//修改数量,原来的数量改为count
olist.get(i).setGnumber(count);
//修改总价
olist.get(i).setSumPrice();
}
b = false;
}
}
if(b){
//订单放ArrayList集合
olist.add(oi);
}
//遍历订单集合
double sumPrice = 0;
//集合放到session
session.setAttribute("sumPrice", sumPrice);
session.setAttribute("olist", olist);
//跳转页面
response.sendRedirect("gwc.jsp");
%>
数量加减功能:
<script type="text/javascript">
function xg(obj,bid) {
var gnumber = obj.value;
location.href="dogwc.jsp?bid="+bid+ "&gn="+gnumber;
}
function jia(bid) {
location.href="dogwc.jsp?bid="+bid;
}
function jian(bid,obj) {
obj--;
if(obj==0){
obj=1;
}
location.href="dogwc.jsp?bid="+bid+"&gn="+bid;
}
</script>