用session实现购物车(含设计分析及源代码)part2

现在来编写domain层、dao层、util层、service层

domain层编写

建立一个名为Product的类,属性对应的是数据库里面的数据

在建议一个名为CarProduct的类,对应的是保存用户购买的产品

如下:


Product类源码:

package cn.zc.domain;
/*与数据库对应的商品类*/
public class Product {
	private Integer id;//存储商品id
	private String name;//存储商品名称
	private Double price;//存储商品价格
	private Integer memory;//存储商品存货量
	private String summary;//存储商品简介
	
	public Product() {
		super();
	}

	public Product(Integer id, String name, Double price, Integer memory,
			String summary) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.memory = memory;
		this.summary = summary;
	}

	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 Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Integer getMemory() {
		return memory;
	}

	public void setMemory(Integer memory) {
		this.memory = memory;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}
	
	
}

CarProduct类源码:

package cn.zc.domain;
/*已经添加到购物车中商品类*/
public class CarProduct {

	private String name;//存储商品名称
	private Double price;//存储商品价格
	private Integer buyNum;//存储商品购买的数量
	public  CarProduct(Product entity,int buyNum){
		this.setName(entity.getName());
		this.setPrice(entity.getPrice());
		this.setBuyNum(buyNum);
	}

	public Integer getBuyNum() {
		return buyNum;
	}

	public void setBuyNum(Integer buyNum) {
		this.buyNum = buyNum;
	}

	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;
	}
}

util层的编写:

编写JdbcUtil类获取连接对象

如下结构:


JdbcUtil源码:

package cn.zc.util;

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

public class JdbcUtil {
	private static Connection con;// 连接对象
	/*获取连接对象的方法*/
	public static Connection getConnection(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			/*getConnection()方法的三个参数是:1、数据库的名称2、数据库管理员名称3、管理员密码*/
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/goods","root","zc");
		}catch(Exception e){
			e.printStackTrace();
		}
		return con;
	}
	/*关闭对象释放资源方法*/
	public static void release(ResultSet rs,PreparedStatement ps,Connection con){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


dao层的编写:

编写名为ProductDao的接口和实现接口的ProductDaoImpl实现类

结构:


ProductDao源码:

package cn.zc.dao;

import java.util.List;

import cn.zc.domain.Product;

public interface ProductDao {
	abstract public List<Product> findAll();//查询所有商品信息
	abstract public Product findById(Integer id);//查询指定id的商品
}
ProductDaoImpl实现类源码:

package cn.zc.dao;

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

import cn.zc.domain.Product;
import cn.zc.util.JdbcUtil;

public class ProductDaoImpl implements ProductDao {
	private  Connection con;//连接对象
	private PreparedStatement ps; //预处理对象
	private ResultSet rs;//结果集对象
	@Override
	public List<Product> findAll() {
		/*第一步:定义返回值变量*/
		List <Product>entities=new ArrayList<Product>();
		/*第二步:获取连接对象*/
		con=JdbcUtil.getConnection();
		/*第三步:定义sql语句*/
		String sql="select id,name,price,memory,summary from product";
		/*第四步:根据sql语句生成预处理对象*/
		try {
			ps=con.prepareStatement(sql);
			/*第五步:为占位符赋值*/
			/* 第六步:提交sql语句 */
			rs = ps.executeQuery();
			/*第七步:判断*/
			while(rs.next()){
				Product entity=new Product();
				entity.setId(rs.getInt("id"));
				entity.setName(rs.getString("name"));
				entity.setPrice(rs.getDouble("price"));
				entity.setMemory(rs.getInt("memory"));
				entity.setSummary(rs.getString("summary"));
				entities.add(entity);
			}
			/*第八步:释放资源*/
			JdbcUtil.release(rs, ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entities;
	}
	@Override
	public Product findById(Integer id) {
		/*第一步:声明返回值变量*/
		Product entity=new Product();
		/*第二步:获取连接对象*/
		con=JdbcUtil.getConnection();
		/*第三步:定义sql语句*/
		String sql="select id,name,price,memory,summary from product where id=?";
		/*第四步:根据sql语句生成预处理对象*/
		try {
			ps=con.prepareStatement(sql);
			/*第五步:为占位符赋值*/
			int index=1;
			ps.setInt(index++, id);
			/*第六步:提交sql语句*/
			rs=ps.executeQuery();
			/*第七步:判断*/
			if(rs.next()){
				entity.setId(rs.getInt("id"));
				entity.setName(rs.getString("name"));
				entity.setPrice(rs.getDouble("price"));
				entity.setMemory(rs.getInt("memory"));
				entity.setSummary(rs.getString("summary"));
			}
			/*第八步:释放资源*/
			JdbcUtil.release(rs, ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entity;
	}
}

service层的编写
编写ProductService接口和实现类ProductServiceImpl

结构:


ProductService借口继承ProductDao即可。

ProductService源码:

package cn.zc.service;

import cn.zc.dao.ProductDao;

public interface ProductService extends ProductDao {

}
ProductServiceImpl类实现ProductService借口调用ProductDaoImpl中的方法即可。

ProductServiceImpl类源码:

package cn.zc.service;

import java.util.List;

import cn.zc.dao.ProductDaoImpl;
import cn.zc.domain.Product;

public class ProductServiceImpl implements ProductService {
	private ProductDaoImpl pdi=new ProductDaoImpl();//定义私有属性ProductDaoImpl类的实体,用来调用里面的方法
	@Override
	public List<Product> findAll() {
		// TODO Auto-generated method stub
		return pdi.findAll();
	}
	@Override
	public Product findById(Integer id) {
		// TODO Auto-generated method stub
		return pdi.findById(id);
	}

}



收藏夹和购物车系统的实现  收藏夹子系统 (1) 【收藏指定图书】能收藏一本图书,并记录收藏日期(某年某月某日,如2016-12-12。所有日期都采用人工定义方式输入,不取机器日期)。图书的信息包括图书号(是唯一的)、书名、作者、出版社、出版日期、价格。 (2) 【查询指定图书】能按照图书号查询显示收藏夹中图书的相关信息(也可以扩展功能为按照书名、作者、出版社、出版日期、指定价格大小范围查询显示收藏夹中图书的相关信息)。 (3) 【按日期显示所有图书】能按照收藏日期的先后显示输出所收藏的所有图书的相关信息。 (4) 【移出收藏夹】可以把不想收藏的某一本指定图书号的图书直接移出收藏夹。 (5) 【加入购物车】将收藏夹中的某一本指定图书号的图书加入到购物车。 (6) 【按价格显示所有图书】能按照价格的大小显示输出收藏夹中的所有图书的相关信息。  购物车子系统 (1) 【直接加入购物车】把准备购买的一本图书直接加入购物车,同时记录加入购物车的日期。图书的信息包括图书号(是唯一的)、书名、作者、出版社、出版日期、价格、购买数量、购买金额(自动计算)。 (2) 【查询指定图书】能按照图书号查询显示购物车中准备购买的图书的相关信息(也可以扩展功能为按照书名、作者、出版社、出版日期、指定价格大小范围查询显示购物车图书的相关信息)。 (3) 【修改购买数量】可以修改购物车中准备购买的某一本指定图书号的图书的数量,同时自动计算修改购买金额(购买金额=购买数量*价格)。 (4) 【删除指定图书并移到收藏夹】把购物车中的某一本指定图书号的图书删除并移到收藏夹。 (5) 【直接删除指定图书】可以把不想购买的某一本指定图书号的图书购物车中删除。 (6) 【按图书号显示所有图书】把购物车中所有图书按照图书号列出每一本图书图书号、书名、作者、出版社、出版日期、价格、购买数量、购买金额,最后列出总共有多少本图书、总金额是多少。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值