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

书店管理系统正确入口

库存模块:

界面:

主界面:

查询以及查询结果界面:

包(体现架构):

关键性代码:

值对象(StockModel):

package cn.hncu.booksManagementSystem.stock.vo;

import java.io.Serializable;

/**
 * CreateTime: 2018年4月19日 下午7:26:31	
 * @author 宋进宇  Email:[email protected]
 * Description:
 *	库存值对象
 */
public class StockModel implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private String id; //库存编号 --- 主键
	private String bookId; //图书编号 ---外键
	private String bookName; //专为图书编号补的一个属性,面向用户
	
	private int sumNum; //库存数量
	public StockModel() {
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	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 String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	@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;
		StockModel other = (StockModel) 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+"图书编号:"+bookId+" ,《"+bookName+"》,"+sumNum+"本";
	}
}

数据层:StockDAO实现类(StockDAO_FileImpl):

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

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

import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.stock.dao.dao.StockDAO;
import cn.hncu.booksManagementSystem.stock.vo.StockModel;
import cn.hncu.booksManagementSystem.stock.vo.StockQueryModel;
import cn.hncu.booksManagementSystem.utils.FileIoUtil;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;
/**
 * CreateTime: 2018年4月19日 下午9:43:41	
 * @author 宋进宇  Email:[email protected]
 * Description:
 *	StockDAO的实现类,通过文件存储
 */
public class StockDAO_FileImpl implements StockDAO{
	//数据源文件名
	private final String FILE_NAME = "./data/stock.info";
	
	@Override
	public boolean add(StockModel stock) {
		//如果stock为null,则返回false
		if (stock==null) {
			return false;
		}
		//读取所有数据
		Collection<StockModel> col = FileIoUtil.readFromFile(FILE_NAME);
		//查找stock是不是新的数据
		for (StockModel model : col) {
			//如果相同则返回false
			if (model.equals(stock)) {
				return false;
			}
		}
		//能到这里说明,是新数据
		boolean boo = col.add(stock);
		//产生下一条数据的id
		GeneratorXxxID_Util.updateXxxId(XxxIdEnum.STOCK);
		return boo && FileIoUtil.write2File(col, FILE_NAME);
	}

	@Override
	public boolean updade(StockModel newStock) {
		//如果newStock为null,则返回false
		if (newStock == null) {
			return false;
		}
		//读取所有数据
		Collection<StockModel> col = FileIoUtil.readFromFile(FILE_NAME);
		List<StockModel> list = (List<StockModel>) col;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).equals(newStock)) {
				return list.set(i, newStock) != null && FileIoUtil.write2File(list, FILE_NAME);
			}
		}
		return false;
	}

	@Override
	public StockModel getStockInfoById(String id) {
		//如果id为null,则返回false
		if (id==null) {
			return null;
		}
		//读取所有数据
		Collection<StockModel> col = FileIoUtil.readFromFile(FILE_NAME);
		for (StockModel stock : col) {
			if (stock.getId().equals(id)) {
				return stock;
			}
		}
		return null;
	}

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

	@Override
	public Collection<StockModel> getStocksInfoByCondition(StockQueryModel sqm) {
		Collection<StockModel> col = getAll();
		if (sqm==null) {
			return col;
		}
		Collection<StockModel> resCol = new ArrayList<StockModel>();
		for (StockModel stock : col) {
			//卫条件1 查id
			//判断条件是否有效
			if (sqm.getId()!=null&&sqm.getId().trim().length()>0) {
				//如果不相等,跳过
				if(!stock.getId().trim().equals(sqm.getId().trim())) {
					continue ;
				}
			}
			
			//卫条件2 查bookId
			//判断条件是否有效
			if (sqm.getBookId()!=null&&sqm.getBookId().trim().length()>0) {
				//如果不相等,跳过
				if(!stock.getBookId().trim().equals(sqm.getBookId().trim())) {
					continue ;
				}
			}
			
			//卫条件3 查数量 范围查询
			//判断条件是否有效
			if (sqm.getSumNum()>0) {
				//小于小的跳过
				if (stock.getSumNum()<sqm.getSumNum()) {
					continue;
				}
			}
			//判断条件是否有效
			if (sqm.getSumNum2()>0) {
				//大于大的跳过
				if (stock.getSumNum()>sqm.getSumNum2()) {
					continue;
				}
			}
			
			resCol.add(stock);
		}
		return resCol;
	}


}

逻辑层:StockEbi实现类(StockEbo):

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

import java.util.Collection;

import cn.hncu.booksManagementSystem.stock.business.ebi.StockEbi;
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.stock.vo.StockQueryModel;
/**
 * CreateTime: 2018年4月23日 下午2:31:27	
 * @author 宋进宇  Email:[email protected]
 */
public class StockEbo implements StockEbi{

	//注入dao
	private StockDAO dao = StockDaoFactory.getStockDAO();
	

	@Override
	public StockModel getByBookId(String bookId) {
		StockQueryModel sqm = new StockQueryModel();
		sqm.setBookId(bookId);
		Collection<StockModel> resCol = dao.getStocksInfoByCondition(sqm);
		if (resCol!=null&&resCol.size()>0) {
			for (StockModel stock : resCol) {
				return stock;
			}
		}
		return null;
	}

	@Override
	public Collection<StockModel> getAll() {
		return dao.getAll();
	}

	@Override
	public Collection<StockModel> getStocksByCondition(StockQueryModel sqm) {
		return dao.getStocksInfoByCondition(sqm);
	}

}

表现层:

package cn.hncu.booksManagementSystem.stock.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.book.vo.BookModel;
import cn.hncu.booksManagementSystem.stock.business.ebi.StockEbi;
import cn.hncu.booksManagementSystem.stock.business.factory.StockEbiFactory;
import cn.hncu.booksManagementSystem.stock.vo.StockModel;
import cn.hncu.booksManagementSystem.stock.vo.StockQueryModel;



public class ListPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private  JFrame mainFrame;
	private JList<StockModel> JList;
	//注入ebi
	private StockEbi stockEbi = StockEbiFactory.getStockEbi();
	private BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi();
	private JTextField tfdId;
	private JTextField tfdSumNum;
	private JTextField tfdSumNum2;

	private JComboBox<String> cbbxBook;
	/**
	 * Create the panel.
	 * @wbp.parser.constructor
	 */
	public ListPanel( JFrame mainFrame) {
		this.mainFrame = mainFrame;
		initContentPane();
		initJListData();
	}
	
	private void initJListData() {
		Collection<BookModel> books = bookEbi.getAll();
		cbbxBook.addItem("查询所有");
		for (BookModel book : books) {
			cbbxBook.addItem(book.getId()+" ,《"+book.getName()+"》");
		}
		JList.setListData(stockEbi.getAll().toArray(new StockModel[0]));
	}
	
	private void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("库存管理");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(308, 10, 145, 38);
		add(label);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(107, 205, 561, 283);
		add(scrollPane);
		
		JList = new JList<StockModel>();
		scrollPane.setViewportView(JList);
		JList.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		
		JButton btnQuery = new JButton("查询...");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnQueryActionPerformed(e);
			}
		});
		btnQuery.setForeground(Color.BLUE);
		btnQuery.setFont(new Font("微软雅黑", Font.BOLD, 20));
		btnQuery.setBounds(650, 146, 101, 38);
		add(btnQuery);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		lblId.setBounds(107, 84, 45, 32);
		add(lblId);
		
		tfdId = new JTextField();
		tfdId.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		tfdId.setBounds(152, 83, 165, 35);
		add(tfdId);
		tfdId.setColumns(10);
		
		tfdSumNum = new JTextField();
		tfdSumNum.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		tfdSumNum.setColumns(10);
		tfdSumNum.setBounds(152, 149, 165, 35);
		add(tfdSumNum);
		
		JLabel label_1 = new JLabel("最少数量:");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label_1.setBounds(58, 150, 95, 32);
		add(label_1);
		
		JLabel label_2 = new JLabel("图书:");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label_2.setBounds(365, 84, 54, 32);
		add(label_2);
		
		tfdSumNum2 = new JTextField();
		tfdSumNum2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		tfdSumNum2.setColumns(10);
		tfdSumNum2.setBounds(434, 149, 176, 35);
		add(tfdSumNum2);
		
		cbbxBook = new JComboBox<String>();
		cbbxBook.setToolTipText("");
		cbbxBook.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		cbbxBook.setBounds(428, 86, 240, 32);
		add(cbbxBook);
		
		JLabel label_3 = new JLabel("最多数量:");
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label_3.setBounds(327, 150, 90, 32);
		add(label_3);
	}
	/**
	 * 查询事件响应
	 * @param e 事件源
	 */
	protected void btnQueryActionPerformed(ActionEvent e) {
		//1收集参数(带格式校验) id,bookId,sumNum,sumNum2
		String id = tfdId.getText().trim();
		if(id!=null && id.length()>0) {
			if (!id.matches("^\\d{1,10}$")) {
				JOptionPane.showMessageDialog(this, "id只能有数字组成,且长度必须在1-10之间");
				return;
			}
		}
		
		String book = (String) cbbxBook.getSelectedItem();
		String bookId = null;
		if (book!=null && !"查询所有".equals(book)) {
			bookId = book.split(",")[0].trim();
		}
		
		int sumNum = 0;
		String strSumNum = tfdSumNum.getText().trim();
		if (strSumNum!=null && strSumNum.length()>0) {
			try {
				sumNum = Integer.parseInt(strSumNum);
			} catch (NumberFormatException e1) {
				JOptionPane.showMessageDialog(this, "最少数量格式错误!");
				return;
			}
		}
		
		int sumNum2 = 0;
		String strSumNum2 = tfdSumNum2.getText().trim();
		if (strSumNum2!=null && strSumNum2.length()>0) {
			try {
				sumNum2 = Integer.parseInt(strSumNum2);
			} catch (NumberFormatException e1) {
				JOptionPane.showMessageDialog(this, "最多数量格式错误!");
				return;
			}
		}
		//2组织参数
		StockQueryModel sqm = new StockQueryModel();
		sqm.setId(id);
		sqm.setBookId(bookId);
		sqm.setSumNum(sumNum);
		sqm.setSumNum2(sumNum2);
		//3调用逻辑层
		Collection<StockModel> resCol = stockEbi.getStocksByCondition(sqm);
		//4导向结果界面
		JList.setListData(resCol.toArray(new StockModel[0]));
		mainFrame.validate();
	}
}







猜你喜欢

转载自blog.csdn.net/qq_34928644/article/details/80138522