Java之------单机版书店管理系统(设计思想和设计模式系列三)工具包和公共包
2016-05-10 13:20
691 查看
书店管理系统
书店管理系统可以说是设计模式及设计思想的一个比较经典的例子。
本系列将分为多个部分讲述此输电管理系统。
书店管理系统将分为:用户、图书、进货、销售和库存五个模块,另外还有公共包、工具包和登录包,另外还有一个框架。
对于分层设计,都是表现层可以调用逻辑层,逻辑层调用数据层,数据层调用工具和公共包,方向不可打乱,必须严格按照这种模式。
本篇将做工具包和公共包。
注:本篇需要使用到的框架在本系列二的用户模块:
链接:http://blog.csdn.net/x121850182/article/details/51362269
工具包:
工具包是使用IO进行数据存储的,因为本项目里面的类进行存储时都是用到了此包,因此在这里先写了。
日期转换:
公共包:
公共包中放的是枚举常量和单独对用户、图书、进货、销售和库存的id编号进行存储的uuid包
1、枚举
用户类型枚举:
2、uuid包
值对象层:
1)接口:
书店管理系统可以说是设计模式及设计思想的一个比较经典的例子。
本系列将分为多个部分讲述此输电管理系统。
书店管理系统将分为:用户、图书、进货、销售和库存五个模块,另外还有公共包、工具包和登录包,另外还有一个框架。
对于分层设计,都是表现层可以调用逻辑层,逻辑层调用数据层,数据层调用工具和公共包,方向不可打乱,必须严格按照这种模式。
本篇将做工具包和公共包。
注:本篇需要使用到的框架在本系列二的用户模块:
链接:http://blog.csdn.net/x121850182/article/details/51362269
工具包:
工具包是使用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); } }Uuid模型枚举:
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; } }
2、uuid包
值对象层:
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); }2)实现类:
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); } }3)工厂类:
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(); } }
相关文章推荐
- spring的PROPAGATION_REQUIRES_NEW事务,下列说法正确的是(D)
- android adt自带eclipse无法设置ndk路径(找不到NDK配置)
- Java 实现图片水印之水印图片(SpringMVC + Jsp)
- java SpringMVC Filter登录拦截器
- java定时任务
- Eclipse NDK 配置
- Java HashMap工作原理及实现(二)
- Java之------单机版书店管理系统(设计思想和设计模式系列二)用户模块
- Java中,当表单含有文件上传时,提交数据的如何读取
- Spring 4.x Reference翻译(一)IOC容器
- struts2标签小问题
- 【JAVA 动态创建风水罗盘 超精华Swing项目方案-项目源码 - 文章05】
- hadoop 2.7.2 (hadoop2.x)使用ant制作eclipse插件hadoop-eclipse-plugin-2.7.2.jar
- JAVA IO学习
- springcloud(第六篇)springcloud ribbon
- Java 泛型
- Java 学习日志 2.1 if 条件选择语句(if switch 四种方法检验成绩状况)
- eclipse不按ALT+/没有提示关键字的解决方法
- SpringMVC 学习-入门篇
- Java解析xml的四种方法汇总