购物车1.0版本

购物车系统1.0实现
这是一个关于购物车1.0版本的介绍,包括jar包、主页面index.jsp、dogwc.jsp用于实现功能,spcar.jsp处理商品数量增减,确保不为负。还有deletewc.jsp提供删除功能。总价计算及后续版本规划也在说明之中。

购物车的思维导图:

 jar包  

主页面 index.jsp

<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-success">添加购物车</button>
			</td>
		</tr>
		<%} %>
	</table>

</body>

//跳转到dao类传个编号

<script type="text/javascript">
	function gm(bid) {
		location.href="dogwc.jsp?bid="+bid;
	}
</script>

用来实现功能 dogwc.jsp

<%
	//获取商品订单的数量
	String number = request.getParameter("gn");
	int count = 1;
	if(number!=null){
		count= Integer.valueOf(number);
	}
	//接收商品的编号
	int bid=Integer.valueOf(request.getParameter("bid"));
	//构造订单对象
	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){
	//把订单放到ArrayList集合中
	olist=new ArrayList<OrderItem>();
	}
	
	boolean b=true;//判断没有相同订单
	//遍历订单集合,判断是否存在相同商品订单
	for(int i=0;i<olist.size();i++){
		if(bid==olist.get(i).getGoods().getBid()){
			if(number==null){
		//修改数量:原来的数量+1
		olist.get(i).setGnumber(olist.get(i).getGnumber()+1);
		//修改总价
		olist.get(i).setSumPrice();
			}else{
				//修改数量:原来的数量+1
				olist.get(i).setGnumber(count);
				//修改总价
				olist.get(i).setSumPrice();
			}
		//表示有相同订单
		b=false;
		}
	}
	
	if(b){
		//把订单放到ArrayList集合中
		olist.add(oi);
	}
	
	
	//把集合放到session中
	double sumPrice=0;
	for(int i=0;i<olist.size();i++){
		sumPrice+=olist.get(i).getSumPrice();
	}
	session.setAttribute("olist", olist);
	session.setAttribute("sumPrice", sumPrice);
	//跳转页面
	response.sendRedirect("spcar.jsp");
%>

 spcar.jsp

//添加入购物车中,文本框中的数量不能为负  商品添加(+)或者减少商品(-) 

<script type="text/javascript">
	function xg(obj,bid) {
		var gnumber=obj.value;
		if(gnumber>0){
		location.href="dogwc.jsp?bid="+bid+"&gn="+gnumber;
		}else{
			alert("数量不能为负数");
		}
		}
	
	function js(xx,cs,bid) {
		var src=document.getElementById(xx).value;
		
		if(cs==1){
			src--;
		location.href="dogwc.jsp?bid="+bid+"&gn="+src;
		}else if(cs==2){
			src++;
			location.href="dogwc.jsp?bid="+bid+"&gn="+src;
		}
		}
	
	
</script>
<body>
	<h1 align="center">
		<a href="index.jsp"> <span class="glyphicon glyphicon-home"></span>
		</a>
	</h1>
	<table class="table table-hover">
		<tr>
			<td>商品图片</td>
			<td>商品名称</td>
			<td>商品单价</td>
			<td>商品介绍</td>
			<td>商品数量</td>
			<td>订单总价</td>
			<td><span class="glyphicon glyphicon-cog"></span></td>
		</tr>
		<%
			//获取到session中的订单集合
			ArrayList<OrderItem> olist = (ArrayList<OrderItem>) session.getAttribute("olist");
			for (int i = 0; i < olist.size(); i++) {
		%>
		<tr>
			<td><img alt="" src="<%=olist.get(i).getGoods().getBface()%>">
			</td>
			<td><%=olist.get(i).getGoods().getBname()%></td>
			<td><%=olist.get(i).getGoods().getBprice()%></td>
			<td><%=olist.get(i).getGoods().getBinfo()%></td>
			<td>
				<button class="btn"
					onclick="js(<%=olist.get(i).getGnumber()%>,1,<%=olist.get(i).getGoods().getBid()%>)">-</button>
				<input id="<%=olist.get(i).getGnumber()%>"
				style="width: 40px; text-align: center;"
				onblur="xg(this,<%=olist.get(i).getGoods().getBid()%>)" type="text"
				value="<%=olist.get(i).getGnumber()%>">
				<button class="btn"
					onclick="js(<%=olist.get(i).getGnumber()%>,2,<%=olist.get(i).getGoods().getBid()%>)">+</button>
			</td>
			<td><%=olist.get(i).getSumPrice()%></td>
			<td><a
				href="deletewc.jsp?bid=<%=olist.get(i).getGoods().getBid()%>">
					<span class="glyphicon glyphicon-trash"></span>
			</a></td>
		</tr>
		<%
			}
		%>
	</table>
	<p align="right" style="margin-right: 40px">
		<button class="btn btn-success">
			总价:<%=session.getAttribute("sumPrice")%></button>
</body>

//获取到session中的订单集合

<%
			//获取到session中的订单集合
			ArrayList<OrderItem> olist = (ArrayList<OrderItem>) session.getAttribute("olist");
			for (int i = 0; i < olist.size(); i++) {
		%>

总价:

<p align="right" style="margin-right: 40px">
		<button class="btn btn-success">
			总价:<%=session.getAttribute("sumPrice")%></button>

删除功能 deletewc.jsp

<%
	//获取商品编号
	int bid=Integer.valueOf(request.getParameter("bid"));
	//获取session中的订单集合
	ArrayList<OrderItem> olist=(ArrayList<OrderItem>)session.getAttribute("olist");
	if(olist==null){
	//把订单放到ArrayList集合中
	olist=new ArrayList<OrderItem>();
	}
	boolean b=true;
	//遍历订单集合,判断是否存在相同商品订单
	for(int i=0;i<olist.size();i++){
		if(bid==olist.get(i).getGoods().getBid()){
			olist.remove(i);
		//表示有相同订单
		b=false;
		}
		
	}
	double sumPrice=0;
	for(int i=0;i<olist.size();i++){
		sumPrice+=olist.get(i).getSumPrice();
	}
	
	//跳转页面
	session.setAttribute("sumPrice", sumPrice);
		response.sendRedirect("spcar.jsp");
%>

这购物车的版本1.0,后面还会有购物车版本2.0和购物车版本3.0

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值