web14(购物车一

创建数据库

用户表

create table shop_user(
userid number primary key,
username varchar2(30) not null,
upassword varchar2(30)  not null);

select * from  shop_user;
insert into shop_user values(1,'root','root123')

商品表

create table shop_goods(
userid number primary key,
uname varchar2(30) not null,
price number  default 0.0,
info  varchar2(230) default '三无产品' not null);

select * from  shop_goods;
insert into shop_goods values(1,'苹果手机',800,'外国')
insert into shop_goods values(2,'菠萝手机',500.0,'中国菠萝')
insert into shop_goods values(3,'魅族手机',600.0,'好用')
insert into shop_goods values(4,'vivo手机',400.0,'拍照清晰')
insert into shop_goods values(5,'华为手机',700.0,'中国扛把子')
insert into shop_goods values(6,'OPPO手机',300.0,'好好好')
insert into shop_goods values(7,'三星手机',200.0,'呵呵呵')

创建包和类

com.zking.util    帮助类
com.zking.pojo  实体类
com.zking.dao(放dao接口)
com.zking.dao.imp    (放dao接口实现类)
com.zking.biz (业务逻辑层放dao方法)
com.zking.biz.imp(业务逻辑层放dao实现类)
 com.zking.vo (view object 视图对象 前端用)

代码如下:

登入


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>

处理登入界面


<%
    
     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));
     
     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");
 
     }
     
%>

实体类


public class User {
    private Integer id;
    private String account;
    private String password;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(Integer id, String account, String password) {
		super();
		this.id = id;
		this.account = account;
		this.password = password;
	}
	public User() {
		super();
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", account=" + account + ", password=" + password + "]";
	}
    

接口


public interface IUserBiz {
  User login(User user);
}

实现接口

public class UserBizImpl implements IUserBiz{
      
	private IUserDao userDao=new UserDaoImpl();
	
	@Override
	public User login(User user) {
		User u = userDao.login(user);
		if(u!=null) {
			if(u.getPassword().equals(user.getPassword())) {
				return u;
			}
		}
		return null;
	}
}

访问数据接口


public interface IUserDao {
 
	User login(User user);
	
}

实现访问数据接口


public class UserDaoImpl implements IUserDao {
     
	private Connection con;
	private PreparedStatement ps;
	private ResultSet rs;
	
	@Override
    public User login(User user) {
		try {
		  con=DBHelper.getCon();
		  ps=con.prepareStatement("select * from shop_user where account=?");
		  ps.setString(1,user.getAccount());
		  rs=ps.executeQuery();
		  if(rs.next()) {
			  User u=new User();
			  u.setId(rs.getInt(1));
			  u.setAccount(rs.getString(2));
			  u.setPassword(rs.getString(3));
              return u;
		   }
		} catch (Exception e) {
          e.printStackTrace();
		}finally {
			DBHelper.close(con, ps, rs);
		}
    	return null;
    }
}

商品类


public class Goods {
	 private Integer id;
	 private String name;
	 private Integer price;
	 private String info;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getPrice() {
		return price;
	}
	public void setPrice(Integer price) {
		this.price = price;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public Goods(Integer id, String name, Integer price, String info) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.info = info;
	}
	public Goods() {
		super();
	}
	@Override
	public String toString() {
		return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", info=" + info + "]";
	}
    
}

首页


<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>
        </div>
    </div>
    <%=session.getAttribute("car")%>
   <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>

商品接口


public interface IGoodsBiz {
     List<Goods>getAll();

	 Goods getOne(Integer id);

}
 

实现商品接口


public class GoodsDaoImpl implements IGoodsDao{
	private Connection con;
	private PreparedStatement ps;
	private ResultSet rs;
	
	/**
	 * 查询全部商品
	 */
	@Override
	public List<Goods> getAll() {
		List<Goods>list=new ArrayList<Goods>();
		try {
			  con=DBHelper.getCon();
			  ps=con.prepareStatement("select * from shop_goods ");
			  rs=ps.executeQuery();
			  while(rs.next()) {
				  Goods goods=new Goods();
				  goods.setId(rs.getInt(1));
				  goods.setName(rs.getString(2));
				  goods.setPrice(rs.getInt(3));
				  goods.setInfo(rs.getString(4));
	              list.add(goods);
			   }
			  return list;
			} catch (Exception e) {
	          e.printStackTrace();
			}finally {
				DBHelper.close(con, ps, rs);
			}
		return list;
	}
 
	/**
	 * 根据id查询商品
	 */
	@Override
	public Goods getOne(Integer id) {
 
		try {
			  con=DBHelper.getCon();
			  ps=con.prepareStatement("select * from shop_goods where id=? ");
			 ps.setInt(1, id);
			  rs=ps.executeQuery();
			  if(rs.next()) {
				  Goods goods=new Goods();
				  goods.setId(rs.getInt(1));
				  goods.setName(rs.getString(2));
				  goods.setPrice(rs.getInt(3));
				  goods.setInfo(rs.getString(4));
			      return goods; 
			  }
			 
			} catch (Exception e) {
	          e.printStackTrace();
			}finally {
				DBHelper.close(con, ps, rs);
			}
		return null;
	}
}

 

 购物车添加和商品总数和价格计算

<%@page import="java.util.List"%>
<%@page import="com.zking.biz.impl.GoodsBizImpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.vo.CarItem"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
   //添加购物车的页面
   
   //拿购物车
   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.add(carItem);
   
  }
   //更新购物车
   session.setAttribute("car", car);
   //跳回首页
   response.sendRedirect("index.jsp");
%>

public class CarItem {
  
	private Integer count;//数量
	private Integer sum;//条目总价
	private Goods goods;//对应的商品
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	public Integer getSum() {
		return sum;
	}
	public void setSum(Integer sum) {
		this.sum = sum;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	public CarItem(Integer count, Integer sum, Goods goods) {
		super();
		this.count = count;
		this.sum = sum;
		this.goods = goods;
	}
	
	public CarItem() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "CarItem [count=" + count + ", sum=" + sum + ", goods=" + goods + "]";
	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值