赞
踩
文章目录
◆ 设计 Student 类包含姓名、学号、年龄等属性及其对应的 getter/setter 方法。
◆ 创建 StudentManager 类,用于管理所有学生的增删改查操作。
◆ 使用 java.io.File 和序列化/反序列机制(如 ObjectOutputStream/ObjectInputStream )进行数据持久化存储。
◆ 应用 ArrayList 或 HashMap 收集并管理学生信息,提高数据处理效率。
1.定义类属性字段及其getter/stter方法。可选地提供toString()方法以方便显示学生信息。
- import java.io.Serializable;
- public class Student implements Serializable{
- String id;
- String name;
- int age;
- String classname;
-
- Student(String id,String name,int age,String classname){
- this.id = id;
- this.name = name;
- this.age = age;
- this.classname = classname;
- }
- String getId(){
- return id;
- }
- String getName(){
- return name;
- }
- int getAge(){
- return age;
- }
- String getClassname(){
- return classname;
- }
- void setId(String id){
- this.id = id;
- }
- void setName(String name){
- this.name = name;
- }
- void setAge(int age){
- this.age = age;
- }
- void setClasses(String classname){
- this.classname = classname;
- }
-
- @Override
- public String toString(){
- return "Student{" + "id='" + id + '\'' + ",name='" + name + '\''
- + ",age='" + age + '\'' + ",classname=" + classname+'}';
- }
- }
- import java.io.*;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
-
- public class StudentManager {
- Map<String,Student> studentsId = new HashMap<>();
- // 添加学生信息
- void add(){
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入学号:");
- String id = scanner.next();
-
- System.out.println("请输入姓名:");
- String name = scanner.next();
-
- System.out.println("请输入年龄:");
- int age = scanner.nextInt();
-
- System.out.println("请输入班级:");
- String classname = scanner.next();
-
- Student student = new Student(id,name,age,classname);
- studentsId.put(student.getId(),student);
-
- saveStudents();
- }
- // 删除学生信息
- void remove() {
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入要删除的学号:");
- String id = scanner.nextLine();
- Student student = studentsId.remove(id);
- if (student != null) {
- saveStudents();
- System.out.println("删除成功!");
- } else {
- System.out.println("未找到该学号的信息!");
- }
- }
- // 修改学生信息
- void update() {
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入要修改的学号:");
- String id = scanner.nextLine();
- Student student = studentsId.get(id);
- if (student != null) {
- System.out.println("请输入新的姓名:");
- String newName = scanner.next();
- System.out.println("请输入新的年龄:");
- int newAge = scanner.nextInt();
- System.out.println("请输入新的班级:");
- String newClassname = scanner.next();
-
- student.setName(newName);
- student.setAge(newAge);
- student.setClasses(newClassname);
- saveStudents();
- System.out.println("修改成功!");
- } else {
- System.out.println("未找到该学号的信息!");
- }
- }
- // 查找学生信息
- void find(){
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入要查找的学号:");
- String id = scanner.nextLine();
- Student student = studentsId.get(id);
- if (student != null){
- System.out.println("查找的信息:"+student);
- }
- else{
- System.out.println("未找到该学号的信息!");
- }
- }
- // 显示所有学生信息
- void display() {
- System.out.println("所有学生信息");
- for (Map.Entry<String, Student> entry : studentsId.entrySet()) {
- System.out.println(entry.getValue());
- }
- }
- void loadStudents(){
- try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students.txt"))) {
- @SuppressWarnings("unchecked")
- HashMap<String, Student> loadedStudents = (HashMap<String, Student>) ois.readObject();
- studentsId = loadedStudents;
- } catch (IOException | ClassNotFoundException e){
- e.printStackTrace();
- }
- }
- void saveStudents(){
- try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.txt"))) {
- oos.writeObject(studentsId);
- } catch (IOException e){
- e.printStackTrace();
- }
- }
- public static void main(String[] args){
- File file = new File("students.txt");
- StudentManager manager = new StudentManager();
- manager.loadStudents();
-
- while (true){
- 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("请选择功能序号:");
- int choice = new Scanner(System.in).nextInt();
-
- switch (choice){
- case 1:
- manager.add();
- break;
- case 2:
- manager.remove();
- break;
- case 3:
- manager.update();
- break;
- case 4:
- manager.find();
- break;
- case 5:
- manager.display();
- break;
- case 6:
- System.out.println("退出系统!");
- return;
- default:
- System.out.println("无效序号,请重新选择功能序号!");
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。