购物车项目1.0

前言:在这个项目中我们将要实现购买商品页面的数据绑定、添加、修改、删除,以及将商品加入购物车。故:我们要实现购物车中临时数据的绑定、修改、删除。

注意:要给项目导入Orcacle的驱动包

购物车项目结构:

src:

1-1:帮助类 DBHelper.java

1-2:实体类(商品的属性) Goods.java

1-3:数据操作类(商品的数据维护) GoodsDao.java

WEB-INF: 

2-1:显示商品:index.jsp

2-2、在shop.jsp页面中显示所有的商品

2-3、点击加入购物车,跳转到doshop.jsp页面。在doshop.jsp页面中接收要购买的商品编号    

2-4、在doshop.jsp页面中根据商品编号查询到一个商品。

2-5、再根据这个商品创建一个orderItem对象。    

2-6、在doshop.jsp中  把上一步创建的orderItem对象放到List集合中ArrayList<OrderItem>

2-7、把List集合添加到session对象中        

2-8、跳转到shopping.jsp页面。在这个页面中遍历session对象中存储的List集合


3-1、删除商品:doshop.jsp

3- 2、点击删除超连接,把要删除的商品的编号传到dodel.jsp页面
3-3、在dodel.jsp页面中获取到要删除的商品编号。
3-4、获取session中的集合
3-5、遍历集合 找到要删除的商品
3-6、直接集合删除数据

主要代码:

(1) 在src文件夹下的com.china.util 包下创建 DBHelper.java

package com.china.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBHelper {
	private static String cname = "oracle.jdbc.driver.OracleDriver";
	private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	private static String user = "scott";
	private static String upwd = "tiger";

	//注册驱动类
	static {
		try {
			Class.forName(cname);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 	连接数据库
	 * @return
	 */
	public static Connection getCon() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, user, upwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	/**
	 * 	关闭连接
	 * @param con
	 * @param ps
	 * @param rs
	 */
	public static void closeDB(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null) {
				con.close();
			}
			if(ps!=null) {
				ps.close();
			}
			if(rs!=null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 	下一个获取下一个编号的方法
	 * @return 下一个编号
	 */
	public static int getNextId(String tableName,String col) {
		int id = 1;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			//连接数据库
			con = DBHelper.getCon();
			ps = con.prepareStatement("select max("+col+") from "+tableName);
			//执行sql语句
			rs = ps.executeQuery();
			if(rs.next()) {
				id = rs.getInt(1)+1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return id;
	}
	
}

 (2) 在src文件夹下的com.china.entity 包下创建 Goods.java

package com.china.entity;

public class Goods {
	private int bid;//商品编号
	private String bname;//商品名称
	private int bprice;//商品单价
	private String binfo;//商品简介
	private String bface;//商品路径
	
	public Goods() {
		super();
	}
	public Goods(String bname, int bprice, String binfo, String bface) {
		super();
		this.bname = bname;
		this.bprice = bprice;
		this.binfo = binfo;
		this.bface = bface;
	}
	public Goods(int bid, String bname, int bprice, String binfo, String bface) {
		super();
		this.bid = bid;
		this.bname = bname;
		this.bprice = bprice;
		this.binfo = binfo;
		this.bface = bface;
	}
	
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	public String getBname() {
		return bname;
	}
	public void setBname(String bname) {
		this.bname = bname;
	}
	public int getBprice() {
		return bprice;
	}
	public void setBprice(int bprice) {
		this.bprice = bprice;
	}
	public String getBinfo() {
		return binfo;
	}
	public void setBinfo(String binfo) {
		this.binfo = binfo;
	}
	public String getBface() {
		return bface;
	}
	public void setBface(String bface) {
		this.bface = bface;
	}
	
	@Override
	public String toString() {
		return "Goods [bid=" + bid + ", bname=" + bname + ", bprice=" + bprice + ", binfo=" + binfo + ", bface=" + bface
				+ "]";
	}

}

 (3) 在src文件夹下的com.china.entity 包下创建 OrderItem.java 

package com.china.entity;
/**
 * 	小订单实体类
 * @author zjjt
 *
 */
public class OrderItem {
	private Goods goods;//商品对象
	private int gnumber;//商品数量
	private double sumPrice;//总价格
	public OrderItem() {
		super();
	}
	public OrderItem(int gnumber, double sumPrice) {
		super();
		this.gnumber = gnumber;
		this.sumPrice = sumPrice;
	}
	public OrderItem(Goods goods, int gnumber, double sumPrice) {
		super();
		this.goods = goods;
		this.gnumber = gnumber;
		this.sumPrice = sumPrice;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	public int getGnumber() {
		return gnumber;
	}
	public void setGnumber(int gnumber) {
		this.gnumber = gnumber;
	}
	public double getSumPrice() {
		return sumPrice;
	}
	public void setSumPrice() {
		this.sumPrice = goods.getBprice()*gnumber;//单价*数量
	}
	@Override
	public String toString() {
		return "OrderItem [goods=" + goods + ", gnumber=" + gnumber + ", sumPrice=" + sumPrice + "]";
	}

}

  (4) 在src文件夹下的com.china.dao 包下创建 GoodsDao.java 

package com.china.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.china.entity.Goods;
import com.china.util.DBHelper;

public class GoodsDao {
	// 查询所有
	/**
	 * 	查询所有商品
	 * @return 返回商品对象
	 */
	public ArrayList<Goods> getAll() {
		ArrayList<Goods> glist = new ArrayList<>();
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 连接数据库
			con = DBHelper.getCon();
			ps = con.prepareStatement("select * from goods");
			rs = ps.executeQuery();
			while (rs.next()) {
				//实例化一个对象,将对象添加到集合中
				glist.add(new Goods(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return glist;
	}
	
	//根据编号查询单个
	/**
	 * 	根据编号查询单个
	 * @param bid 要查询的商品编号
	 * @return 返回要查询的商品对象,否则返回null
	 */
	public Goods getByid(int bid) {
		Goods g = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 连接数据库
			con = DBHelper.getCon();
			ps = con.prepareStatement("select * from goods where bid="+bid);
			rs = ps.executeQuery();
			if (rs.next()) {
				//实例化一个对象,将对象添加到集合中
			 g = new Goods(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeDB(con, ps, rs);
		}
		return g;
	}

}

主页面效果图:如下1-1

 主界面:index.jsp

<%@page import="com.china.entity.Goods"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.china.dao.GoodsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品页面</title>
<!-- 引用jQuery库 -->
<!-- //danger-红  info-蓝  warning-黄  -->
<script type="text/javascript" src= "js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript">
	function addGm(bid) {
		//跳转到do页面将编号传过去
		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类对象
		GoodsDao gd = new GoodsDao();
		//调用dao类查询所有方法
		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="addGm(<%=g.getBid() %>)" class="btn btn-success"> 加入购物车</button> 
			</td>
		</tr>
		<%} %>
	</table>
</body>
</html>

 doshop.jsp   ——将商品添加到购物车,将商品对象放入session中

<%@page import="com.china.dao.GoodsDao"%>
<%@page import="com.china.entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@ 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不为空 说明是 从spcar页面过来的
				//修改数量:原来的数量修改为count
				olist.get(i).setGnumber(count);
				//修改总价
				olist.get(i).setSumPrice();
			}
			//表示有相同订单
			b = false;
		}
	}
	if(b){
		//把订单放到ArrayList集合中
		olist.add(oi);
	}
	//遍历订单集合
	double sumPrice = 0;
	for(int i=0;i<olist.size();i++){
		sumPrice+=olist.get(i).getSumPrice();
	}
	//把集合放到session中
	session.setAttribute("olist", olist);
	session.setAttribute("sumPrice", sumPrice);
	
	//跳转页面
	response.sendRedirect("ShoppingCar.jsp");
%>

 商品信息页面效果图:如下1-2

  

 ShoopingCar.jsp  ——个人中心购物车页面,可查看添加商品信息

<%@page import="com.china.entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品信息确认</title>
<!-- 引用jQuery库 -->
<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 xg(obj,bid) {
		var gnumber = obj.value;	
		if(gnumber>0){
			location.href="doshop.jsp?bid="+bid+"&gn="+gnumber;
		}else{
			alert("商品数量不能为空!!!");
		}
	}
	//修改商品信息函数
    //num:商品数量   cs:参数  bid:商品编号
	function up(num,cs,bid) {
		var src = document.getElementById(num).value;
		if(cs==1){
			src--;
			if(src<0){
				src=1;
				alert("商品数量不能为0!");
			}
			//跳转到do页面将编号传过去
			location.href="doshop.jsp?bid="+bid+"&gn="+src;
		}else if(cs==2){
			src++;
			//跳转到do页面将编号传过去
			location.href="doshop.jsp?bid="+bid+"&gn="+src;
		}
	}
	
	function  sc() {
		return confirm("删除请点击确认");
	}
</script>
</head>
<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="up(<%=olist.get(i).getGoods().getBid() %>,1,<%=olist.get(i).getGoods().getBid()%>)">-</button>
				<input id="<%=olist.get(i).getGoods().getBid() %>" onblur="xg(this,<%=olist.get(i).getGoods().getBid()%>)" style="width:40px;text-align:center;" type="text" value="<%=olist.get(i).getGnumber()%>">
				<button class="btn" onclick="up(<%=olist.get(i).getGoods().getBid() %>,2,<%=olist.get(i).getGoods().getBid()%>)">+</button>
			</td>
			<td><%=olist.get(i).getSumPrice() %></td>
			<td>
				<a href="delshop.jsp?bid=<%=olist.get(i).getGoods().getBid()%>">
					<span class="glyphicon glyphicon-trash" onclick="sc()"></span>
				</a>
			</td>
			<td>
				<button class="btn btn-warning" >修改</button>
			</td>
		</tr>
		<%} %>
		</table>
		<p align="right" style="margin-right:40px">
		<button class="btn btn-success">总价:<%=session.getAttribute("sumPrice") %></button>
		</p>
</body>
</html>

删除商品前效果图(总金额为4800):1-3 

删除商品后效果图(总金额为3300):1-4 

删除商品代码展示:delshop.jsp 

<%@page import="java.util.ArrayList"%>
<%@page import="com.china.entity.OrderItem"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	//接收商品编号
	String id = request.getParameter("bid");
	int bid = Integer.valueOf(id);
	
	//获取session中的订单集合
	ArrayList<OrderItem> olist=(ArrayList<OrderItem>)session.getAttribute("olist");
	if(olist==null){
		//创建订单集合
		olist =new ArrayList<OrderItem>();
	}
	
	boolean x = true;//表示默认 没有相同的订单
	//遍历订单集合,判断是否已存在相同商品订单
	for(int i=0;i<olist.size();i++){
		if(bid==olist.get(i).getGoods().getBid()){
			//若编号相同则删除
			olist.remove(i);
			//表示有相同订单
			x = false;
		}
	}
	//遍历订单集合
	double sumPrice = 0;
	for(int i=0;i<olist.size();i++){
		sumPrice+=olist.get(i).getSumPrice();
	}
	
	//把集合放到session中
	session.setAttribute("sumPrice",sumPrice);
	//跳转页面
	response.sendRedirect("ShoppingCar.jsp");
	
%>

此购物车版本为1.0,功能还有待添加及完善。敬请期待!!!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值