哈哈哈,经过几天的学习我把Book模块(和User差不多但是还是化了很多时间,毕竟初学者嘛)搞定了,还学会把uuid(编号) 变成电脑自己添加了。
先说上图看看Book的样子
老规矩,先写VO,值对象(因为后面的表现层/逻辑层/DAO层都会用到所以先写) 先看设计图
BookVO
package cn.hncu.bookStore.book.vo;
import java.io.Serializable;
public class BookVO implements Serializable{
private String uuid ;
private String name;
private double inPrice;
private double salePrice;
@Override
public String toString() {
return uuid + ", 书名:" + name + ", 进价:"
+ inPrice + ", 售价:" + salePrice ;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BookVO other = (BookVO) obj;
if (uuid == null) {
if (other.uuid != null)
return false;
} else if (!uuid.equals(other.uuid))
return false;
return true;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getInPrice() {
return inPrice;
}
public void setInPrice(double inPrice) {
this.inPrice = inPrice;
}
public double getSalePrice() {
return salePrice;
}
public void setSalePrice(double salePrice) {
this.salePrice = salePrice;
}
public BookVO (){
}
}
还有一个专门为了范围查询写的BookQueryVO
package cn.hncu.bookStore.book.vo;
public class BookQueryVO extends BookVO{
private double inPrice2;
private double salePrice2;
public double getInPrice2() {
return inPrice2;
}
public void setInPrice2(double inPrice2) {
this.inPrice2 = inPrice2;
}
public double getSalePrice2() {
return salePrice2;
}
public void setSalePrice2(double salePrice2) {
this.salePrice2 = salePrice2;
}
}
写完VO 就写DAO 层
三个部分接口。工厂方法。实现类
BookDao
package cn.hncu.bookStore.book.dao.dao;
import java.util.List;
import cn.hncu.bookStore.book.vo.BookQueryVO;
import cn.hncu.bookStore.book.vo.BookVO;
public interface BookDao {
//增 删 改
public boolean create(BookVO bookvo);
public boolean delete(String uuid);
public boolean update(BookVO bookvo);
//查(单,全,范围)
public BookVO getSingle(String uuid);
public List getAll();
public List getByCondtion(BookQueryVO bqvo);
}
实现类
package cn.hncu.bookStore.book.dao.impl;
import java.util.ArrayList;
import java.util.List;
import cn.hncu.bookStore.book.dao.dao.BookDao;
import cn.hncu.bookStore.book.vo.BookQueryVO;
import cn.hncu.bookStore.book.vo.BookVO;
import cn.hncu.bookStore.utils.FileIoUtil;
public class BookImpl implements BookDao{
private final String FILE_NAME = "Book.txt";
@Override
public boolean create(BookVO bookvo) {
//先找到,然后遍历记录,再判断,若存在返回 false,不存在 新添加 再存 返回 true
List books = getAll();
for(BookVO book:books){
if(book.equals(bookvo)){
return false;
}
}
books.add(bookvo);
FileIoUtil.write2File(FILE_NAME, books);
return true;
}
@Override
public boolean delete(String uuid) {
//先找到,然后遍历记录,再判断,若存在 删除 再存 返回 true,不存在 返回 false
List books = getAll();
for(int i=0;iif(book.getUuid().equals(uuid)){
books.remove(i);
FileIoUtil.write2File(FILE_NAME, books);
return true;
}
}
return false;
}
@Override
public boolean update(BookVO bookvo) {
//先找到,然后遍历记录,再判断,若存在替换他,再存 返回 true 不存在 返回 false;
List books = getAll();
for(int i=0;iif(book.equals(bookvo)){
books.set(i, bookvo);
FileIoUtil.write2File(FILE_NAME, books);
return true;
}
}
return false;
}
@Override
public BookVO getSingle(String uuid) {
List books = getAll();
for(BookVO book:books){
if(book.getUuid().equals(uuid)){
return book;
}
}
return null;
}
@Override
public List getAll() {
return FileIoUtil.readFromFile(FILE_NAME);
}
@Override
public List getByCondtion(BookQueryVO bqvo) {
// uuid 精确匹配 name 模糊查询 inPrice salePrice 范围查询
List list = getAll();
List result = new ArrayList();
if(bqvo==null){
return list;
}
//经过上面的卫条件,能够到达这里,bqm一定是非空的,所以下面的卫条件都是 bqm!=null的情况
for(BookVO vo:list){
// uuid
if(bqvo.getUuid()!=null&&bqvo.getUuid().trim().length()>0){ // 这个查询条件存在时
if(!vo.getUuid().trim().equals(bqvo.getUuid().trim())){
continue;
}
}
//name
if(bqvo.getName()!=null&&bqvo.getName().trim().length()>0){
if(!vo.getName().trim().contains(bqvo.getName().trim())){
continue;
}
}
// inPrice
//小于小的 --- 不属于这个范围内的 筛选掉
if(bqvo.getInPrice()>0){
if(vo.getInPrice()continue;
}
}
//大于大的 --- 不属于这个范围内的 筛选掉
if(bqvo.getInPrice2()>0){
if(vo.getInPrice()>bqvo.getInPrice2()){
continue;
}
}
// salePrice
//小于小的 --- 不属于这个范围内的 筛选掉
if(bqvo.getSalePrice()>0){
if(vo.getSalePrice()continue;
}
}
//大于大的 --- 不属于这个范围内的 筛选掉
if(bqvo.getSalePrice2()>0){
if(vo.getSalePrice()>bqvo.getSalePrice2()){
continue;
}
}
result.add(vo);
}
return result;
}
}
工厂方法
package cn.hncu.bookStore.book.dao.factory;
import cn.hncu.bookStore.book.dao.dao.BookDao;
import cn.hncu.bookStore.book.dao.impl.BookImpl;
public class BookDaoFactory {
private BookDaoFactory(){
}
public static BookDao getBookDao(){
return new BookImpl();
}
}
接下来就是逻辑层了
也是三部分 接口 实现类 工厂方法
接口 ebi
package cn.hncu.bookStore.book.business.ebi;
import java.util.List;
import cn.hncu.bookStore.book.vo.BookQueryVO;
import cn.hncu.bookStore.book.vo.BookVO;
public interface BookEbi {
//增 删 改
public boolean create(BookVO bookvo);
public boolean delete(String uuid);
public boolean update(BookVO bookvo);
//查(单,全,范围)
public BookVO getSingle(String uuid);
public List getAll();
public List getByCondtion(BookQueryVO bqvo);
}
实现类EBO
package cn.hncu.bookStore.book.business.ebo;
import java.util.List;
import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.dao.dao.BookDao;
import cn.hncu.bookStore.book.dao.factory.BookDaoFactory;
import cn.hncu.bookStore.book.vo.BookQueryVO;
import cn.hncu.bookStore.book.vo.BookVO;
import cn.hncu.bookStore.common.uuid.constance.UuidModelConstance;
import cn.hncu.bookStore.common.uuid.dao.dao.UuidDao;
import cn.hncu.bookStore.common.uuid.dao.factory.UuidFactory;
public class BookEbo implements BookEbi{
//注入Dao
BookDao dao = BookDaoFactory.getBookDao();
@Override
public boolean create(BookVO bookvo) {
UuidDao uuiddao = UuidFactory.getUuidDao();
bookvo.setUuid(uuiddao.getUuid(UuidModelConstance.BOOK));
return dao.create(bookvo);
}
@Override
public boolean delete(String uuid) {
// TODO Auto-generated method stub
return dao.delete(uuid);
}
@Override
public boolean update(BookVO bookvo) {
// TODO Auto-generated method stub
return false;
}
@Override
public BookVO getSingle(String uuid) {
// TODO Auto-generated method stub
return dao.getSingle(uuid);
}
@Override
public List getAll() {
// TODO Auto-generated method stub
return dao.getAll();
}
@Override
public List getByCondtion(BookQueryVO bqvo) {
return dao.getByCondtion(bqvo);
}
}
工厂方法
package cn.hncu.bookStore.book.business.factory;
import cn.hncu.bookStore.book.business.ebi.BookEbi;
import cn.hncu.bookStore.book.business.ebo.BookEbo;
public class BookEbiFactory {
private BookEbiFactory(){
}
public static BookEbi getBookEbi(){
return new BookEbo();
}
}
接下来就是表现层了 表现层看起来代码多其实大部分都是用VE设图做的,手敲的打给就是数据初始化,页面切换,参数收集。(只贴核心部分)
添加界面
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//1.收集参数
String name = tfdName.getText();
String strinPrice = tfdInPrice.getText();
String strsalePrice = tfdSalePrice.getText();
//这里要确定用户输入了价格,再来判断是否输入格式错误。
double inPrice = 0;
if (strinPrice.trim().length() > 0) {
try {
inPrice = Double.parseDouble(strinPrice);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "进价输入错误");
e.printStackTrace();
}
}
double salePrice = 0;
if (strsalePrice.trim().length() > 0) {
try {
salePrice = Double.parseDouble(strsalePrice);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "售价输入错误");
e.printStackTrace();
}
}
//2.组织参数
BookVO bookvo = new BookVO();
bookvo.setInPrice(inPrice);
bookvo.setSalePrice(salePrice);
bookvo.setName(name);
//调用逻辑层
boolean boo = ebi.create(bookvo);
//导向结果页面
if (!boo) {
JOptionPane.showMessageDialog(null, "添加失败");
return;
}
back();
}
private void back() {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
}
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
back();
}
删除界面
数据初始化
private void myDataInitComponents() {
tfdUuid.setText(bookvo.getUuid());
tfdName.setText(bookvo.getName());
tfdInPrice.setText(String.valueOf(bookvo.getInPrice()));
tfdSalePrice.setText(String.valueOf(bookvo.getSalePrice()));
}
private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {
boolean boo=ebi.delete(bookvo.getUuid());
if(!boo){
JOptionPane.showMessageDialog(null, "删除失败");return;
}else{
back();
}
}
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
back();
}
private void back() {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
}
修改页面
private void myDataInitComponents() {
tfdUuid.setText(bookvo.getUuid());
tfdName.setText(bookvo.getName());
tfdInPrice.setText(String.valueOf(bookvo.getInPrice()));
tfdSalePrice.setText(String.valueOf(bookvo.getSalePrice()));
}
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
//1.收集参数
String uuid = tfdUuid.getText();
String name = tfdName.getText();
String strinPrice = tfdInPrice.getText();
String strsalePrice = tfdSalePrice.getText();
//这里要确定用户输入了价格,再来判断是否输入格式错误。
double inPrice = 0;
if (strinPrice.trim().length() > 0) {
try {
inPrice = Double.parseDouble(strinPrice);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "进价输入错误");
e.printStackTrace();
}
}
double salePrice = 0;
if (strsalePrice.trim().length() > 0) {
try {
salePrice = Double.parseDouble(strsalePrice);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "售价输入错误");
e.printStackTrace();
}
}
//2.组织参数
BookVO bookvo = new BookVO();
bookvo.setInPrice(inPrice);
bookvo.setSalePrice(salePrice);
bookvo.setName(name);
bookvo.setUuid(uuid);
//调用逻辑层
boolean boo = ebi.create(bookvo);
//导向结果页面
if (!boo) {
JOptionPane.showMessageDialog(null, "修改失败");
return;
}
back();
}
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
back();
}
private void back() {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
}
查询界面
private void btnQueryActionPerformed(java.awt.event.ActionEvent evt) {
//1.收集参数
String uuid = tfdUuid.getText();
String name = tfdName.getText();
String strinPrice = tfdInPrice.getText();
String strinPrice2 = tfdInPrice2.getText();
String strsalePrice = tfdSalePrice.getText();
String strsalePrice2 = tfdSalePrice2.getText();
//这里要确定用户输入了价格,再来判断是否输入格式错误。
double inPrice = 0;
double inPrice2 = Double.MAX_VALUE;
if (strinPrice.trim().length() > 0) {
try {
inPrice = Double.parseDouble(strinPrice);
inPrice2 = Double.parseDouble(strinPrice2);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "进价输入错误");
return;
}
}
double salePrice = 0;
double salePrice2 = Double.MAX_VALUE;
if (strsalePrice.trim().length() > 0) {
try {
salePrice = Double.parseDouble(strsalePrice);
salePrice2 = Double.parseDouble(strsalePrice2);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "售价输入错误");
return;
}
}
//2.组织参数
BookQueryVO bookvo = new BookQueryVO();
bookvo.setInPrice(inPrice);
bookvo.setSalePrice(salePrice);
bookvo.setName(name);
bookvo.setUuid(uuid);
bookvo.setInPrice2(inPrice2);
bookvo.setSalePrice2(salePrice2);
//调用逻辑层
List result = ebi.getByCondtion(bookvo);
//导向结果页面
mainFrame.setContentPane(new ListPanel(mainFrame, result));
mainFrame.validate();
}
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
}