Menu.java
package org.michael.demo.menu;
import org.michael.demo.operate.PersonOperate;
import org.michael.demo.operate.StudentOperate;
import org.michael.demo.operate.WorkerOperate;
import org.michael.demo.util.InputData;
public class Menu {
private InputData input = null;
private PersonOperate po = null;
private boolean sflag = true;
private boolean wflag = true;
// 菜单要有增加信息、修改信息等的功能,增加信息的时候要选择是增加学生还是工人
public Menu() {
this.input = new InputData();
while (true) {
this.showMain();
}
}
// 显示主菜单
public void showMain() {
System.out.println(" 学校信息管理程序 ");
System.out.println(" 1、学生信息管理 ");
System.out.println(" 2、工人信息管理 ");
System.out.println(" 3、退出系统 ");
// 要进行选择
System.out.println("\n\n 请选择所要的操作:");
switch (input.getInt()) {
case 1: {
this.sflag = true;
while (sflag) {
this.showStudent();
}
break;
}
case 2: {
this.wflag = true;
while (wflag) {
this.showWorker();
}
break;
}
case 3: {
System.out.println("系统退出,拜拜~~~") ;
System.exit(1);
break;
}
default: {
System.out.println(" 选择错误,请重新选择!");
break;
}
}
}
// 显示学生操作的菜单
public void showStudent() {
this.po = new StudentOperate();
System.out.println(" 学生信息管理 ");
System.out.println(" 1、增加学生信息 ");
System.out.println(" 2、列出全部学生信息 ");
System.out.println(" 3、查询学生信息 ");
System.out.println(" 4、删除学生信息 ");
System.out.println(" 5、修改学生信息 ");
System.out.println(" 6、返回上一级菜单 ");
System.out.println("\n\n请选择具体的操作:");
switch (this.input.getInt()) {
case 1: {
this.po.add();
break;
}
case 2: {
this.po.findAll();
break;
}
case 3: {
this.po.findByLike();
break;
}
case 4: {
this.po.delete();
break;
}
case 5: {
this.po.update();
break;
}
case 6: {
this.sflag = false;
break;
}
default: {
System.out.println(" 选择错误,请重新选择!");
break;
}
}
}
// 显示工人操作的菜单
public void showWorker() {
this.po = new WorkerOperate();
System.out.println(" 工人信息管理 ");
System.out.println(" 1、增加工人信息 ");
System.out.println(" 2、列出全部工人信息 ");
System.out.println(" 3、查询工人信息 ");
System.out.println(" 4、删除工人信息 ");
System.out.println(" 5、修改工人信息 ");
System.out.println(" 6、返回上一级菜单 ");
System.out.println("\n\n请选择具体的操作:");
switch (this.input.getInt()) {
case 1: {
this.po.add();
break;
}
case 2: {
this.po.findAll();
break;
}
case 3: {
this.po.findByLike();
break;
}
case 4: {
this.po.delete();
break;
}
case 5: {
this.po.update();
break;
}
case 6: {
this.wflag = false;
break;
}
default: {
System.out.println(" 选择错误,请重新选择!");
break;
}
}
}
}
import org.michael.demo.operate.PersonOperate;
import org.michael.demo.operate.StudentOperate;
import org.michael.demo.operate.WorkerOperate;
import org.michael.demo.util.InputData;
public class Menu {
private InputData input = null;
private PersonOperate po = null;
private boolean sflag = true;
private boolean wflag = true;
// 菜单要有增加信息、修改信息等的功能,增加信息的时候要选择是增加学生还是工人
public Menu() {
this.input = new InputData();
while (true) {
this.showMain();
}
}
// 显示主菜单
public void showMain() {
System.out.println(" 学校信息管理程序 ");
System.out.println(" 1、学生信息管理 ");
System.out.println(" 2、工人信息管理 ");
System.out.println(" 3、退出系统 ");
// 要进行选择
System.out.println("\n\n 请选择所要的操作:");
switch (input.getInt()) {
case 1: {
this.sflag = true;
while (sflag) {
this.showStudent();
}
break;
}
case 2: {
this.wflag = true;
while (wflag) {
this.showWorker();
}
break;
}
case 3: {
System.out.println("系统退出,拜拜~~~") ;
System.exit(1);
break;
}
default: {
System.out.println(" 选择错误,请重新选择!");
break;
}
}
}
// 显示学生操作的菜单
public void showStudent() {
this.po = new StudentOperate();
System.out.println(" 学生信息管理 ");
System.out.println(" 1、增加学生信息 ");
System.out.println(" 2、列出全部学生信息 ");
System.out.println(" 3、查询学生信息 ");
System.out.println(" 4、删除学生信息 ");
System.out.println(" 5、修改学生信息 ");
System.out.println(" 6、返回上一级菜单 ");
System.out.println("\n\n请选择具体的操作:");
switch (this.input.getInt()) {
case 1: {
this.po.add();
break;
}
case 2: {
this.po.findAll();
break;
}
case 3: {
this.po.findByLike();
break;
}
case 4: {
this.po.delete();
break;
}
case 5: {
this.po.update();
break;
}
case 6: {
this.sflag = false;
break;
}
default: {
System.out.println(" 选择错误,请重新选择!");
break;
}
}
}
// 显示工人操作的菜单
public void showWorker() {
this.po = new WorkerOperate();
System.out.println(" 工人信息管理 ");
System.out.println(" 1、增加工人信息 ");
System.out.println(" 2、列出全部工人信息 ");
System.out.println(" 3、查询工人信息 ");
System.out.println(" 4、删除工人信息 ");
System.out.println(" 5、修改工人信息 ");
System.out.println(" 6、返回上一级菜单 ");
System.out.println("\n\n请选择具体的操作:");
switch (this.input.getInt()) {
case 1: {
this.po.add();
break;
}
case 2: {
this.po.findAll();
break;
}
case 3: {
this.po.findByLike();
break;
}
case 4: {
this.po.delete();
break;
}
case 5: {
this.po.update();
break;
}
case 6: {
this.wflag = false;
break;
}
default: {
System.out.println(" 选择错误,请重新选择!");
break;
}
}
}
}
PersonOperate.java
package org.michael.demo.operate;
public interface PersonOperate {
public void add();
public void delete();
public void update();
public void findAll();
public void findByLike();
}
public interface PersonOperate {
public void add();
public void delete();
public void update();
public void findAll();
public void findByLike();
}
StudentOperate.java
package org.michael.demo.operate;
import java.util.Iterator;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.util.InputData;
import org.michael.demo.util.TimeStamp;
import org.michael.demo.vo.Person;
import org.michael.demo.vo.Student;
import org.michael.demo.factory.*;
public class StudentOperate implements PersonOperate {
private PersonDAO dao = null;
private InputData input = null;
public StudentOperate() {
this.dao = DAOFactory.getPersonDAOInstance("0");
this.input = new InputData();
}
public void add() {
String id = new TimeStamp("s").getTimeStampRandom();
System.out.print("输入学生姓名:");
String name = this.input.getString();
System.out.print("输入学生年龄:");
int age = this.input.getInt();
System.out.print("输入学生成绩:");
float score = this.input.getFloat();
Student s = new Student(id, name, age, score);
try {
this.dao.doCreate(s);
} 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();
Student s = null;
try {
s = (Student) this.dao.findById(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print("输入学生姓名(原姓名:" + s.getName() + "):");
String name = this.input.getString();
System.out.print("输入学生年龄(原年龄:" + s.getAge() + "):");
int age = this.input.getInt();
System.out.print("输入学生成绩(原成绩:" + s.getScore() + "):");
float score = this.input.getFloat();
s.setName(name);
s.setAge(age);
s.setScore(score);
try {
this.dao.doUpdate(s);
} catch (Exception e) {
e.printStackTrace();
}
}
public void findAll() {
Iterator<Person> iter = null;
try {
iter = this.dao.findAll().iterator();
} catch (Exception e) {
// TODO Auto-generated catch block
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<Person> iter = null;
try {
iter = this.dao.findByLike(keyWord).iterator();
} catch (Exception e) {
// TODO Auto-generated catch block
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.util.InputData;
import org.michael.demo.util.TimeStamp;
import org.michael.demo.vo.Person;
import org.michael.demo.vo.Student;
import org.michael.demo.factory.*;
public class StudentOperate implements PersonOperate {
private PersonDAO dao = null;
private InputData input = null;
public StudentOperate() {
this.dao = DAOFactory.getPersonDAOInstance("0");
this.input = new InputData();
}
public void add() {
String id = new TimeStamp("s").getTimeStampRandom();
System.out.print("输入学生姓名:");
String name = this.input.getString();
System.out.print("输入学生年龄:");
int age = this.input.getInt();
System.out.print("输入学生成绩:");
float score = this.input.getFloat();
Student s = new Student(id, name, age, score);
try {
this.dao.doCreate(s);
} 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();
Student s = null;
try {
s = (Student) this.dao.findById(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print("输入学生姓名(原姓名:" + s.getName() + "):");
String name = this.input.getString();
System.out.print("输入学生年龄(原年龄:" + s.getAge() + "):");
int age = this.input.getInt();
System.out.print("输入学生成绩(原成绩:" + s.getScore() + "):");
float score = this.input.getFloat();
s.setName(name);
s.setAge(age);
s.setScore(score);
try {
this.dao.doUpdate(s);
} catch (Exception e) {
e.printStackTrace();
}
}
public void findAll() {
Iterator<Person> iter = null;
try {
iter = this.dao.findAll().iterator();
} catch (Exception e) {
// TODO Auto-generated catch block
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<Person> iter = null;
try {
iter = this.dao.findByLike(keyWord).iterator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ID\t\t\t姓名\t年龄\t成绩");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
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("1");
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<Person> 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<Person> 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("1");
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<Person> 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<Person> 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());
}
}
}
###################Michael分割线###################
###################Michael分割线###################
#################Michael分割线####################
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/145388,如需转载请自行联系原作者