java控制台实现学生信息管理系统(IO版)
作者:没事多喝水w
这篇文章主要为大家详细介绍了java控制台实现学生信息管理系统(IO版),文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
使用java语言用本地文件存储数据实现学生信息管理系统,在控制台上编译执行,也就是学生管理系统IO版
可以实现基本的学生信息增加、删除、修改、查询功能(细化了查询功能)
集合版可以参考我的另外一篇博文。
代码如下
StudentManager
提供用户界面
import java.io.IOException; import java.util.Scanner; public class StudentManager { public static void main(String[] args) throws IOException, ClassNotFoundException { while (true) { Scanner sc = new Scanner(System.in); System.out.println(); System.out.println(); System.out.println("================================="); System.out.println("=========欢迎使用学生管理系统 ========="); 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("=================================="); System.out.println("请输入您的选择"); int choose; if (sc.hasNextInt()) { choose = sc.nextInt(); if (choose > 0 && choose < 6) { StudentDAO std = new StudentDAO(); switch (choose) { case 1: std.add(); break; case 2: std.del(); break; case 3: std.update(); break; case 4: std.query(); break; case 5: System.out.println("成功退出……"); System.out.println("欢迎下次使用"); System.exit(0); break; } } else { System.out.println("请正确输入"); } } else { System.out.println("请您输入正确的选项"); } } } }
Student类
public class Student implements Serializable{ private static final long serialVersionUID = 3420928184417313845L; private String stuNo; private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String stuNo, String name, int age) { super(); this.stuNo = stuNo; this.name = name; this.age = age; } public String getStuNo() { return stuNo; } public void setStuNo(String stuNo) { this.stuNo = stuNo; } 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; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((stuNo == null) ? 0 : stuNo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (stuNo == null) { if (other.stuNo != null) return false; } else if (!stuNo.equals(other.stuNo)) return false; return true; } @Override public String toString() { return "学生:学号 " + stuNo + ", 姓名 " + name + ", 年龄 " + age ; } }
IOUtil类
用于从文件中读写信息到集合
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; public class IOUtil { //读取文件信息 public static ArrayList<Student> readStu() { ArrayList<Student> stuList = null; File file = new File("test.txt"); if (!file.exists()) { try { file.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } try ( ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); ) { stuList = (ArrayList<Student>) ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return stuList; } //写入文件信息 public static void writeStu(ArrayList<Student> stu) { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("test.txt"))); ) { oos.writeObject(stu); } catch (Exception e) { e.printStackTrace(); } } }
StudentDAO
用于执行数据的增删改查操作
import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; import java.util.Scanner; public class StudentDAO { static Scanner sc = new Scanner(System.in); // 增加学生信息 public void add() throws IOException, ClassNotFoundException { ArrayList<Student> list = IOUtil.readStu(); // 学号自增 String stuNo = null; a: while (true) { // 如果不存在学生则学号为1000 if (list.size() == 0) { stuNo = "1000"; } else { // 创建新的学号,让新的学号在列表中的最后一个学生学号的基础上加1 int sNo = Integer.parseInt(list.get(list.size() - 1).getStuNo()) + 1; // 将学号类型转为String类型 stuNo = String.valueOf(sNo); } System.out.println("请输入你要添加的学号为" + stuNo + "的学生的姓名"); String name = sc.next(); while (true) { System.out.println("请输入你要添加的学号为" + stuNo + "的学生的年龄"); Scanner sc1 = new Scanner(System.in); if (sc1.hasNextInt()) { int age = sc1.nextInt(); list.add(new Student(stuNo, name, age)); IOUtil.writeStu(list); break; } else { System.out.println("请正确输入"); continue a; } } System.out.println("学号为" + stuNo + "的学生信息添加成功"); System.out.println("添加信息后,学生的信息为:"); showStudentInformation(); System.out.println("是否继续执行添加操作(y/n)"); String result = sc.next(); if (result.equalsIgnoreCase("n") || result.equalsIgnoreCase("y")) { if (result.equalsIgnoreCase("n")) { break; } } else { } } } // 删除学生信息 public void del() throws IOException, ClassNotFoundException { ArrayList<Student> list = IOUtil.readStu(); if (list.size() != 0) { a: while (true) { showStudentInformation(); System.out.println("请输出你要删除的学生的学号"); String str = sc.next(); Iterator<Student> it = list.iterator(); while (it.hasNext()) { Student stu = it.next(); if (stu.getStuNo().equals(str)) { it.remove(); System.out.println("删除成功!"); System.out.println("删除操作后,学生的信息为:"); IOUtil.writeStu(list); showStudentInformation(); break a;// 跳出到指定循环外 } } System.out.println("查无此人……请查证后重新输入"); } } else { System.out.println("还没有添加学生信息,快去添加学生信息吧"); } } // 修改学生信息 public void update() { ArrayList<Student> list = IOUtil.readStu(); if (list.size() != 0) { a: while (true) { showStudentInformation(); System.out.println("请输入要修改学生的学号:"); String stuNo = sc.next(); ListIterator<Student> lit = list.listIterator(); while (lit.hasNext()) { Student stu = lit.next(); if (stu.getStuNo().equals(stuNo)) { System.out.println("请输入您要修改的学生的姓名"); String name = sc.next(); System.out.println("请输入您要修改的学生的年龄"); if (sc.hasNextInt()) { int age = sc.nextInt(); stu.setName(name); stu.setAge(age); System.out.println("修改操作后,学生的信息为:"); IOUtil.writeStu(list); showStudentInformation(); break a; } else { System.out.println("请正确输入年龄"); } } } System.out.println("查无此人……请查证后重新输入"); } } else { System.out.println("还没有添加学生信息,快去添加学生信息吧"); } } // 显示学生信息 public void showStudentInformation() { ArrayList<Student> list = IOUtil.readStu(); if (list.size() != 0) { System.out.println("=============学生信息=============="); for (Student stu : list) { System.out.println(stu); } System.out.println("================================="); } else { System.out.println("还没有添加学生信息,快去添加学生信息吧"); } } // 查询学生信息 public void query() { ArrayList<Student> list = IOUtil.readStu(); if (list.size() != 0) { int choose = 0; while (true) { System.out.println("请选择您的查询条件:1.查询全部 2.学号 3.姓名 4.年龄"); System.out.println("请输入您的选择"); try { choose = sc.nextInt(); break; } catch (Exception e) { System.out.println("请重新输入选择"); sc = new Scanner(System.in); } } switch (choose) { case 1: System.out.println("=============学生信息=============="); for (Student stu : list) { System.out.println(stu); } System.out.println("================================="); break; case 2: { queryStuNo(list); break; } case 3: queryStuName(list); break; case 4: queryStuAge(list); break; default: System.out.println("查询条件不正确,正在退出查询……"); break; } } else { System.out.println("还没有添加学生信息,快去添加学生信息吧"); } } // 按学号查询 public static void queryStuNo(ArrayList<Student> list) { a: while (true) { System.out.println("请输入您要查询的学号"); String stuNo = sc.next(); for (Student student : list) { if ((student.getStuNo()).equals(stuNo)) { System.out.println(student); break a; } } System.out.println("查无此人……请查证后重新输入"); } } // 按姓名查询 public static void queryStuName(ArrayList<Student> list) { while (true) { ArrayList<Student> list1 = new ArrayList<>(); System.out.println("请输入您要查询的姓名"); String stuName = sc.next(); for (Student student : list) { if ((student.getName()).equals(stuName)) { list1.add(student); } } if (list1.size() == 0) { System.out.println("查无此人……请查证后重新输入"); } else { // 遍历集合 for (Student student : list1) { System.out.println(student); } break; } } } // 按年龄查询 public static void queryStuAge(ArrayList<Student> list) { while (true) { ArrayList<Student> list1 = new ArrayList<>(); int age; System.out.println("请输入您要查询的年龄"); while (true) { try { age = sc.nextInt(); break; } catch (Exception e) { System.out.println("请重新输入年龄"); sc = new Scanner(System.in); } } for (Student student : list) { if (student.getAge() == age) { list1.add(student); } } if (list1.size() == 0) { System.out.println("没有此年龄的人,请重新查找"); } else { // 遍历集合 for (Student student : list1) { System.out.println(student); } break; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。