IO流实现学生信息管理系统-JAVA

本系统通过IO实现对学生基本信息和语数英成绩的增删改查
创建文件存放信息
a.txt
(这里是按照学号,姓名,性别,年龄,语文,数学,英语排列的)

1 李四 女 18 60 60 60
2 张三 男 17 60 60 60
4 王五 男 16 60 60 60
5 赵七 女 20 60 60 60
3 一三 男 15 59 59 59

Student类

package demo_03;

public class Student {
    private String id;     //学号
    private String name;   //姓名
    private String sex;    //性别
    private int age;       //年龄
    //默认初始值为0
    private int chineseScores=0;   //语文成绩
    private int mathScores=0;    //数学成绩
    private int englishScores=0;   //英语成绩

    public int getChineseScores() {
        return chineseScores;
    }

    public void setChineseScores(int chineseScores) {
        this.chineseScores = chineseScores;
    }

    public int getMathScores() {
        return mathScores;
    }

    public void setMathScores(int mathScores) {
        this.mathScores = mathScores;
    }

    public int getEnglishScores() {
        return englishScores;
    }

    public void setEnglishScores(int englishScores) {
        this.englishScores = englishScores;
    }

    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 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;
    }
}


StudentDemo实现类

package demo_03;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentDemo {
    //文件存放路径
    private String path = "d:/Java/新建文本文件/a.txt";
    //判断是否退出系统
    private static boolean pd=true;

    public static void main(String[] args) throws IOException {
        while (pd) {
            StudentDemo studentDemo = new StudentDemo();
            //功能提示信息
            studentDemo.Tips();
            //功能选择
            studentDemo.function(studentDemo);
            //循环间隔
            System.out.println("");
        }
    }

    //功能提示
    public void Tips() {
        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("请选择功能");
    }

    //功能选择
    public void function(StudentDemo studentDemo) throws IOException {
        switch (studentDemo.scannerInt()) {
            //查询单个学生信息
            case 1:
                studentDemo.findstudent(studentDemo.path);
                break;
            //显示所有信息
            case 2:
                studentDemo.showstudent(studentDemo.path);
                break;
            //添加学生信息
            case 3:
                studentDemo.addStudent(studentDemo.path);
                break;
            //更新学生信息
            case 4:
                studentDemo.updatestudent(studentDemo.path);
                break;
            //删除学生信息
            case 5:
                studentDemo.deletestudent(studentDemo.path);
                break;
            //退出
            case 6:
                pd=false;
                break;
            //输入无效
            default: {
                System.out.println("请重新输入");
                //再次输入
                function(studentDemo);
                break;
            }

        }
    }

    //输入功能
    public int scannerInt() {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        return x;
    }

    //输入学生信息
    public String scannerString() {
        Scanner scanner = new Scanner(System.in);
        String x = scanner.next().toString();
        return x;
    }


    /*
    读数据  利用ArrayList动态数组

     */

    //显示所有学生信息
    void showstudent(String path) throws IOException {
        //读数据到集合中
        ArrayList<Student> studentArrList = new ArrayList<Student>();
        //0是判断
        readData("0", path, studentArrList);
        if (studentArrList.size() == 0) {
            System.out.println("无可查询信息");
            return;
        }
        print(studentArrList);
    }

    //输出信息
    static void print(ArrayList<Student> studentArrList) {
        System.out.println("学号  姓名  性别  年龄  语文  数学  英语");
        for (int i = 0; i < studentArrList.size(); i++) {
            Student student = studentArrList.get(i);
            System.out.println(student.getId() + "     " + student.getName() + "    " + student.getSex() + "    " + student.getAge()
                    + "     " + student.getChineseScores() + "     " + student.getMathScores() + "     " + student.getEnglishScores());
        }
    }

    //查询学生信息
    private void findstudent(String path) throws IOException {
        System.out.println("请输入需要查找的信息(学号/姓名/年龄/某一课的成绩)");
        //获取需要查找的信息
        String student = scannerString();
        ArrayList<Student> studentArrList = new ArrayList<Student>();
        //传入非0数据
        readData(student, path, studentArrList);
        if (studentArrList.size() == 0) {
            System.out.println("查无此人");
            return;
        }
        print(studentArrList);
    }

    //保存对象信息
    public static Student student(String[] str) {
        Student student = new Student();
        student.setId(str[0]);
        student.setName(str[1]);
        student.setSex(str[2]);
        student.setAge(Integer.parseInt(str[3]));
        student.setChineseScores(Integer.parseInt(str[4]));
        student.setEnglishScores(Integer.parseInt(str[5]));
        student.setMathScores(Integer.parseInt(str[6]));
        return student;
    }

    //将文件读入集合
    static void readData(String onlystudnet, String path, ArrayList<Student> studentArrayList) throws IOException {
        //利用处理流提高性能
        BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            //获取本行数据,文本文件用空格分隔数据,将数据保存在数组当中
            String[] str = line.split(" ");
            //此处使用onlystudent,是为了匹配查找学生时所输入的信息(见findstudent()方法)
            //使用默认的"0"只是为了减少代码的重复性
            if (onlystudnet.equals("0"))
                studentArrayList.add(student(str));
                //匹配学生数据
            else if (onlystudnet.equals(str[0]) || onlystudnet.equals(str[1])
                    || onlystudnet.equals(str[2]) || onlystudnet.equals(str[3])
                    || onlystudnet.equals(str[4]) || onlystudnet.equals(str[5])
                    || onlystudnet.equals(str[6]))
                studentArrayList.add(student(str));
        }
        //记得关闭流
        bufferedReader.close();
    }

    //将集合写入文件
    public void writeData(String path, ArrayList<Student> studentArrayList) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
        for (int i = 0; i < studentArrayList.size(); i++) {
            //获取对象
            Student student = studentArrayList.get(i);
            //使用StringBuilder提高效率
            StringBuilder stringBuilder = new StringBuilder();
            //获取并保存数据
            stringBuilder.append(student.getId()).append(" ").append(student.getName()).append(" ")
                    .append(student.getSex()).append(" ").append(student.getAge()).append(" ").append(student.getChineseScores())
                    .append(" ").append(student.getMathScores()).append(" ").append(student.getEnglishScores());
            //写入
            bufferedWriter.write(stringBuilder.toString());
            //换行
            bufferedWriter.newLine();
            //强制刷新
            bufferedWriter.flush();
        }
    }

    //增添学生信息
    public void addStudent(String path) throws IOException {
        ArrayList<Student> studentArrayList = new ArrayList<Student>();
        readData("0", path, studentArrayList);
        boolean flag = false;
        String id;
        while (true) {
            System.out.println("请输入需要添加的学号");
            id = scannerString();
            //判断用户是否已经存在
            for (int i = 0; i < studentArrayList.size(); i++) {
                Student s = studentArrayList.get(i);
                if (s.getId().equals(id)) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                System.out.println("用户已存在,请重新操作");
            } else
                break;
        }
        System.out.println("请输入需要添加的学生姓名");
        String name = scannerString();
        System.out.println("请输入需要添加的学生性别");
        String sex = scannerString();
        System.out.println("请输入需要添加的学生年龄");
        int age = scannerInt();
        System.out.println("请输入需要添加的学生语文成绩");
        int chineseScore = scannerInt();
        System.out.println("请输入需要添加的学生数学成绩");
        int mathScore = scannerInt();
        System.out.println("请输入需要添加的学生英语成绩");
        int englishScore = scannerInt();
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setSex(sex);
        student.setAge(age);
        student.setChineseScores(chineseScore);
        student.setMathScores(mathScore);
        student.setEnglishScores(englishScore);
        studentArrayList.add(student);
        writeData(path, studentArrayList);
        System.out.println("添加成功");
    }

    //删除学生信息
    private void deletestudent(String path) throws IOException {
        ArrayList<Student> studentArrayList = new ArrayList<Student>();
        readData("0", path, studentArrayList);
        System.out.println("请输入要删除的学生信息(学号/姓名)");
        String id_name = scannerString();
        Boolean flag = false;
        int x = 0;
        for (int i = 0; i < studentArrayList.size(); i++) {
            if (id_name.equals(studentArrayList.get(i).getId()) || id_name.equals(studentArrayList.get(i).getName())) {
                flag = true;
                x = i;
                break;
            }
        }
        if (flag) {
            studentArrayList.remove(x);
            //千万别忘记在写入覆盖
            writeData(path, studentArrayList);
            System.out.println("删除完毕");
        } else
            System.out.println("查无此人,请重新输入");
    }

    //修改学生信息
    private void updatestudent(String path) throws IOException {
        System.out.println("请输入需要修改的学生学号");
        String s = scannerString();
        ArrayList<Student> studentArrayList = new ArrayList<Student>();
        readData("0", path, studentArrayList);
        for (int i = 0; i < studentArrayList.size(); i++) {
            System.out.println(studentArrayList.get(i).getId());
            if (s.equals(studentArrayList.get(i).getId())) {
                System.out.println("请输入需要修改后的学生姓名");
                String name = scannerString();
                System.out.println("请输入需要修改后的学生性别");
                String sex = scannerString();
                System.out.println("请输入需要修改后的学生年龄");
                int age = scannerInt();
                System.out.println("请输入需要添加的学生语文成绩");
                int chineseScore = scannerInt();
                System.out.println("请输入需要添加的学生数学成绩");
                int mathScore = scannerInt();
                System.out.println("请输入需要添加的学生英语成绩");
                int englishScore = scannerInt();
                studentArrayList.get(i).setId(s);
                studentArrayList.get(i).setName(name);
                studentArrayList.get(i).setSex(sex);
                studentArrayList.get(i).setAge(age);
                studentArrayList.get(i).setChineseScores(chineseScore);
                studentArrayList.get(i).setMathScores(mathScore);
                studentArrayList.get(i).setEnglishScores(englishScore);
            }
            else if(studentArrayList.size()==i) {
                System.out.println("查无此人");
                return;
            }
        }
        writeData(path, studentArrayList);
        System.out.println("修改完成");
    }
}


运行结果:
在这里插入图片描述

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值