学生管理系统——IO流


一、思路

需求:增删改查学生信息,退出系统功能,能够将学生信息保存下来。
在这里插入图片描述
Student类,Tset类:对一组学生的增删改查
Student类:学号,姓名,性别,年龄
Tset类:对象数组:Student[] students=new Student[10]固定长度
集合:list,set,map<Integer,Student>(key 学号,value 学生对象)合适
存储:使用IO把学生信息保存到文件,永久保存
对象流,序列化,反序列化

二、代码实践

2.1学生类

package com.students;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: 张乾
 * Date: 2022/4/27
 * Time: 20:27
 * Description: 介绍
 */
public class Student implements Serializable {
    //序列化
    private static final long serialVersionUID = 8200197124745796150L;
    private int id;
    private String  name;
    private int age;
    private String sex;
    public Student() {
    }

    public Student(int id, String name, int age,String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex=sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    //打印学生的方法
    public  void showinfo(){
        System.out.println(this.id+"\t\t"+this.name+"\t\t"+this.sex+"\t\t"+this.age);
    }
}

测试类

package com.students;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: 张乾
 * Date: 2022/4/27
 * Time: 20:30
 * Description: 介绍
 */
public class Test {
    //创建全局变量
    private static Map<Integer,Student> students = new HashMap<>();
    //控制台输入通道
    static Scanner input = new Scanner(System.in);
    /*
    静态代码块
     */
    static {
        //创建文件对象选择读取文件的文件地址与文件名
        File file=new File("D:/AAAAAA/students.txt");
        //文件输入流
        FileInputStream fis=null;
        //对象输入流
        ObjectInputStream ois=null;
        //如果文件对象存在则进行读取文件的操作
        if (file.exists()){
            try {
                //输入流以及对象流
                fis=new FileInputStream(file);
                ois=new ObjectInputStream(fis);
                //将文件的信息写入Map集合
                students  = (Map<Integer,Student>)ois.readObject();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }finally {
                try{
                    //关闭流先开后关原则,不关则浪费资源,不影响运行
                    if(ois != null)ois.close();
                    if(fis != null)fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //如文件不存在则创建文件
        }else {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws IOException {
        //调用菜单
        mean();
    }

    private static void mean() throws IOException {
        System.out.println("************************欢迎进入学生管理系统************************");
        System.out.println("\t\t\t1.显示所有的学生信息");
        System.out.println("\t\t\t2.添加学生");
        System.out.println("\t\t\t3.修改学生信息");
        System.out.println("\t\t\t4.删除学生");
        System.out.println("\t\t\t5.退出");
        System.out.print("请选择:");
        int choose = input.nextInt();
        switch (choose){
            case 1:
                showInfo();
                break;
            case 2:
                addStudent();
                break;
            case 3:
                updatestu();
                break;
            case 4:
                delstudent();
                break;
            case 5:
                System.out.println("欢迎下次使用");
                System.out.println("正在退出.....");
                writes();
                System.exit(1);
        }
    }

    /**
     * 最后写入文本文档
     */
    private static void writes() throws IOException {
        //文件输出流
        FileOutputStream fos=null;
        //对象输出流
        ObjectOutputStream oos=null;
        try{
            //创建输出流,对象流
            //将信息写入的文件
            fos=new FileOutputStream("D:/AAAAAA/students.txt");
            oos=new ObjectOutputStream(fos);
            oos.writeObject(students);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭
            if (oos!=null)oos.close();
            if (fos!=null)fos.close();
        }
    }

    /**
     * 删除学生的方法
     */
    private static void delstudent() throws IOException {
        System.out.println("请输入你要删除的学生学号");
        int id = input.nextInt();
        if (students.containsKey(id)){
            students.remove(id);
            System.out.println("删除成功");
            mean();
        }else {
            System.out.println("学号不存在");
            mean();
        }
    }

    /**
     * 添加修改的方法
     */
    private static void updatestu() throws IOException {
        System.out.println("输入你要修改的学号");
        int id = input.nextInt();
        if (students.containsKey(id)){
            System.out.println("请输入姓名:");
            String name = input.next();
            System.out.println("请选择性别: 1.男  2.女");
            String sex ="男";
            int sexInt = input.nextInt();
            if(sexInt == 2){
                sex = "女";
            }
            System.out.println("请输入年龄:");
            int age = input.nextInt();
            //创建一个Student对象
            Student stu = new Student(id,name,age,sex);
            //把学生对象保存到Map集合
            students.put(id,stu);
            System.out.println("修改成功!!!");
            mean();
        }else {
            System.out.println("学号不存在");
            updatestu();
        }
    }

    /**
     * 添加学生的方法
     */
    private static void addStudent() throws IOException {
        System.out.println("请输入学号");
        int id = input.nextInt();
        //Map的键值是否存在
        if(students.containsKey(id)){
            System.out.println("该学号已经存在");
        }else {
            System.out.println("请输入姓名:");
            String name = input.next();
            System.out.println("请选择性别: 1.男  2.女");
            String sex ="男";
            int sexInt = input.nextInt();
            if(sexInt == 2){
                sex = "女";
            }

            System.out.println("请输入年龄:");
            int age = input.nextInt();

            //创建一个Student对象
            Student stu = new Student(id,name,age,sex);
            //把学生对象保存到Map集合
            students.put(id,stu);
            System.out.println("添加成功!!!");

        }
        mean();
    }

    /**
     * 显示所有学生
     */
    private static void showInfo() throws IOException {
        Set<Map.Entry<Integer, Student>> entries = students.entrySet();
        System.out.println("学号\t姓名\t性别\t年龄");
        for (Map.Entry<Integer, Student> entry : entries) {
            Student student = entry.getValue();
            student.showinfo();
        }
        //菜单循环
        mean();
    }
}

三、运行结果

3.1显示学生信息

在这里插入图片描述

3.2添加学生信息

在这里插入图片描述
重复添加则添加失败
在这里插入图片描述

3.3修改学生信息

选择修改的学生学号
在这里插入图片描述
修改成功后再显示
在这里插入图片描述

3.4删除学生

在这里插入图片描述
删除之后显示学生
在这里插入图片描述
假如已经删除的学生再删除则会显示该学号已经不存在
在这里插入图片描述

3.5退出系统

退出系统将信息写入文件
在这里插入图片描述

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值