Java之------单机版书店管理系统(设计思想和设计模式系列三)工具包和公共类

书店管理系统

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

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

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

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

本篇将做工具包和公共包。


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

链接:点击打开链接


同系列有:

系列一(概述):点击打开链接

系列二(用户):点击打开链接

系列四(图书):点击打开链接

系列五(进货):点击打开链接

系列六(销售):点击打开链接

系列七(库存):点击打开链接

系列八(登录):点击打开链接


工具包:

工具包是使用IO进行数据存储的,因为本项目里面的类进行存储时都是用到了此包,因此在这里先写了。

日期转换:

[java]  view plain  copy
 
  1. package cn.hncu.utils;  
  2.   
  3. import java.text.DateFormat;  
  4. import java.text.ParseException;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. import javax.swing.JOptionPane;  
  9.   
  10. public class DateUtil {  
  11.     private DateUtil(){  
  12.     }  
  13.       
  14.     public static String longToString(long inDate){  
  15.         Date date=new Date(inDate);  
  16.         DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");  
  17.         String str = df.format(date);  
  18.         return str;  
  19.     }  
  20.       
  21.     public static long StringToLong(String txtDate,String errorInfo){  
  22.         DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");  
  23.         long inDate=0;  
  24.         try {  
  25.             Date d=df.parse(txtDate);  
  26.             inDate=d.getTime();  
  27.         } catch (ParseException e) {  
  28.             JOptionPane.showMessageDialog(null, errorInfo);  
  29.             return -1;  
  30.         }  
  31.         return inDate;  
  32.     }  
  33. }  
文件读取和存储:

[java]  view plain  copy
 
  1. package cn.hncu.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.ObjectInputStream;  
  9. import java.io.ObjectOutputStream;  
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12.   
  13. import javax.swing.JOptionPane;  
  14.   
  15. public class FileIo {  
  16.     public static<E> List<E> read(String fileName){  
  17.         List<E> list = new ArrayList<E>();  
  18.         final File file = new File(fileName);  
  19.         if (!file.exists()) {  
  20.             JOptionPane.showMessageDialog(null"数据表不存在!");  
  21.             return list;  
  22.         }  
  23.         ObjectInputStream in = null;  
  24.         try {  
  25.             in = new ObjectInputStream(new FileInputStream(fileName));  
  26.             list = (List<E>)in.readObject();  
  27.         } catch (FileNotFoundException e) {  
  28.             e.printStackTrace();  
  29.         } catch (IOException e) {  
  30.             e.printStackTrace();  
  31.         } catch (ClassNotFoundException e) {  
  32.             e.printStackTrace();  
  33.         }finally{  
  34.             if(in!=null){  
  35.                 try {  
  36.                     in.close();  
  37.                 } catch (IOException e) {  
  38.                     throw new RuntimeException("数据库关闭失败!");  
  39.                 }  
  40.             }  
  41.         }  
  42.   
  43.         return list;  
  44.     }  
  45.       
  46.     public static<E> void write(List<E> list,String fileName){  
  47.         ObjectOutputStream out=null;  
  48.         try {  
  49.             out=new ObjectOutputStream(new FileOutputStream(fileName));  
  50.             out.writeObject(list);  
  51.         } catch (FileNotFoundException e) {  
  52.             e.printStackTrace();  
  53.         } catch (IOException e) {  
  54.             e.printStackTrace();  
  55.         } finally {  
  56.             if (out!=null){  
  57.                 try {  
  58.                     out.close();  
  59.                 } catch (IOException e) {  
  60.                     throw new RuntimeException("数据库关闭失败");  
  61.                 }  
  62.             }  
  63.         }  
  64.     }  
  65. }  

公共包:

公共包中放的是枚举常量和单独对用户、图书、进货、销售和库存的id编号进行存储的uuid包

1、枚举

用户类型枚举:

[java]  view plain  copy
 
  1. package cn.hncu.common;  
  2.   
  3. public enum UserTypeEnum {  
  4.     ADMIN(1,"超级管理员"),  
  5.     BOOK(2,"图书管理员"),  
  6.     IN(3,"进货管理员"),  
  7.     OUT(4,"销售管理员"),  
  8.     STOCK(5,"库存管理员");  
  9.   
  10.     private final int type;  
  11.     private final String name;  
  12.     private UserTypeEnum(int type, String name){  
  13.         this.type = type;  
  14.         this.name = name;  
  15.     }  
  16.   
  17.     public int getType() {  
  18.         return type;  
  19.     }  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     public static String getNameByType( int type){  
  25.         for( UserTypeEnum userType:UserTypeEnum.values()){  
  26.             if(userType.getType()==type){  
  27.                 return userType.getName();  
  28.             }  
  29.         }  
  30.         throw new IllegalArgumentException("枚举中没有对应的用户类型:"+type);  
  31.     }  
  32.   
  33.     public static int getTypeByName( String name){  
  34.         for(UserTypeEnum userType: UserTypeEnum.values()){  
  35.             if(userType.getName().equals(name)){  
  36.                 return userType.getType();  
  37.             }  
  38.         }  
  39.         throw new IllegalArgumentException("枚举中没有对应的用户类型:"+name);  
  40.     }  
  41. }  
Uuid模型枚举:

[java]  view plain  copy
 
  1. package cn.hncu.common;  
  2.   
  3. public enum UuidModelConstance {  
  4.     USER("UserModel"),  
  5.     BOOK("BookModel"),  
  6.     IN_MAIN("InMainModel"),  
  7.     IN_DETAIL("InDetailModel"),  
  8.     OUT_MAIN("outMainModel"),  
  9.     OUT_DETAIL("OutDetailModel"),  
  10.     STOCK("StockModel");  
  11.       
  12.     private final String name;  
  13.     private UuidModelConstance(String name){  
  14.         this.name=name;  
  15.     }  
  16.     public String getName(){  
  17.         return name;  
  18.     }  
  19. }  


2、uuid包

值对象层:

[java]  view plain  copy
 
  1. package cn.hncu.common.uuid.vo;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class UuidModel implements Serializable{  
  6.     private String modelName;  
  7.     private int currentNum;  
  8.       
  9.     public String getModelName() {  
  10.         return modelName;  
  11.     }  
  12.     public void setModelName(String modelName) {  
  13.         this.modelName = modelName;  
  14.     }  
  15.     public int getCurrentNum() {  
  16.         return currentNum;  
  17.     }  
  18.     public void setCurrentNum(int currentNum) {  
  19.         this.currentNum = currentNum;  
  20.     }  
  21.       
  22.     @Override  
  23.     public int hashCode() {  
  24.         final int prime = 31;  
  25.         int result = 1;  
  26.         result = prime * result + currentNum;  
  27.         result = prime * result  
  28.                 + ((modelName == null) ? 0 : modelName.hashCode());  
  29.         return result;  
  30.     }  
  31.     @Override  
  32.     public boolean equals(Object obj) {  
  33.         if (this == obj)  
  34.             return true;  
  35.         if (obj == null)  
  36.             return false;  
  37.         if (getClass() != obj.getClass())  
  38.             return false;  
  39.         UuidModel other = (UuidModel) obj;  
  40.         if (currentNum != other.currentNum)  
  41.             return false;  
  42.         if (modelName == null) {  
  43.             if (other.modelName != null)  
  44.                 return false;  
  45.         } else if (!modelName.equals(other.modelName))  
  46.             return false;  
  47.         return true;  
  48.     }  
  49. }  
数据层:

1)接口:

[java]  view plain  copy
 
  1. package cn.hncu.common.uuid.dao.dao;  
  2.   
  3. import cn.hncu.common.UuidModelConstance;  
  4.   
  5. public interface UuidDAO {  
  6.     public String getNextUuid(UuidModelConstance uuidEnum);  
  7. }  
2)实现类:

[java]  view plain  copy
 
  1. package cn.hncu.common.uuid.dao.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.hncu.common.UuidModelConstance;  
  6. import cn.hncu.common.uuid.dao.dao.UuidDAO;  
  7. import cn.hncu.common.uuid.vo.UuidModel;  
  8. import cn.hncu.utils.FileIo;  
  9.   
  10. public class UuidDAOImpl implements UuidDAO {  
  11.     private String FILE_NAME="Uuid.txt";  
  12.   
  13.     @Override  
  14.     public String getNextUuid(UuidModelConstance uuidEnum) {  
  15.         String modelName=uuidEnum.getName();  
  16.         List<UuidModel> list=FileIo.read(FILE_NAME);  
  17.         for (UuidModel uuid:list){  
  18.             if (uuid.getModelName().equals(modelName)){  
  19.                 int type=uuid.getCurrentNum();  
  20.                 uuid.setCurrentNum(type+1);  
  21.                 FileIo.write(list, FILE_NAME);  
  22.                 return String.valueOf(type);  
  23.             }  
  24.         }  
  25.         int type=1;  
  26.         UuidModel uuid=new UuidModel();  
  27.         uuid.setModelName(modelName);  
  28.         uuid.setCurrentNum(2);  
  29.         list.add(uuid);  
  30.         FileIo.write(list, FILE_NAME);  
  31.         return String.valueOf(type);  
  32.     }  
  33. }  
3)工厂类:

[java]  view plain  copy
 
  1. package cn.hncu.common.uuid.dao.factory;  
  2.   
  3. import cn.hncu.common.uuid.dao.dao.UuidDAO;  
  4. import cn.hncu.common.uuid.dao.impl.UuidDAOImpl;  
  5.   
  6. public class UuidDAOFactory {  
  7.     public static UuidDAO getUuidDAO(){  
  8.         return new UuidDAOImpl();  
  9.     }  


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