学生信息管理系统V0.3(优化文件存取、增加修改删除功能)

0.3版更新内容:
1.程序启动时从文件中加载学生信息到HashMap中;
2.所有操作都在内存中进行,操作完成后写入文件;
3.增加修改、删除功能。

Student.java:学生类

package cn.edu.ahau.mgc.stu; public class Student { private String id; private String name; private int 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; } }

StudentAction.java:学生信息处理类

package cn.edu.ahau.mgc.stu; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class StudentAction { Scanner sc = new Scanner(System.in); private HashMap<String, Student> stus; public void init() { try { this.stus = DAOFactory.getStudentDAOInstance().load(); } catch (Exception e) { System.out.println("信息初始化失败!"); } } public void add(Student stu) { this.stus.put(stu.getId(), stu); try { DAOFactory.getStudentDAOInstance().save(stus); } catch (Exception e) { System.out.println("学生信息保存失败!"); } } public void showAll() { Iterator<Student> iter = this.stus.values().iterator(); this.printHeader(); while (iter.hasNext()) { Student stu = iter.next(); this.print(stu); } this.printFooter(); } public void printHeader() { System.out.println("------------------------"); System.out.println("学号/t姓名/t年龄"); System.out.println("------------------------"); } public void printFooter() { System.out.println("------------------------"); } public void print(Student stu) { System.out.println(stu.getId() + "/t" + stu.getName() + "/t" + stu.getAge()); } public Student queryById(String id) { Student stu = null; if (stus.containsKey(id)) { stu = this.stus.get(id); } if (stu != null) { this.printHeader(); this.print(stu); this.printFooter(); } else { System.out.println("对不起,没找到您要查找的学生!"); } return stu; } public void delete(String id) { stus.remove(id); try { DAOFactory.getStudentDAOInstance().save(stus); } catch (Exception e) { System.out.println("学生信息保存失败!"); } } public void modify(Student stu) { stus.put(stu.getId(), stu); try { DAOFactory.getStudentDAOInstance().save(stus); } catch (Exception e) { System.out.println("学生信息保存失败!"); } } }

 

DAOFactory.java:数据操作工厂类
package cn.edu.ahau.mgc.stu; public class DAOFactory { public static StudentDAO getStudentDAOInstance() { return new StudentDAOImpl(); } }

 

StudentDAO.java:学生信息数据操作接口

package cn.edu.ahau.mgc.stu; import java.util.HashMap; public interface StudentDAO { //从文件中读取学生信息 public HashMap<String, Student> load() throws Exception; //将学生信息写入文件 public void save(HashMap<String, Student> stus) throws Exception; }

StudentDAOImpl.java:学生信息数据操作实现类

package cn.edu.ahau.mgc.stu; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Iterator; public class StudentDAOImpl implements StudentDAO { public static final String FILE_PATH = "DB/student.txt"; public HashMap<String, Student> load() throws Exception { HashMap<String, Student> stus = new HashMap<String, Student>(); FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(FILE_PATH); br = new BufferedReader(fr); String stuStr = null; while ((stuStr = br.readLine()) != null) { String[] stuMsg = stuStr.split(":"); if (stuMsg.length == 3) { Student stu = new Student(); stu.setId(stuMsg[0]); stu.setName(stuMsg[1]); int age = 20; try { age = Integer.parseInt(stuMsg[2]); } catch (NumberFormatException e) { age = 20; } stu.setAge(age); stus.put(stuMsg[0], stu); } } } catch (FileNotFoundException e) { e.printStackTrace(); throw new Exception(e); } catch (IOException e) { e.printStackTrace(); throw new Exception(e); } finally { if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); throw new Exception(e); } br = null; } if (fr != null) { try { fr.close(); } catch (Exception e) { e.printStackTrace(); throw new Exception(e); } fr = null; } } return stus; } public void save(HashMap<String, Student> stus) throws Exception { FileWriter fw = null; PrintWriter pw = null; try { fw = new FileWriter(FILE_PATH); pw = new PrintWriter(fw, true); for (Iterator iterator = stus.values().iterator(); iterator.hasNext();) { Student stu = (Student) iterator.next(); pw.println(stu.getId() + ":" + stu.getName() + ":" + stu.getAge()); } } catch (IOException e) { e.printStackTrace(); } finally { if (pw != null) { pw.close(); pw = null; } if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); throw new Exception(e); } fw = null; } } } }

StudentMgr.java:学生信息管理类

package cn.edu.ahau.mgc.stu; import java.util.Scanner; public class StudentMgr { public static void main(String[] args) { StudentAction sa = new StudentAction(); Scanner sc = new Scanner(System.in); boolean flag = true; while (flag) { sa.init(); showMenu(); int selected = sc.nextInt(); switch(selected) { case 1 : add(sa, sc); break; case 2 : sa.showAll(); break; case 3 : query(sa, sc); break; case 4 : delete(sa, sc); break; case 5 : modify(sa, sc); break; case 6 : flag = false; break; } } } private static void modify(StudentAction sa, Scanner sc) { System.out.println("请输入要修改的学生学号:"); String id = sc.next(); System.out.println("请重新输入该学生姓名:"); String name = sc.next(); System.out.println("请重新输入该学生年龄:"); int age = sc.nextInt(); Student stu = new Student(); stu.setId(id); stu.setName(name); stu.setAge(age); sa.modify(stu); } private static void delete(StudentAction sa, Scanner sc) { System.out.println("请输入要删除的学生学号:"); String id = sc.next(); sa.delete(id); } private static void query(StudentAction sa, Scanner sc) { System.out.println("请输入学号:"); String id = sc.next(); sa.queryById(id); } private static void add(StudentAction sa, Scanner sc) { Student stu = new Student(); System.out.println("请输入学生学号:"); String id = sc.next(); System.out.println("请输入学生姓名:"); String name = sc.next(); System.out.println("请输入学生年龄:"); int age = sc.nextInt(); stu.setId(id); stu.setName(name); stu.setAge(age); sa.add(stu); } private static void showMenu() { 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)退出管理"); } }

student.txt:学生数据文件
1001:Magci:20 1002:MGC:20 1003:haha:24 1005:lala:19 1004:test:23

你可能感兴趣的:(exception,优化,String,null,iterator,Class)