Java---设计模式---书店管理系统(单机版)④

书店管理系统正确入口

进货模块

界面:

主界面:

Java---设计模式---书店管理系统(单机版)④_第1张图片

进货明细界面:

Java---设计模式---书店管理系统(单机版)④_第2张图片

填写进货单界面:

Java---设计模式---书店管理系统(单机版)④_第3张图片

添加进货明细界面:

Java---设计模式---书店管理系统(单机版)④_第4张图片

修改明细界面:

Java---设计模式---书店管理系统(单机版)④_第5张图片

查询订单界面:

Java---设计模式---书店管理系统(单机版)④_第6张图片

查询结果界面:

Java---设计模式---书店管理系统(单机版)④_第7张图片

包(体现架构):

Java---设计模式---书店管理系统(单机版)④_第8张图片

Java---设计模式---书店管理系统(单机版)④_第9张图片

关键性代码:

值对象1(InMainModel):

package cn.hncu.booksManagementSystem.in.vo;

import java.io.Serializable;

import cn.hncu.booksManagementSystem.utils.DateUtil;

/**
 * CreateTime: 2018年4月19日 下午7:20:31	
 * @author 宋进宇  Email:[email protected]
 * Description:
 *	进货值对象
 */
public class InMainModel implements Serializable {
	private static final long serialVersionUID = 1L;
	//注入userEbi 违反设计原则了,值对象不该调用别的模块的方法,除了工具类的方法
	//private static UserEbi userEbi = UserEbiFactory.getInstanceOfUserEbi();
	
	private String id; //进货单编号 --- 主键
	private long inDate; //进货时间
	private String inUserId; //进货人员编号 --- 外键
	private String inUserName; //进货人姓名---为外键补的有个属性,提高用户体验
	
	
	public InMainModel() {
	}
	
	public String getInUserName() {
		return inUserName;
	}

	public void setInUserName(String inUserName) {
		this.inUserName = inUserName;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public long getInDate() {
		return inDate;
	}
	public void setInDate(long inDate) {
		this.inDate = inDate;
	}
	public String getInUserId() {
		return inUserId;
	}
	public void setInUserId(String inUserId) {
		this.inUserId = inUserId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.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;
		InMainModel other = (InMainModel) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return  "订单编号:"+id + " ,日期:" + DateUtil.long2String(inDate) + " ,进货人编号:"+inUserId+" , " + inUserName;
		//违反设计原则---userEbi.getUserById(inUserId).getName();
	}
	
	
}

值对象2(InDetailModel):

package cn.hncu.booksManagementSystem.in.vo;
import java.io.Serializable;
/**
 * CreateTime: 2018年4月19日 下午7:16:08	
 * @author 宋进宇  Email:[email protected]
* @Description: * 进货明细值对象 */ public class InDetailModel implements Serializable{ private static final long serialVersionUID = 1L; private String id; //进货明细编号 --- 主键 private String inId; //进货单编号 --- 外键 private String bookId; //图书编号 --- 外键 private String bookName; //图书名称---为外键添加一个属性,提高用户体验 public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } private int sumNum; //进货数量 private double sumMoney; //进货总金额 //注入bookEbi //这样是违反设计原则的//private static BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi(); @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } public InDetailModel() { } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getInId() { return inId; } public void setInId(String inId) { this.inId = inId; } public String getBookId() { return bookId; } public void setBookId(String bookId) { this.bookId = bookId; } public int getSumNum() { return sumNum; } public void setSumNum(int sumNum) { this.sumNum = sumNum; } public double getSumMoney() { return sumMoney; } public void setSumMoney(double sumMoney) { this.sumMoney = sumMoney; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; InDetailModel other = (InDetailModel) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } @Override public String toString() { //return "订单明细编号:"+id + " ,订单编号:"+inId+ " ,图书编号:"+bookId +" ,《"+bookEbi.getBookModelById(bookId).getName()+"》,共"+ sumNum + "本, " + sumMoney+"元" ; //上面这样做是违反设计原则,应该通过增加一个外键相对应的属性,来提高用户体验 return "订单明细编号:"+id +" ,图书编号:"+bookId +" ,《"+bookName+"》, "+ sumNum + "本 , 共:" + sumMoney+"元" ; } }

数据层:InMainDAO实现类(InMainDAO_FileImpl):

package cn.hncu.booksManagementSystem.in.dao.impl;

import java.util.ArrayList;
import java.util.Collection;

import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.in.dao.dao.InMainDAO;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;
import cn.hncu.booksManagementSystem.in.vo.InMainQueryModel;
import cn.hncu.booksManagementSystem.utils.FileIoUtil;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;
/**
 * CreateTime: 2018年4月20日 下午11:09:52	
 * @author 宋进宇  Email:[email protected]
 * Description:
 *	InMainDAO实现类
 */
public class InMainDAO_FileImpl implements InMainDAO {
	private final String FILE_NAME = "./data/inMain.info";
	
	@Override
	public boolean add(InMainModel inMain) {
		if (inMain==null) {
			return false;
		}
		Collection col = FileIoUtil.readFromFile(FILE_NAME);
		for (InMainModel model : col) {
			if (model.equals(inMain)) {
				return false;
			}
		}
		boolean boo = col.add(inMain);
		boolean boo2 = FileIoUtil.write2File(col, FILE_NAME);
		if (boo&&boo2) {
			GeneratorXxxID_Util.updateXxxId(XxxIdEnum.IN_MAIN);
			return true;
		}
		return false;
	}

	@Override
	public boolean detelte(String id) {
		if (id==null) {
			return false;
		}
		Collection col = FileIoUtil.readFromFile(FILE_NAME);
		for (InMainModel model : col) {
			if (model.getId().equals(id)) {
				return col.remove(model)&&FileIoUtil.write2File(col, FILE_NAME);
			}
		}
		return false;
	}

	@Override
	public boolean update(InMainModel inMain) {
		if (inMain==null) {
			return false;
		}
		Collection col = FileIoUtil.readFromFile(FILE_NAME);
		ArrayList list = (ArrayList) col;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).equals(inMain)) {
				return list.set(i, inMain)!=null&&FileIoUtil.write2File(list, FILE_NAME);
				
			}
		}
		return false;
	}

	@Override
	public InMainModel getInMainInfoById(String id) {
		if (id==null) {
			return null;
		}
		Collection col = FileIoUtil.readFromFile(FILE_NAME);
		for (InMainModel model : col) {
			if (model.getId().equals(id)) {
				return model;
			}
		}
		return null;
	}

	@Override
	public Collection getAll() {
		return FileIoUtil.readFromFile(FILE_NAME);
	}

	@Override
	public Collection getInMainsByCondition(InMainQueryModel imqm) {
		Collection col = getAll();
		//如果查询条件为null,直接返回所有记录
		if (imqm==null) {
			return col;
		}
		Collection resCol = new ArrayList();
		for (InMainModel main : col) {
			//卫条件1 查id
			//判断查询条件是否有效
			if(imqm.getId()!=null &&!"".equals(imqm.getId())) {
				//不相等跳过
				if (!main.getId().equals(imqm.getId())) {
					continue;
				}
			}
			
			//卫条件2 查时间-范围查询:小于小的,大于大的
			//判断查询条件是否有效
			if (imqm.getInDate()>0) {
				//小于最小时间跳过
				if (main.getInDate()0) {
				//大于最大时间跳过
				if (main.getInDate()>imqm.getInDate2()) {
					continue;
				}
			}
			
			//卫条件3 查进货人id
			//判断查询条件是否有效
			if (imqm.getInUserId()!=null && imqm.getInUserId().trim().length()>0) {
				//如果不相等跳过
				if (!main.getInUserId().trim().equals(imqm.getInUserId().trim())) {
					continue;
				}
			}
			//能到这了说明该记录符合条件
			resCol.add(main);
		}
		return resCol;
	}

}

数据层:InDetailDAO实现类(InDetailDAO_FileImpl):

package cn.hncu.booksManagementSystem.in.dao.impl;

import java.util.ArrayList;
import java.util.Collection;

import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.in.dao.dao.InDetailDAO;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;
import cn.hncu.booksManagementSystem.in.vo.InDetailQueryModel;
import cn.hncu.booksManagementSystem.utils.FileIoUtil;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;
/**
 * CreateTime: 2018年4月20日 下午11:10:23	
 * @author 宋进宇  Email:[email protected]
 * Description:
 *	InDetailDAO实现类
 */
public class InDetailDAO_FileImpl implements InDetailDAO{
	private final String FILE_NAME = "./data/inDetail.info";
	
	@Override
	public boolean add(InDetailModel inDetail) {
		if (inDetail==null) {
			return false;
		}
		Collection col = FileIoUtil.readFromFile(FILE_NAME);
		for (InDetailModel model : col) {
			if (model.equals(inDetail)) {
				return false;
			}
		}
		boolean boo = col.add(inDetail);
		boolean boo2 = FileIoUtil.write2File(col, FILE_NAME);
		if (boo&&boo2) {
			GeneratorXxxID_Util.updateXxxId(XxxIdEnum.IN_DETAIL);
			return true;
		}
		return false;
	}

	@Override
	public boolean delete(String id) {
		if (id==null) {
			return false;
		}
		Collection col = FileIoUtil.readFromFile(FILE_NAME);
		for (InDetailModel model : col) {
			if (model.getId().equals(id)) {
				return col.remove(model)&&FileIoUtil.write2File(col, FILE_NAME);
			}
		}
		return false;
	}

	@Override
	public boolean update(InDetailModel inDetail) {
		if (inDetail==null) {
			return false;
		}
		Collection col = FileIoUtil.readFromFile(FILE_NAME);
		ArrayList list = (ArrayList) col;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).equals(inDetail)) {
				return list.set(i, inDetail)!=null&&FileIoUtil.write2File(list, FILE_NAME);
			}
		}
		return false;
	}

	@Override
	public InDetailModel getInDetailInfoById(String id) {
		if (id==null) {
			return null;
		}
		Collection col = FileIoUtil.readFromFile(FILE_NAME);
		for (InDetailModel model : col) {
			if (model.getId().equals(id)) {
				return model;
			}
		}
		return null;
	}

	@Override
	public Collection getAll() {
		return FileIoUtil.readFromFile(FILE_NAME);
	}

	@Override
	public Collection getInDetailsByCondition(InDetailQueryModel idqm) {
		Collection col = getAll();
		//如果查询条件为null,直接返回所有记录
		if (idqm==null) {
			return col;
		}
		Collection resCol = new ArrayList();
		for (InDetailModel model : col) {
			//卫条件1 查id
			//判断查询条件是否有效
			if(idqm.getId()!=null &&!"".equals(idqm.getId())) {
				//不相等跳过
				if (!model.getId().equals(idqm.getId())) {
					continue;
				}
			}
			//卫条件2 查进货单id
			//判断查询条件是否有效
			if(idqm.getInId()!=null &&!"".equals(idqm.getInId())) {
				//不相等跳过
				if (!model.getInId().equals(idqm.getInId())) {
					continue;
				}
			}
			//卫条件3 查进图书id
			//判断查询条件是否有效
			if(idqm.getBookId()!=null &&!"".equals(idqm.getBookId())) {
				//不相等跳过
				if (!model.getBookId().equals(idqm.getBookId())) {
					continue;
				}
			}
			//卫条件4 查数量 范围查询
			//判断查询条件是否有效
			if (idqm.getSumNum()>0) {
				//小于小的跳过
				if (model.getSumNum() < idqm.getSumNum()) {
					continue;
				}
			}
			//判断查询条件是否有效
			if (idqm.getSumNum2()>0) {
				//大于大的跳过
				if (model.getSumNum() > idqm.getSumNum2()) {
					continue;
				}
			}
			//卫条件4 查金额范围查询
			//判断查询条件是否有效
			if (idqm.getSumMoney()>0) {
				//小于小的跳过
				if (model.getSumMoney() < idqm.getSumMoney()) {
					continue;
				}
			}
			//判断查询条件是否有效
			if (idqm.getSumMoney2()>0) {
				//大于大的跳过
				if (model.getSumMoney() > idqm.getSumMoney2()) {
					continue;
				}
			}
			//能到这了说明该记录符合条件
			resCol.add(model);
		}
		return resCol;
	}

}

逻辑层:InEbi接口实现类(InEbo):

package cn.hncu.booksManagementSystem.in.business.ebo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.in.business.ebi.InEbi;
import cn.hncu.booksManagementSystem.in.dao.dao.InDetailDAO;
import cn.hncu.booksManagementSystem.in.dao.dao.InMainDAO;
import cn.hncu.booksManagementSystem.in.dao.factory.InDetailDaoFactory;
import cn.hncu.booksManagementSystem.in.dao.factory.InMainDaoFactory;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;
import cn.hncu.booksManagementSystem.in.vo.InDetailQueryModel;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;
import cn.hncu.booksManagementSystem.in.vo.InMainQueryModel;
import cn.hncu.booksManagementSystem.stock.business.ebi.StockEbi;
import cn.hncu.booksManagementSystem.stock.business.factory.StockEbiFactory;
import cn.hncu.booksManagementSystem.stock.dao.dao.StockDAO;
import cn.hncu.booksManagementSystem.stock.dao.factory.StockDaoFactory;
import cn.hncu.booksManagementSystem.stock.vo.StockModel;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;

/**
 * CreateTime: 2018年4月23日 上午10:33:22	
 * @author 宋进宇  Email:[email protected]
 */
public class InEbo implements InEbi {
	//注入dao
	InMainDAO inMainDao = InMainDaoFactory.getInstanceOfInMainDAO();
	InDetailDAO inDetailDao = InDetailDaoFactory.getInstanceOfInDetail();
	StockDAO stockDao = StockDaoFactory.getStockDAO();
	//注入ebi
	BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi();
	StockEbi stockEbi = StockEbiFactory.getStockEbi();
	@Override
	public boolean add(InMainModel inMain, List details) {
		//inMain补(id,inDate)
		inMain.setId(GeneratorXxxID_Util.getXxxId(XxxIdEnum.IN_MAIN	));
		inMain.setInDate(System.currentTimeMillis());
		boolean boo = inMainDao.add(inMain);
		if (!boo) {
			return false;
		}
		//details补(in,inId,sumMoney)
		for (InDetailModel detail : details) {
			detail.setId(GeneratorXxxID_Util.getXxxId(XxxIdEnum.IN_DETAIL));
			detail.setInId(inMain.getId());
			double inPrice = bookEbi.getBookModelById(detail.getBookId()).getInPrice();
			double sumMoney = detail.getSumNum()*inPrice;
			detail.setSumMoney(sumMoney);
			boo = inDetailDao.add(detail);
			if (!boo) {
				return false;
			}
			
			//能到这里说明本次进货有效,应该更新库存
			//先查询,再判断是添加还是更新
			StockModel stock = stockEbi.getByBookId(detail.getBookId());
			if (stock!=null) {
				stock.setSumNum(stock.getSumNum()+detail.getSumNum());
				boo = stockDao.updade(stock);
				if (!boo) {
					return false;
				}
			}else {
				//能进入这里说明库存中没有该图书库存,应该添加一条
				stock = new StockModel();
				//组织参数
				stock.setId(GeneratorXxxID_Util.getXxxId(XxxIdEnum.STOCK));
				stock.setBookId(detail.getBookId());
				stock.setSumNum(detail.getSumNum());
				stock.setBookName(bookEbi.getBookModelById(detail.getBookId()).getName());
				boo = stockDao.add(stock);
				if (!boo) {
					return false;
				}
			}
		}
		return true;
	}
	@Override
	public Collection getInMainsByCondition(InMainQueryModel imqm, InDetailQueryModel idqm) {
		//查询所有符合imqm的的进货单
		Set set = new HashSet();
		Collection col = inMainDao.getInMainsByCondition(imqm);
		set.addAll(col);
		//查询所有符合idqm的进货单
		Set set2 = new HashSet();
		Collection col2 = inDetailDao.getInDetailsByCondition(idqm);
		for (InDetailModel detail : col2) {
			set2.add(inMainDao.getInMainInfoById(detail.getInId()));
		}
		//两个set的交集就是两个查询条件都符合的查询结果
		set.retainAll(set2);
		//下面是为了给集合末尾加个null,以便ui层实现点击JList空白部分能出现弹框
		//因为HashSet不能加null;
		col= new ArrayList();
		col.addAll(set);
		col.add(null);
		
		return col;
	}
}

逻辑层:InMainEbi实现类(InMainEbo)

package cn.hncu.booksManagementSystem.in.business.ebo;

import java.util.Collection;

import cn.hncu.booksManagementSystem.in.business.ebi.InMainEbi;
import cn.hncu.booksManagementSystem.in.dao.dao.InMainDAO;
import cn.hncu.booksManagementSystem.in.dao.factory.InMainDaoFactory;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;
import cn.hncu.booksManagementSystem.in.vo.InMainQueryModel;
/**
 * CreateTime: 2018年4月22日 上午9:54:17	
 * @author 宋进宇  Email:[email protected]
 * Description:
 *	InMainEbi实现类
 */
public class InMainEbo implements InMainEbi{
	//注入inMainDAO
	InMainDAO inMainDao = InMainDaoFactory.getInstanceOfInMainDAO();
	@Override
	public InMainModel[] getAll() {
		Collection col = inMainDao.getAll();
		col.add(null);
		return col.toArray(new InMainModel[0]);
	}
	@Override
	public InMainModel[] getInMainsByCondition(InMainQueryModel imqm) {
		Collection col = inMainDao.getInMainsByCondition(imqm);
		col.add(null);
		return col.toArray(new InMainModel[0]);
	}
	
	
}

逻辑层:InDetailEbi实现类(InDetailEbo)

package cn.hncu.booksManagementSystem.in.business.ebo;

import java.util.Collection;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.in.business.ebi.InDetailEbi;
import cn.hncu.booksManagementSystem.in.dao.dao.InDetailDAO;
import cn.hncu.booksManagementSystem.in.dao.factory.InDetailDaoFactory;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;
import cn.hncu.booksManagementSystem.in.vo.InDetailQueryModel;
/**
 * CreateTime: 2018年4月22日 上午9:53:56	
 * @author 宋进宇  Email:[email protected]
 * Description:
 *	InDetailEbi实现类
 */
public class InDetailEbo implements InDetailEbi {
	//注入inDetailDao
	InDetailDAO inDetailDao = InDetailDaoFactory.getInstanceOfInDetail();
	
	//注入BookEbi
	BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi();
	

	@Override
	public Collection getAll() {
		return inDetailDao.getAll();
	}


	@Override
	public Collection getInDetailsByCondition(InDetailQueryModel idqm) {
		return inDetailDao.getInDetailsByCondition(idqm);
	}

}

表现层:采用VE生产,就不详细列举了。








你可能感兴趣的:(2.Java,------④设计模式,Java,Java,设计模式)