MIS信息管理系统实战开发之单独使用文件实现保存
开发背景
ID、姓名、年龄为公共信息,而学生有成绩,工人有工资
定义一个抽象类Person(ID、姓名、年龄),学生是其子类,有成绩,工人是其子类有工资
ID如何定义呢?
ID最好可以自己生成,最好的方式是采用下面的编码方式:
· 标记 + 时间戳 + 三位随机数
· 例如:2009年3月22 20:10:10.345
· 学生的标记为s,工人的标记为w
· 生成的ID号: 学生 --> s20090322201010345023
工人 --> w20090322201010345023
因为现在的程序要满足文件和数据库的操作标准,所以此处应该定义出一个公共的标准 —— 接口
查询信息的时候可以进行排序操作,可以使用Comparable接口完成。
ID最好可以自己生成,最好的方式是采用下面的编码方式:
· 标记 + 时间戳 + 三位随机数
· 例如:2009年3月22 20:10:10.345
· 学生的标记为s,工人的标记为w
· 生成的ID号: 学生 --> s20090322201010345023
工人 --> w20090322201010345023
因为现在的程序要满足文件和数据库的操作标准,所以此处应该定义出一个公共的标准 —— 接口
查询信息的时候可以进行排序操作,可以使用Comparable接口完成。
整个代码中牵扯到数据层的操作
· 数据层就是指真实的数据操作 --> CRUD。
· 最终结果操作的肯定是一个人(人为工人和学生)
应该进行分开,一个是全部的学生管理,一个是全部的工人管理。
数据层操作标准定义完成之后,有两种选择,一种是直接使用子类实现,但是以后的修改不是很方便,
所以此处最好使用代理设计的思路完成,做一个中间层。
代码关系:
Main --> Menu --> PersonOperate --> DAO
· 数据层就是指真实的数据操作 --> CRUD。
· 最终结果操作的肯定是一个人(人为工人和学生)
应该进行分开,一个是全部的学生管理,一个是全部的工人管理。
数据层操作标准定义完成之后,有两种选择,一种是直接使用子类实现,但是以后的修改不是很方便,
所以此处最好使用代理设计的思路完成,做一个中间层。
代码关系:
Main --> Menu --> PersonOperate --> DAO
因为程序即要求使用文件保存,又要求使用数据库保存,所以此处可以设计出一个工厂,通过此工厂进行DAO的操作子类实例取得。
###################Michael分割线#####################
WorkerOperate.java
package org.michael.demo.operate;
import java.util.Iterator;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.factory.DAOFactory;
import org.michael.demo.util.InputData;
import org.michael.demo.util.TimeStamp;
import org.michael.demo.vo.Person;
import org.michael.demo.vo.Worker;
public class WorkerOperate implements PersonOperate {
private PersonDAO dao = null;
private InputData input = null;
public WorkerOperate() {
this.dao = DAOFactory.getPersonDAOInstance( "worker.ser");
this.input = new InputData();
}
public void add() {
String id = new TimeStamp( "w").getTimeStampRandom();
System.out.print( "输入工人姓名:");
String name = this.input.getString();
System.out.print( "输入工人年龄:");
int age = this.input.getInt();
System.out.print( "输入工人工资:");
float salary = this.input.getFloat();
Worker w = new Worker(id, name, age, salary);
try {
this.dao.doCreate(w);
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete() {
String id = null;
System.out.print( "请输入要删除的工人ID:");
id = input.getString();
try {
this.dao.doDelete(id);
} catch (Exception e) {
e.printStackTrace();
}
}
public void update() {
// 更新之前需要先查询出来
String id = null;
System.out.print( "请输入要修改的工人ID:");
id = input.getString();
Worker w = null;
try {
w = (Worker) this.dao.findById(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print( "输入工人姓名(原姓名:" + w.getName() + "):");
String name = this.input.getString();
System.out.print( "输入工人年龄(原年龄:" + w.getAge() + "):");
int age = this.input.getInt();
System.out.print( "输入工人工资(原工资:" + w.getSalary() + "):");
float salary = this.input.getFloat();
w.setName(name);
w.setAge(age);
w.setSalary(salary);
try {
this.dao.doUpdate(w);
} catch (Exception e) {
e.printStackTrace();
}
}
public void findAll() {
Iterator iter =
null;
try {
iter = this.dao.findAll().iterator();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( "ID\t\t\t姓名\t年龄\t工资");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
public void findByLike() {
String keyWord = null ;
System.out.print( "请输入查询关键字:") ;
keyWord = this.input.getString() ;
Iterator iter =
null;
try {
iter = this.dao.findByLike(keyWord).iterator();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( "ID\t\t\t姓名\t年龄\t工资");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
import java.util.Iterator;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.factory.DAOFactory;
import org.michael.demo.util.InputData;
import org.michael.demo.util.TimeStamp;
import org.michael.demo.vo.Person;
import org.michael.demo.vo.Worker;
public class WorkerOperate implements PersonOperate {
private PersonDAO dao = null;
private InputData input = null;
public WorkerOperate() {
this.dao = DAOFactory.getPersonDAOInstance( "worker.ser");
this.input = new InputData();
}
public void add() {
String id = new TimeStamp( "w").getTimeStampRandom();
System.out.print( "输入工人姓名:");
String name = this.input.getString();
System.out.print( "输入工人年龄:");
int age = this.input.getInt();
System.out.print( "输入工人工资:");
float salary = this.input.getFloat();
Worker w = new Worker(id, name, age, salary);
try {
this.dao.doCreate(w);
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete() {
String id = null;
System.out.print( "请输入要删除的工人ID:");
id = input.getString();
try {
this.dao.doDelete(id);
} catch (Exception e) {
e.printStackTrace();
}
}
public void update() {
// 更新之前需要先查询出来
String id = null;
System.out.print( "请输入要修改的工人ID:");
id = input.getString();
Worker w = null;
try {
w = (Worker) this.dao.findById(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print( "输入工人姓名(原姓名:" + w.getName() + "):");
String name = this.input.getString();
System.out.print( "输入工人年龄(原年龄:" + w.getAge() + "):");
int age = this.input.getInt();
System.out.print( "输入工人工资(原工资:" + w.getSalary() + "):");
float salary = this.input.getFloat();
w.setName(name);
w.setAge(age);
w.setSalary(salary);
try {
this.dao.doUpdate(w);
} catch (Exception e) {
e.printStackTrace();
}
}
public void findAll() {
Iterator
try {
iter = this.dao.findAll().iterator();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( "ID\t\t\t姓名\t年龄\t工资");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
public void findByLike() {
String keyWord = null ;
System.out.print( "请输入查询关键字:") ;
keyWord = this.input.getString() ;
Iterator
try {
iter = this.dao.findByLike(keyWord).iterator();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( "ID\t\t\t姓名\t年龄\t工资");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
PersonDAOProxyFile.java
package org.michael.demo.proxy;
import java.util.Set;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.dao.impl.PersonDAOImplFile;
import org.michael.demo.vo.Person;
public class PersonDAOProxyFile implements PersonDAO {
private PersonDAO dao = null;
public PersonDAOProxyFile(String fileName) {
PersonDAOImplFile.fileName = fileName;
this.dao = new PersonDAOImplFile();
}
public boolean doCreate(Person person) throws Exception {
return this.dao.doCreate(person);
}
public boolean doDelete(String id) throws Exception {
return this.dao.doDelete(id);
}
public boolean doUpdate(Person person) throws Exception {
return this.dao.doUpdate(person);
}
public Set findAll()
throws Exception {
return this.dao.findAll();
}
public Person findById(String id) throws Exception {
return this.dao.findById(id);
}
public Set findByLike(String keyWord)
throws Exception {
return this.dao.findByLike(keyWord);
}
}
import java.util.Set;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.dao.impl.PersonDAOImplFile;
import org.michael.demo.vo.Person;
public class PersonDAOProxyFile implements PersonDAO {
private PersonDAO dao = null;
public PersonDAOProxyFile(String fileName) {
PersonDAOImplFile.fileName = fileName;
this.dao = new PersonDAOImplFile();
}
public boolean doCreate(Person person) throws Exception {
return this.dao.doCreate(person);
}
public boolean doDelete(String id) throws Exception {
return this.dao.doDelete(id);
}
public boolean doUpdate(Person person) throws Exception {
return this.dao.doUpdate(person);
}
public Set
return this.dao.findAll();
}
public Person findById(String id) throws Exception {
return this.dao.findById(id);
}
public Set
return this.dao.findByLike(keyWord);
}
}
InputData.java
package org.michael.demo.util;
//输入数据的操作类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputData {
private BufferedReader buf = null;
public InputData() {
buf = new BufferedReader( new InputStreamReader(System.in));
}
public int getInt() {
int temp = 0;
// 如果输入的不是数字,至少应该有一个提示,告诉用户输入错了
// 可以使用正则验证
String str = null;
boolean flag = true;
while (flag) {
// 输入数据
str = this.getString();
if (!(str.matches( "\\d+"))) {
// 如果不是一个数字,则必须重新输入
System.out.print( "输入的内容,必须是整数,请重新输入:");
} else {
// 是一个正确的数字,则可以进行转换
temp = Integer.parseInt(str);
// 表示退出循环
flag = false;
}
}
return temp;
}
public String getString() {
String str = null;
try {
str = buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public float getFloat() {
float f = 0.0f;
String str = null;
boolean flag = true;
while (flag) {
// 输入数据
str = this.getString();
if (!(str.matches( "\\d+.?\\d{1,2}"))) {
// 如果不是一个数字,则必须重新输入
System.out.print( "输入的内容,必须是小数(小数点后只到两位),请重新输入:");
} else {
// 是一个正确的数字,则可以进行转换
f = Float.parseFloat(str);
// 表示退出循环
flag = false;
}
}
return f;
}
}
//输入数据的操作类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputData {
private BufferedReader buf = null;
public InputData() {
buf = new BufferedReader( new InputStreamReader(System.in));
}
public int getInt() {
int temp = 0;
// 如果输入的不是数字,至少应该有一个提示,告诉用户输入错了
// 可以使用正则验证
String str = null;
boolean flag = true;
while (flag) {
// 输入数据
str = this.getString();
if (!(str.matches( "\\d+"))) {
// 如果不是一个数字,则必须重新输入
System.out.print( "输入的内容,必须是整数,请重新输入:");
} else {
// 是一个正确的数字,则可以进行转换
temp = Integer.parseInt(str);
// 表示退出循环
flag = false;
}
}
return temp;
}
public String getString() {
String str = null;
try {
str = buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public float getFloat() {
float f = 0.0f;
String str = null;
boolean flag = true;
while (flag) {
// 输入数据
str = this.getString();
if (!(str.matches( "\\d+.?\\d{1,2}"))) {
// 如果不是一个数字,则必须重新输入
System.out.print( "输入的内容,必须是小数(小数点后只到两位),请重新输入:");
} else {
// 是一个正确的数字,则可以进行转换
f = Float.parseFloat(str);
// 表示退出循环
flag = false;
}
}
return f;
}
}
TimeStamp.java
package org.michael.demo.util;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
// 取得时间戳的类
public class TimeStamp {
private Calendar calendar = null;
// 此标记有外部决定
private String flag = null;
public TimeStamp() {
this.calendar = new GregorianCalendar();
}
public TimeStamp(String flag) {
this() ;
this.flag = flag;
}
public String getTimeStamp() {
StringBuffer buf = new StringBuffer();
if ( this.flag != null) {
buf.append( this.flag);
}
buf.append( this.addZero(calendar.get(Calendar.YEAR), 4));
buf.append( this.addZero(calendar.get(Calendar.MONTH) + 1, 2));
buf.append( this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
buf.append( this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2));
buf.append( this.addZero(calendar.get(Calendar.MINUTE), 2));
buf.append( this.addZero(calendar.get(Calendar.SECOND), 2));
buf.append( this.addZero(calendar.get(Calendar.MILLISECOND), 3));
return buf.toString();
}
public String getTimeStampRandom() {
StringBuffer buf = new StringBuffer();
Random r = new Random();
buf.append( this.getTimeStamp());
buf.append(r.nextInt(10));
buf.append(r.nextInt(10));
buf.append(r.nextInt(10));
return buf.toString();
}
// 可以单独设置一个加“0”的操作
private String addZero( int temp, int len) {
String str = temp + "";
while (str.length() < len) {
str = "0" + str;
}
return str;
}
}
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
// 取得时间戳的类
public class TimeStamp {
private Calendar calendar = null;
// 此标记有外部决定
private String flag = null;
public TimeStamp() {
this.calendar = new GregorianCalendar();
}
public TimeStamp(String flag) {
this() ;
this.flag = flag;
}
public String getTimeStamp() {
StringBuffer buf = new StringBuffer();
if ( this.flag != null) {
buf.append( this.flag);
}
buf.append( this.addZero(calendar.get(Calendar.YEAR), 4));
buf.append( this.addZero(calendar.get(Calendar.MONTH) + 1, 2));
buf.append( this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
buf.append( this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2));
buf.append( this.addZero(calendar.get(Calendar.MINUTE), 2));
buf.append( this.addZero(calendar.get(Calendar.SECOND), 2));
buf.append( this.addZero(calendar.get(Calendar.MILLISECOND), 3));
return buf.toString();
}
public String getTimeStampRandom() {
StringBuffer buf = new StringBuffer();
Random r = new Random();
buf.append( this.getTimeStamp());
buf.append(r.nextInt(10));
buf.append(r.nextInt(10));
buf.append(r.nextInt(10));
return buf.toString();
}
// 可以单独设置一个加“0”的操作
private String addZero( int temp, int len) {
String str = temp + "";
while (str.length() < len) {
str = "0" + str;
}
return str;
}
}
Person.java
package org.michael.demo.vo;
import java.io.Serializable;
public abstract class Person implements Comparable,Serializable {
// 定义各公共属性
private String id ;
private String name ;
private int age ;
public Person(String id,String name, int age){
this.setId(id) ;
this.setName(name) ;
this.setAge(age) ;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge( int age) {
this.age = age;
}
}
import java.io.Serializable;
public abstract class Person implements Comparable,Serializable {
// 定义各公共属性
private String id ;
private String name ;
private int age ;
public Person(String id,String name, int age){
this.setId(id) ;
this.setName(name) ;
this.setAge(age) ;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge( int age) {
this.age = age;
}
}
Student.java
package org.michael.demo.vo;
public class Student extends Person {
private static final long serialVersionUID = 1L;
private float score;
public Student(String id, String name, int age, float score) {
super(id, name, age);
this.setScore(score);
}
public int compareTo(Object arg0) {
Student s = (Student) arg0;
if ( this.score < s.score) {
return 1;
} else if ( this.score > s.score) {
return -1;
} else {
if ( super.getAge() < s.getAge()) {
return -1;
} else if ( super.getAge() > s.getAge()) {
return 1;
} else {
return 0;
}
}
}
public String toString() {
return this.getId() + "\t" + this.getName() + "\t" + this.getAge() + "\t" + this.score;
}
public float getScore() {
return score;
}
public void setScore( float score) {
this.score = score;
}
}
public class Student extends Person {
private static final long serialVersionUID = 1L;
private float score;
public Student(String id, String name, int age, float score) {
super(id, name, age);
this.setScore(score);
}
public int compareTo(Object arg0) {
Student s = (Student) arg0;
if ( this.score < s.score) {
return 1;
} else if ( this.score > s.score) {
return -1;
} else {
if ( super.getAge() < s.getAge()) {
return -1;
} else if ( super.getAge() > s.getAge()) {
return 1;
} else {
return 0;
}
}
}
public String toString() {
return this.getId() + "\t" + this.getName() + "\t" + this.getAge() + "\t" + this.score;
}
public float getScore() {
return score;
}
public void setScore( float score) {
this.score = score;
}
}
Worker.java
package org.michael.demo.vo;
public class Worker extends Person {
private static final long serialVersionUID = 1L;
private float salary;
public Worker(String id, String name, int age, float salary) {
super(id, name, age);
this.setSalary(salary);
}
public int compareTo(Object arg0) {
Worker w = (Worker) arg0;
if ( this.salary < w.salary) {
return 1;
} else if ( this.salary > w.salary) {
return -1;
} else {
if ( super.getAge() < w.getAge()) {
return -1;
} else if ( super.getAge() > w.getAge()) {
return 1;
} else {
return 0;
}
}
}
public String toString() {
return this.getId() + "\t" + this.getName() + "\t" + this.getAge()
+ "\t" + this.salary;
}
public float getSalary() {
return salary;
}
public void setSalary( float salary) {
this.salary = salary;
}
}
public class Worker extends Person {
private static final long serialVersionUID = 1L;
private float salary;
public Worker(String id, String name, int age, float salary) {
super(id, name, age);
this.setSalary(salary);
}
public int compareTo(Object arg0) {
Worker w = (Worker) arg0;
if ( this.salary < w.salary) {
return 1;
} else if ( this.salary > w.salary) {
return -1;
} else {
if ( super.getAge() < w.getAge()) {
return -1;
} else if ( super.getAge() > w.getAge()) {
return 1;
} else {
return 0;
}
}
}
public String toString() {
return this.getId() + "\t" + this.getName() + "\t" + this.getAge()
+ "\t" + this.salary;
}
public float getSalary() {
return salary;
}
public void setSalary( float salary) {
this.salary = salary;
}
}
测试:
新增学生测试:
查询学生测试:
修改学生信息测试:
删除学生信息测试:
###################Michael分割线#####################
这季研究的是使用单独文件实现保存信息的MIS信息管理系统,下季研究使用数据库来保存信息哈~~~
###################Michael分割线#####################
×××
###################Michael分割线#####################