Java之------单机版书店管理系统(设计思想和设计模式系列七)库存模块

书店管理系统

书店管理系统可以说是设计模式及设计思想的一个比较经典的例子。

本系列将分为多个部分讲述此输电管理系统。

书店管理系统将分为:用户、图书、进货、销售和库存五个模块,另外还有公共包、工具包和登录包,另外还有一个框架。

对于分层设计,都是表现层可以调用逻辑层,逻辑层调用数据层,数据层调用工具和公共包,方向不可打乱,必须严格按照这种模式。

本篇将做库存模块。


注:本篇需要使用到的框架在本系列二的用户模块:

链接:http://blog.csdn.net/x121850182/article/details/51362269


数据层:

接口:

package cn.hncu.stock.dao.dao;

import java.util.List;

import cn.hncu.stock.vo.StockModel;
import cn.hncu.stock.vo.StockQueryModel;

public interface StockDAO {
	public boolean create(StockModel stock);
	public boolean upDate(StockModel stock);
	
	public StockModel getSingle(String uuid);
	public List<StockModel> getAll();
	public List<StockModel> getByCondition(StockQueryModel sqm);
}
实现类:

package cn.hncu.stock.dao.impl;

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

import cn.hncu.stock.dao.dao.StockDAO;
import cn.hncu.stock.vo.StockModel;
import cn.hncu.stock.vo.StockQueryModel;
import cn.hncu.utils.FileIo;

public class StockDAOImpl implements StockDAO {
	private final String FILE_NAME="stock.txt";
	
	@Override
	public boolean create(StockModel stock) {
		List<StockModel> list=FileIo.read(FILE_NAME);
		for (StockModel model:list){
			if (model.getUuid().equals(stock.getUuid())){
				return false;
			}
		}
		list.add(stock);
		FileIo.write(list, FILE_NAME);
		return true;
	}

	@Override
	public List<StockModel> getAll() {
		return FileIo.read(FILE_NAME);
	}

	@Override
	public List<StockModel> getByCondition(StockQueryModel sqm) {
		List<StockModel> list=FileIo.read(FILE_NAME);
		List<StockModel> results=new ArrayList<StockModel>();
		for (StockModel model:list){
			if (sqm.getUuid()!=null&&sqm.getUuid().trim().length()>0){
				if (!model.getUuid().equals(sqm.getUuid())){
					continue;
				}
			}
			if (sqm.getBookUuid()!=null&&sqm.getBookUuid().trim().length()>0){
				if (!model.getBookUuid().equals(sqm.getBookUuid())){
					continue;
				}
			}
			if (sqm.getSumNum()>0){
				if (sqm.getSumNum()>model.getSumNum()){
					continue;
				}
			}
			if (sqm.getSumNum2()>0){
				if (sqm.getSumNum2()<model.getSumNum()){
					continue;
				}
			}
			results.add(model);
		}
		
		return results;
	}

	@Override
	public StockModel getSingle(String uuid) {
		List<StockModel> list=FileIo.read(FILE_NAME);
		for (StockModel model:list){
			if (model.getUuid().equals(uuid)){
				return model;
			}
		}
		return null;
	}

	@Override
	public boolean upDate(StockModel stock) {
		List<StockModel> list=FileIo.read(FILE_NAME);
		for (int i=0;i<list.size();i++){
			if (list.get(i).getUuid().equals(stock.getUuid())){
				list.set(i, stock);
				FileIo.write(list, FILE_NAME);
				return true;
			}
		}
		return false;
	}
}
工厂类:

package cn.hncu.stock.dao.factory;

import cn.hncu.stock.dao.dao.StockDAO;
import cn.hncu.stock.dao.impl.StockDAOImpl;

public class StockDAOFactory {
	public static StockDAO getStockDAO(){
		return new StockDAOImpl();
	}
}

逻辑层:

接口:

package cn.hncu.stock.business.ebi;

import java.util.List;

import cn.hncu.stock.vo.StockModel;
import cn.hncu.stock.vo.StockQueryModel;

public interface StockEbi {
	public List<StockModel> getAll();
	public List<StockModel> getByCondition(StockQueryModel sqm);
}
实现类:

package cn.hncu.stock.business.ebo;

import java.util.List;

import cn.hncu.stock.business.ebi.StockEbi;
import cn.hncu.stock.dao.dao.StockDAO;
import cn.hncu.stock.dao.factory.StockDAOFactory;
import cn.hncu.stock.vo.StockModel;
import cn.hncu.stock.vo.StockQueryModel;

public class StockEbo implements StockEbi {
	StockDAO dao=StockDAOFactory.getStockDAO();
	
	@Override
	public List<StockModel> getAll() {
		return dao.getAll();
	}

	@Override
	public List<StockModel> getByCondition(StockQueryModel sqm) {
		return dao.getByCondition(sqm);
	}
}
工厂类:

package cn.hncu.stock.business.factory;

import cn.hncu.stock.business.ebi.StockEbi;
import cn.hncu.stock.business.ebo.StockEbo;

public class StockEbiFactory {
	public static StockEbi getStockEbi(){
		return new StockEbo();
	}
}

值对象层:
StockModel

package cn.hncu.stock.vo;

import java.io.Serializable;

public class StockModel implements Serializable{
	private String uuid;
	private String bookUuid;
	private int sumNum;
	private String bookName;//显示给用户看的
	
	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	public String getBookUuid() {
		return bookUuid;
	}
	public void setBookUuid(String bookUuid) {
		this.bookUuid = bookUuid;
	}
	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 + ((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;
		StockModel other = (StockModel) obj;
		if (uuid == null) {
			if (other.uuid != null)
				return false;
		} else if (!uuid.equals(other.uuid))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "库存编号:" + uuid + ", 书名:" + bookName + ", 数量:" + sumNum + "本";
	}
}
StockQueryModel
package cn.hncu.stock.vo;

public class StockQueryModel extends StockModel{
	private int sumNum2;

	public int getSumNum2() {
		return sumNum2;
	}

	public void setSumNum2(int sumNum2) {
		this.sumNum2 = sumNum2;
	}
}

表现层:
库存只能通过进货与销售来改变其值,因此表现层只能有查询功能与列表。

列表:

/*
 * ListPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.stock.ui;

import java.util.List;

import javax.swing.JFrame;

import cn.hncu.stock.business.factory.StockEbiFactory;
import cn.hncu.stock.vo.StockModel;

/**
 *
 * @author  __USER__
 */
public class ListPanel extends javax.swing.JPanel {
	private JFrame mainFrame = null;
	
	/** Creates new form ListPanel */
	public ListPanel(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		this.setOpaque(false);
		initComponents();
		myInit();
	}
	
	public ListPanel(JFrame mainFrame,List<StockModel> list) {
		this.mainFrame = mainFrame;
		this.setOpaque(false);
		initComponents();
		stockList.setListData(list.toArray());
	}

	private void myInit() {
		List<StockModel> list = StockEbiFactory.getStockEbi().getAll();
		stockList.setListData(list.toArray());
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		stockList = new javax.swing.JList();
		btnQuery = new javax.swing.JButton();

		setLayout(null);

		jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24));
		jLabel1.setForeground(new java.awt.Color(51, 255, 51));
		jLabel1.setText("\u5e93\u5b58\u5217\u8868");
		add(jLabel1);
		jLabel1.setBounds(320, 40, 130, 60);

		stockList.setModel(new javax.swing.AbstractListModel() {
			String[] strings = { "" };

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		jScrollPane1.setViewportView(stockList);

		add(jScrollPane1);
		jScrollPane1.setBounds(170, 120, 410, 260);

		btnQuery.setText("\u5e93\u5b58\u67e5\u8be2");
		btnQuery.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnQueryActionPerformed(evt);
			}
		});
		add(btnQuery);
		btnQuery.setBounds(330, 430, 93, 40);
	}// </editor-fold>
	//GEN-END:initComponents

	private void btnQueryActionPerformed(java.awt.event.ActionEvent evt) {
		mainFrame.setContentPane(new QueryPanel(mainFrame));
		mainFrame.validate();
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnQuery;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JList stockList;
	// End of variables declaration//GEN-END:variables

}
查询:

/*
 * QueryPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.stock.ui;

import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cn.hncu.book.business.factory.BookEbiFactory;
import cn.hncu.book.vo.BookModel;
import cn.hncu.stock.business.factory.StockEbiFactory;
import cn.hncu.stock.vo.StockModel;
import cn.hncu.stock.vo.StockQueryModel;

/**
 *
 * @author  __USER__
 */
public class QueryPanel extends javax.swing.JPanel {
	private JFrame mainFrame = null;

	/** Creates new form QueryPanel */
	public QueryPanel(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		this.setOpaque(false);
		initComponents();
		myInit();
	}

	private void myInit() {
		List<BookModel> books = BookEbiFactory.getBookEbi().getAll();
		for (BookModel book : books) {
			combBook.addItem(book.getName());
		}

	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		tfdSumNum2 = new javax.swing.JTextField();
		tfdUuid = new javax.swing.JTextField();
		tfdSumNum = new javax.swing.JTextField();
		combBook = new javax.swing.JComboBox();
		btnBack = new javax.swing.JButton();
		btnQuery = new javax.swing.JButton();

		setLayout(null);

		jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24));
		jLabel1.setForeground(new java.awt.Color(102, 255, 102));
		jLabel1.setText("\u5e93\u5b58\u67e5\u8be2");
		add(jLabel1);
		jLabel1.setBounds(310, 30, 120, 80);

		jLabel2.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
		jLabel2.setText("\u5e93\u5b58\u6700\u5927\u91cf:");
		add(jLabel2);
		jLabel2.setBounds(360, 260, 100, 40);

		jLabel3.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
		jLabel3.setText("\u5e93\u5b58\u7f16\u53f7:");
		add(jLabel3);
		jLabel3.setBounds(110, 150, 80, 30);

		jLabel4.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
		jLabel4.setText("\u56fe\u4e66:");
		add(jLabel4);
		jLabel4.setBounds(410, 150, 50, 30);

		jLabel5.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
		jLabel5.setText("\u5e93\u5b58\u6700\u5c0f\u91cf:");
		add(jLabel5);
		jLabel5.setBounds(90, 260, 100, 40);
		add(tfdSumNum2);
		tfdSumNum2.setBounds(470, 270, 130, 26);
		add(tfdUuid);
		tfdUuid.setBounds(210, 150, 120, 26);
		add(tfdSumNum);
		tfdSumNum.setBounds(210, 270, 120, 26);

		combBook.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "请选择" }));
		add(combBook);
		combBook.setBounds(470, 150, 160, 26);

		btnBack.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
		btnBack.setText("\u8fd4\u56de");
		btnBack.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnBackActionPerformed(evt);
			}
		});
		add(btnBack);
		btnBack.setBounds(390, 380, 69, 33);

		btnQuery.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
		btnQuery.setText("\u67e5\u8be2");
		btnQuery.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnQueryActionPerformed(evt);
			}
		});
		add(btnQuery);
		btnQuery.setBounds(220, 380, 69, 33);
	}// </editor-fold>
	//GEN-END:initComponents

	private void btnQueryActionPerformed(java.awt.event.ActionEvent evt) {
		//1收集参数

		//库存编号
		String uuid = tfdUuid.getText();
		//图书编号
		String bookUuid = null;
		if (combBook.getSelectedIndex() > 0) {
			bookUuid = BookEbiFactory.getBookEbi().getBookByName(
					combBook.getSelectedItem().toString()).getUuid();
		}
		//库存最小量
		int sumNum = 0;
		if (tfdSumNum.getText() != null
				&& tfdSumNum.getText().trim().length() > 0) {
			try {
				sumNum = Integer.parseInt(tfdSumNum.getText());
				if (sumNum < 0) {
					throw new NumberFormatException();
				}
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null, "库存最小数量必须输入正整数!");
			}
		}
		//库存最大量
		int sumNum2 = 0;
		if (tfdSumNum2.getText() != null
				&& tfdSumNum2.getText().trim().length() > 0) {
			try {
				sumNum2 = Integer.parseInt(tfdSumNum2.getText());
				if (sumNum2 < 0) {
					throw new NumberFormatException();
				}
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null, "库存最大数量必须输入正整数!");
			}
		}

		//2组织参数
		StockQueryModel sqm = new StockQueryModel();
		sqm.setUuid(uuid);
		sqm.setBookUuid(bookUuid);
		sqm.setSumNum(sumNum);
		sqm.setSumNum2(sumNum2);

		//3调用逻辑层
		List<StockModel> list = StockEbiFactory.getStockEbi().getByCondition(
				sqm);

		//4返回到结果界面
		mainFrame.setContentPane(new ListPanel(mainFrame, list));
		mainFrame.validate();
	}

	private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
		mainFrame.setContentPane(new ListPanel(mainFrame));
		mainFrame.validate();
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnBack;
	private javax.swing.JButton btnQuery;
	private javax.swing.JComboBox combBook;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JTextField tfdSumNum;
	private javax.swing.JTextField tfdSumNum2;
	private javax.swing.JTextField tfdUuid;
	// End of variables declaration//GEN-END:variables

}


你可能感兴趣的:(设计模式,设计思想,面向接口编程,书店系统)