日期:2014-05-17 浏览次数:20748 次
package 信息管理系统; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class StudentManageSystem { /** StudentManager中提供的操作菜单 * */ public static void showMenus() { String format = " %s\n"; System.out.println("----------------功能菜单------------------"); System.out.printf(format, "0. 退出管理"); System.out.printf(format, "1. 功能菜单"); System.out.printf(format, "2. 显示学生"); System.out.printf(format, "3. 查询学生"); System.out.printf(format, "4. 添加学生"); System.out.printf(format, "5. 删除学生"); System.out.printf(format, "6. 修改学生"); System.out.printf(format, "7. 读取学生"); System.out.printf(format, "8. 保存学生"); System.out.println("-------------------结束------------------"); System.out.println(); } /** 管理学生信息 * */ public void manageStudents() throws IOException { Scanner scanner = new Scanner(System.in); StudentDao sd = new StudentDao(); StudentService ss = new StudentService(sd, scanner); showMenus(); int op = -1; while (op != 0) { System.in.skip(System.in.available()); System.out.print("输入菜单编号: "); op = scanner.nextInt(); scanner.nextLine(); // 吃掉输入菜单编号后无效的回车字符 try { switch (op) { case 0: break; case 1: showMenus(); break; case 2: ss.showStudents(); break; case 3: ss.findStudents(); break; case 4: ss.addStudent(); break; case 5: ss.removeStudent(); break; case 6: ss.updateStudent(); break; case 7: ss.readStudents(); break; case 8: ss.saveStudents(); break; default: System.out.println("没有提供此项操作\n"); } } catch (Exception e) { System.out.println("\t" + e.getMessage() + "\n"); } } } public static void main(String[] args) throws IOException { StudentManageSystem sm = new StudentManageSystem(); sm.manageStudents(); System.out.println("程序结束"); } } /** Service中的很多功能都是委托给Dao对象去完成 * */ class StudentService { private StudentDao studentDao; private Scanner scanner; public StudentService(StudentDao studentDao, Scanner scanner) { this.studentDao = studentDao; this.scanner = scanner; } // 添加学生 public void addStudent() { System.out.print