Jweb 之购物车小程序简约session版

1.创源于灵感

  • showtime 思维导图

2.实践真理代码实操

帮助类:util包 DBHelpr类

package util;

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

import oracle.jdbc.driver.OracleDriver;

public class DBHelper {
	
	private static String user="scott";
	private static String upwd="tiger";
	private static String cname="oracle.jdbc.driver.OracleDriver";
	private static String url="jdbc:oracle:thin:@localhost:1521:orcl";
	static {
		try {
			Class.forName(cname);
			
		}catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	public static Connection getCon() {
		Connection con=null;
		try {
			con=DriverManager.getConnection(url,user,upwd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return con;
	}
	public static void closeDb(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null) {
				con.close();
			}
			if(ps!=null) {
				con.close();
			}
			if(rs!=null) {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	
	
	
	
	
  
}

实体类entity包 Goods ,Order 对象

Goods:

package entity;

public class Goods {
	private int id;
	private String name;
	private double price;
	private String info;
	private String pic;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public String getPic() {
		return pic;
	}
	public void setPic(String pic) {
		this.pic = pic;
	}
	public Goods(String name, double price, String info, String pic) {
		super();
		this.name = name;
		this.price = price;
		this.info = info;
		this.pic = pic;
	}
	public Goods(int id, String name, double price, String info, String pic) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.info = info;
		this.pic = pic;
	}
	
}

Order:

package entity;

public class Order {
	private  Goods good;
	
	private  int num;
	private  double prcice;
	public Goods getGood() {
		return good;
	}
	public void setGood(Goods good) {
		this.good = good;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public double getPrcice() {
		return prcice;
	}
	public void setPrcice() {
		this.prcice =good.getPrice()*num;
	}
	public Order(Goods good, int num, double prcice) {
		super();
		this.good = good;
		this.num = num;
		this.prcice = prcice;
	}
	public Order(int num, double prcice) {
		super();
		this.num = num;
		this.prcice = prcice;
	}
	public Order(double prcice) {
		super();
		this.prcice = prcice;
	}
	public Order() {
		super();
	}
	
}

方法类GoodsDao

package dao;

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

import entity.Goods;
import entity.Order;
import util.DBHelper;

public class GoodsDao {
	//查询单个商品
	public ArrayList<Goods> getAll(){
		ArrayList<Goods> slist=new ArrayList<>();
		Goods g=null;
		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()) {
				g=new Goods(rs.getInt(1), rs.getString(2), rs.getInt(3),rs.getString(4), rs.getString(5));
				slist.add(g);
			}
					
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
	
		return slist;
	}
	public Goods getById(int gid){
		Goods g=null;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("select * from goods where gid="+gid);
			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();
			// TODO: handle exception
		}
		
	
		return g;
	}
	

	
}

jsp界面index.jsp  ShopaCar.jsp   doShopaCar.jsp  

index.jsp:

<%@page import="dao.GoodsDao"%>
<%@page import="entity.Goods"%>
<%@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>
<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="doShopCar.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>
  <%
  ArrayList<Goods> slist=new GoodsDao().getAll();
  for(Goods s:slist){
	  
  %>
  <tr>
    <td><%=s.getId()%></td>
    <td><%=s.getName()%></td>
    <td><%=s.getPrice()%></td>
    <td><%=s.getInfo()%></td>
    <td>
    <img src="<%=s.getPic()%>">
    </td>
    <td>
    <button onclick="gm(<%=s.getId()%>)"  class="btn btn-success">添加到购物车</button>
    </td>
  </tr>
  <%}%>
</table>

</body>
</html>

ShopCar.jsp:

<%@page import="entity.Order"%>
<%@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>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>
<script type="text/javascript">
function sc(obj,bid) {
		
	location.href="dodel.jsp?bid="+bid;
}


function $(id) {
	return document.getElementById(id);
}

function xg(obj,id) {
	var gnumber=obj.value;

	location.href="doShopCar.jsp?bid="+id+"&gn="+gnumber;
	
}
function xg1(obj,id,a) {
	if(a=='-'){
		var gnumber=parseInt($("a").value)-1;
		
			location.href="doShopCar.jsp?bid="+id+"&gn="+gnumber;
	}else if(a=='+'){
		var gnumber=parseInt($("a").value)+1;
		location.href="doShopCar.jsp?bid="+id+"&gn="+gnumber;
    }
	

	
}





</script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<table class="table table-hover">
  <tr>
    <a href="index.jsp"> <span class="glyphicon glyphicon-home">主界面</span></a>
    
    <td>
  
       商品编号</td>
    <td>商品名称</td>
    <td>商品价格</td>
    <td>商品数量</td> 
    <td>商品总价</td>
    <td>商品图片</td>
    <td>操作</td>
  </tr>
  <%


	ArrayList<Order> slist=(ArrayList<Order>)session.getAttribute("slist");
	
	for(Order s:slist){
		

%>

  <tr>
    <td><%=s.getGood().getId()%></td>
    <td><%=s.getGood().getName()%></td>
    <td><%=s.getGood().getPrice()%></td>
    <td>
    <span> <button onclick="xg1(this,<%=s.getGood().getId()%>,'+')">+</button></span>
    
    <input id="a" style="width:20px" onblur="xg(this,<%=s.getGood().getId()%>)" type="text" value="<%=s.getNum()%>">
    <span> <button onclick="xg1(this,<%=s.getGood().getId()%>,'-')">-</button></span>
    </td>
    <td><%=s.getPrcice()%></td>
    
    <td>
    <img src="<%=s.getGood().getPic()%>">
    </td>
    <td>
   
    </td>
     <td>
       <span> <button class="btn btn-success" value="<%=s.getPrcice()%>">总金额:<%=s.getPrcice() %></button></span>
    </td>
    </tr> 
   
<%}%>
   
</table>
       <span><button class="btn btn-warning" value="<%=session.getAttribute("zje")%>">总金额:<%=session.getAttribute("zje")%></button></span>
</button></span>

</body>
</html>

doShopCar.jsp:

<%@page import="java.util.ArrayList"%>
<%@page import="dao.GoodsDao"%>
<%@page import="entity.Order"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String number=request.getParameter("gn");

String ggid=request.getParameter("ggid");

int count=1;
if(number!=null){
	count=Integer.valueOf(number);
}

	String id=request.getParameter("bid");
	int gid=Integer.valueOf(id);
	Order o=new Order();
	o.setGood(new GoodsDao().getById(gid));
	o.setNum(1);
	o.setPrcice();
	
	ArrayList<Order> olist=(ArrayList<Order>)session.getAttribute("slist");
	if(olist==null){
		olist=new ArrayList();
	}
	
	if(ggid!=null){
		int gggid=Integer.valueOf(ggid);
		for(int i=0;i<olist.size();i++){
			if(gggid==olist.get(i).getGood().getId()){
				olist.remove(i);
			}
		}
	}
	
//开关  如果选中的商品是一样的,数量发生改变

	boolean b=true;
	for(int i=0;i<olist.size();i++){
		if(gid==olist.get(i).getGood().getId()){
			if(number==null){
				olist.get(i).setNum(olist.get(i).getNum()+1);
				olist.get(i).setPrcice();
			}else{
				olist.get(i).setNum(count);
				olist.get(i).setPrcice();
			}
			b=false;
		}
	}
	
	if(b){
		olist.add(o);

	}
	double sumprice=0;
	
	for(int i=0;i<olist.size();i++){
		sumprice+=olist.get(i).getPrcice();
	}
	
	session.setAttribute("slist",olist);
	session.setAttribute("zje",sumprice);
	response.sendRedirect("ShopCar.jsp");	
	

%>
</body>
</html><%@page import="java.util.ArrayList"%>
<%@page import="dao.GoodsDao"%>
<%@page import="entity.Order"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String number=request.getParameter("gn");

String ggid=request.getParameter("ggid");

int count=1;
if(number!=null){
	count=Integer.valueOf(number);
}

	String id=request.getParameter("bid");
	int gid=Integer.valueOf(id);
	Order o=new Order();
	o.setGood(new GoodsDao().getById(gid));
	o.setNum(1);
	o.setPrcice();
	
	ArrayList<Order> olist=(ArrayList<Order>)session.getAttribute("slist");
	if(olist==null){
		olist=new ArrayList();
	}
	
	if(ggid!=null){
		int gggid=Integer.valueOf(ggid);
		for(int i=0;i<olist.size();i++){
			if(gggid==olist.get(i).getGood().getId()){
				olist.remove(i);
			}
		}
	}
	
//开关  如果选中的商品是一样的,数量发生改变

	boolean b=true;
	for(int i=0;i<olist.size();i++){
		if(gid==olist.get(i).getGood().getId()){
			if(number==null){
				olist.get(i).setNum(olist.get(i).getNum()+1);
				olist.get(i).setPrcice();
			}else{
				olist.get(i).setNum(count);
				olist.get(i).setPrcice();
			}
			b=false;
		}
	}
	
	if(b){
		olist.add(o);

	}
	double sumprice=0;
	
	for(int i=0;i<olist.size();i++){
		sumprice+=olist.get(i).getPrcice();
	}
	
	session.setAttribute("slist",olist);
	session.setAttribute("zje",sumprice);
	response.sendRedirect("ShopCar.jsp");	
	

%>
</body>
</html>

3.功能效果图:

 

4.项目体会

只要思想不滑坡,办法总比困难多!

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值