书店管理系统
书店管理系统可以说是设计模式及设计思想的一个比较经典的例子。
本系列将分为多个部分讲述此输电管理系统。
书店管理系统将分为:用户、图书、进货、销售和库存五个模块,另外还有公共包、工具包和登录包,另外还有一个框架。
对于分层设计,都是表现层可以调用逻辑层,逻辑层调用数据层,数据层调用工具和公共包,方向不可打乱,必须严格按照这种模式。
本篇将做工具包和公共包。
注:本篇需要使用到的框架在本系列二的用户模块:
链接:点击打开链接
同系列有:
系列一(概述):点击打开链接
系列二(用户):点击打开链接
系列四(图书):点击打开链接
系列五(进货):点击打开链接
系列六(销售):点击打开链接
系列七(库存):点击打开链接
系列八(登录):点击打开链接
工具包:
工具包是使用IO进行数据存储的,因为本项目里面的类进行存储时都是用到了此包,因此在这里先写了。
日期转换:
- package cn.hncu.utils;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.swing.JOptionPane;
- public class DateUtil {
- private DateUtil(){
- }
- public static String longToString(long inDate){
- Date date=new Date(inDate);
- DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
- String str = df.format(date);
- return str;
- }
- public static long StringToLong(String txtDate,String errorInfo){
- DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
- long inDate=0;
- try {
- Date d=df.parse(txtDate);
- inDate=d.getTime();
- } catch (ParseException e) {
- JOptionPane.showMessageDialog(null, errorInfo);
- return -1;
- }
- return inDate;
- }
- }
- package cn.hncu.utils;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JOptionPane;
- public class FileIo {
- public static<E> List<E> read(String fileName){
- List<E> list = new ArrayList<E>();
- final File file = new File(fileName);
- if (!file.exists()) {
- JOptionPane.showMessageDialog(null, "数据表不存在!");
- return list;
- }
- ObjectInputStream in = null;
- try {
- in = new ObjectInputStream(new FileInputStream(fileName));
- list = (List<E>)in.readObject();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }finally{
- if(in!=null){
- try {
- in.close();
- } catch (IOException e) {
- throw new RuntimeException("数据库关闭失败!");
- }
- }
- }
- return list;
- }
- public static<E> void write(List<E> list,String fileName){
- ObjectOutputStream out=null;
- try {
- out=new ObjectOutputStream(new FileOutputStream(fileName));
- out.writeObject(list);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (out!=null){
- try {
- out.close();
- } catch (IOException e) {
- throw new RuntimeException("数据库关闭失败");
- }
- }
- }
- }
- }
公共包:
公共包中放的是枚举常量和单独对用户、图书、进货、销售和库存的id编号进行存储的uuid包
1、枚举
用户类型枚举:
- package cn.hncu.common;
- public enum UserTypeEnum {
- ADMIN(1,"超级管理员"),
- BOOK(2,"图书管理员"),
- IN(3,"进货管理员"),
- OUT(4,"销售管理员"),
- STOCK(5,"库存管理员");
- private final int type;
- private final String name;
- private UserTypeEnum(int type, String name){
- this.type = type;
- this.name = name;
- }
- public int getType() {
- return type;
- }
- public String getName() {
- return name;
- }
- public static String getNameByType( int type){
- for( UserTypeEnum userType:UserTypeEnum.values()){
- if(userType.getType()==type){
- return userType.getName();
- }
- }
- throw new IllegalArgumentException("枚举中没有对应的用户类型:"+type);
- }
- public static int getTypeByName( String name){
- for(UserTypeEnum userType: UserTypeEnum.values()){
- if(userType.getName().equals(name)){
- return userType.getType();
- }
- }
- throw new IllegalArgumentException("枚举中没有对应的用户类型:"+name);
- }
- }
- package cn.hncu.common;
- public enum UuidModelConstance {
- USER("UserModel"),
- BOOK("BookModel"),
- IN_MAIN("InMainModel"),
- IN_DETAIL("InDetailModel"),
- OUT_MAIN("outMainModel"),
- OUT_DETAIL("OutDetailModel"),
- STOCK("StockModel");
- private final String name;
- private UuidModelConstance(String name){
- this.name=name;
- }
- public String getName(){
- return name;
- }
- }
值对象层:
- package cn.hncu.common.uuid.vo;
- import java.io.Serializable;
- public class UuidModel implements Serializable{
- private String modelName;
- private int currentNum;
- public String getModelName() {
- return modelName;
- }
- public void setModelName(String modelName) {
- this.modelName = modelName;
- }
- public int getCurrentNum() {
- return currentNum;
- }
- public void setCurrentNum(int currentNum) {
- this.currentNum = currentNum;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + currentNum;
- result = prime * result
- + ((modelName == null) ? 0 : modelName.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;
- UuidModel other = (UuidModel) obj;
- if (currentNum != other.currentNum)
- return false;
- if (modelName == null) {
- if (other.modelName != null)
- return false;
- } else if (!modelName.equals(other.modelName))
- return false;
- return true;
- }
- }
1)接口:
- package cn.hncu.common.uuid.dao.dao;
- import cn.hncu.common.UuidModelConstance;
- public interface UuidDAO {
- public String getNextUuid(UuidModelConstance uuidEnum);
- }
- package cn.hncu.common.uuid.dao.impl;
- import java.util.List;
- import cn.hncu.common.UuidModelConstance;
- import cn.hncu.common.uuid.dao.dao.UuidDAO;
- import cn.hncu.common.uuid.vo.UuidModel;
- import cn.hncu.utils.FileIo;
- public class UuidDAOImpl implements UuidDAO {
- private String FILE_NAME="Uuid.txt";
- @Override
- public String getNextUuid(UuidModelConstance uuidEnum) {
- String modelName=uuidEnum.getName();
- List<UuidModel> list=FileIo.read(FILE_NAME);
- for (UuidModel uuid:list){
- if (uuid.getModelName().equals(modelName)){
- int type=uuid.getCurrentNum();
- uuid.setCurrentNum(type+1);
- FileIo.write(list, FILE_NAME);
- return String.valueOf(type);
- }
- }
- int type=1;
- UuidModel uuid=new UuidModel();
- uuid.setModelName(modelName);
- uuid.setCurrentNum(2);
- list.add(uuid);
- FileIo.write(list, FILE_NAME);
- return String.valueOf(type);
- }
- }
- package cn.hncu.common.uuid.dao.factory;
- import cn.hncu.common.uuid.dao.dao.UuidDAO;
- import cn.hncu.common.uuid.dao.impl.UuidDAOImpl;
- public class UuidDAOFactory {
- public static UuidDAO getUuidDAO(){
- return new UuidDAOImpl();
- }
- }
本文含有隐藏内容,请 开通VIP 后查看