PersonDAOProxyJDBC.java
package org.michael.demo.proxy;    
import java.util.Set;    
import org.michael.demo.dao.PersonDAO;    
import org.michael.demo.dao.impl.PersonDAOImplJDBC;    
import org.michael.demo.vo.Person;    
public class PersonDAOProxyJDBC implements PersonDAO {    
        private PersonDAO dao = null;    
        public PersonDAOProxyJDBC(String type) {    
                this.dao = new PersonDAOImplJDBC(type);    
        }    
        public boolean doCreate(Person person) throws Exception {    
                // TODO Auto-generated method stub    
                return this.dao.doCreate(person);    
        }    
        public boolean doDelete(String id) throws Exception {    
                // TODO Auto-generated method stub    
                return this.dao.doDelete(id);    
        }    
        public boolean doUpdate(Person person) throws Exception {    
                // TODO Auto-generated method stub    
                return this.dao.doUpdate(person);    
        }    
        public Set<Person> findAll() throws Exception {    
                // TODO Auto-generated method stub    
                return this.dao.findAll();    
        }    
        public Person findById(String id) throws Exception {    
                // TODO Auto-generated method stub    
                return this.dao.findById(id);    
        }    
        public Set<Person> findByLike(String keyWord) throws Exception {    
                // TODO Auto-generated method stub    
                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;    
        }    
}
 
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;    
        }    
}
 
Person.java
package org.michael.demo.vo;

import java.io.Serializable;

public abstract class Person implements Comparable<Object>,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;    
        }    
}
 
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;    
        }    
}
 
测试:
image
image
image
image
  ####################Michael分割线###########################
本身显示的效果并不复杂,但是在此程序中重点是体现了一种程序的设计的分层思路,如果可以做的再好,最好把学生和工人的操作分开,分别完成。
建立一个专门的:StudentDAO操作
建立一个专门的:WorkerDAO操作
这样的话通过工厂分别取得两个接口的实例,就可以避免传递参数了。
所有的类要分别按不同的包存放,而且在写代码的时候必须使用考虑如果万一修改了该怎么办?
####################Michael分割线###########################
#################Michael分割线####################