学生信息管理系统V0.2(使用文件存储数据)【MVC模式、DAO模式、Factory模式】
2010-05-12 12:39
936 查看
0.2版更新内容:
1.使用文件存储数据;
2.使用HashMap查找学生信息;
3.使用DAO模式;
4.使用Factory模式;
5.使用BufferedReader、PrintWriter读写文件。
Student.java:学生类
StudentAction.java:学生信息处理类
DAOFactory.java:数据操作工厂类
StudentDAO.java:学生信息数据操作接口
StudentDAOImpl.java:学生信息数据操作实现类
StudentMgr.java:学生信息管理类
student.txt:学生数据文件
1.使用文件存储数据;
2.使用HashMap查找学生信息;
3.使用DAO模式;
4.使用Factory模式;
5.使用BufferedReader、PrintWriter读写文件。
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); public void add() { 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); try { DAOFactory.getStudentDAOInstance().add(stu); } catch (Exception e) { System.out.println("添加学生信息失败!"); } } public void showAll() { List<Student> stus = null; try { stus = DAOFactory.getStudentDAOInstance().queryAll(); } catch (Exception e) { System.out.println("信息查找失败!"); } Iterator<Student> iter = stus.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() { System.out.println("请输入学号:"); String id = sc.next(); HashMap<String, Student> stus = null; try { stus = DAOFactory.getStudentDAOInstance().queryById(); } catch (Exception e) { System.out.println("信息查找失败!"); } Student stu = null; if (stus.containsKey(id)) { stu = stus.get(id); } if (stu != null) { this.printHeader(); this.print(stu); this.printFooter(); } else { System.out.println("对不起,没找到您要查找的学生!"); } return stu; } }
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; import java.util.List; public interface StudentDAO { //查询所有学生信息 public List<Student> queryAll() throws Exception; //按ID查询学生信息 public HashMap<String, Student> queryById() throws Exception; //添加学生信息 public void add(Student stu) 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.ArrayList; import java.util.HashMap; import java.util.List; public class StudentDAOImpl implements StudentDAO { public static final String FILE_PATH = "DB/student.txt"; public List<Student> queryAll() throws Exception { List<Student> stus = new ArrayList<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.add(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 HashMap<String, Student> queryById() 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 add(Student stu) throws Exception { FileWriter fw = null; PrintWriter pw = null; try { fw = new FileWriter(FILE_PATH, true); pw = new PrintWriter(fw, true); 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) { showMenu(); int selected = sc.nextInt(); switch(selected) { case 1 : sa.add(); break; case 2 : sa.showAll(); break; case 3 : sa.queryById(); break; case 4 : flag = false; System.out.println("已退出!"); break; } } } public static void showMenu() { System.out.println("学生信息管理菜单:"); System.out.println("(1)增加学生信息"); System.out.println("(2)显示所有学生信息"); System.out.println("(3)根据学号查询学生信息"); System.out.println("(4)退出管理"); } }
student.txt:学生数据文件
1001:Magci:20 1002:MGC:20 1003:haha:24 1005:lala:19 1004:test:23
相关文章推荐
- 学生信息管理系统V0.1(字符界面、内存中存储数据)【MVC模式】
- python下学生管理系统:从文件中读取30位学生的信息(含邮箱),并实现简单的增、删、查找、统计(邮箱使用人数)。---附程序哦!
- C下学生管理系统:从文件中读取30位学生的信息(含邮箱),并实现简单的增、删、查找、统计(邮箱使用人数)。---附程序哦!
- 探讨-信息管理系统中,图像、文件数据存储方式
- 使用单向链表实现学生信息管理系统
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 学生信息管理系统--书签的使用
- C语言学习10:结构体,结构体应用,联合用法,枚举,fopen函数使用,fseek,ftell的作用和文件结束符EOF,数组和文件交换数据,个人信息管理。
- 使用Struts2+Hibernate开发学生信息管理系统
- 数据结构课程设计-学生信息管理系统
- 数据结构课程设计---学生信息管理系统
- 学生信息管理系统 SSH 框架使用及范型使用
- 数据结构 学生信息管理系统
- 学生信息管理系统(三)更新数据时,不正当输入是如何使数据被删除的
- 慕课网_使用Struts2+Hibernate开发学生信息管理系统
- 数据结构课程设计-------学生信息管理系统
- 数据结构课程设计——学生信息管理系统
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。