【学校实验】学生管理系统包含外部输入Java

定义学生类

/*
 * 学生类
 */
public class Student {
    //学号
    private String id;
    //姓名
    private String name;
    //年龄
    private String age;
    //家庭住址
    private String address;

    public Student() {

    }

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

    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 getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

主函数

/*
、

集合存储学生信息的代码基础上,增加了两个方法,一个是从学生文件读取信息到集合中的方法,
另一个是从集合中把学生信息写到文件中,再将增删改查方法中形参换成文件路径名的变量功能分析:
        1.主界面(欢迎界面),退出系统
        2.添加学生
        3.查看学生
        4.删除学生
        5.修改学生

        功能实现:
        1.主界面(欢迎界面),退出系统
        1.1.使用打印语句打印信息
        1.2.使用键盘输入选项
        1.3.使用switch进行匹配
        1.4.while

        2.查看学生
        2.1.学生类
        2.2.创建一个ArrayList存储学生
        2.3.搞一批假数据放到集合中
        2.4.查看学生,遍历集合得到每个学生的信息,打印即可

        3.添加学生
        1.使用键盘输入学号
        2.使用键盘输入姓名
        3.使用键盘输入年龄
        4.使用键盘输入居住地
        5.创建学生对象
        6.将学生对象添加到集合中

        4.删除学生
        1.输入要删除学生的学号
        2.遍历集合得到每个学生
        3.判断学号是否相同,如果相同,就记录索引
        4.根据索引删除这个学生

        5.修改学生
        1.输入要修改学生的学号
        2.输入新的姓名
        3.输入新的年龄
        4.输入新的居住地
        5.找到要修改的学生的索引
        6.根据索引修改这个学生

        6.使用外部存储
        */



import java.io.BufferedReader;
        import java.io.BufferedWriter;
        import java.io.FileReader;
        import java.io.FileWriter;
        import java.io.IOException;
        import java.util.ArrayList;
        import java.util.Scanner;


public class StudentManageText {
    public static void main(String[] args) throws IOException {
        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();

        //定义文件路径
        String fileName = "student.txt";

        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.退出");

            //从键盘输入学生信息
            Scanner sc = new Scanner(System.in);

            //选择操作
            System.out.println("请输入您的选择:");
            int choice = sc.nextInt();

            switch(choice){
                case 1:
                    //查看所有学生信息
                    findAllStudent(fileName);
                    break;
                case 2:
                    //添加学生信息
                    addStudent(fileName);
                    break;
                case 3:
                    //删除学生信息
                    removeStudent(fileName);
                    break;
                case 4:
                    //修改学生信息
                    updateStudent(fileName);
                    break;
                case 5:
                    //退出
                    System.out.println("谢谢使用");
                    System.exit(0);//系统退出
                default:
                    System.out.println("您输入的操作有误,请重新输入!");
                    break;
            }
        }
    }

    //查看学生信息的方法
    public static void findAllStudent(String fileName) throws IOException{
        //创建学生类对象
        Student student = new Student();

        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();

        //从文件中读取学生信息到集合中
        readFile(fileName,array);

        if(array.size()==0){
            System.out.println("非常抱歉,当前没有学生信息可供查看!");
        }else{
            System.out.println("学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"家庭住址");
            for(int i=0;i<array.size();i++){
                student = array.get(i);
                System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAddress());

            }
        }
    }

    //添加学生信息的方法
    public static void addStudent(String fileName) throws IOException{
        //创建键盘输入对象
        Scanner sc = new Scanner(System.in);

        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();

        //从文件中读取学生信息到集合中
        readFile(fileName,array);

        String id;
        while(true){
            System.out.println("请输入学生学号:");
            id = sc.nextLine();

            //判断学号是否重复
            boolean flag = false;
            for(int i=0;i<array.size();i++){
                Student student = array.get(i);
                if(student.getId().equals(id)){
                    flag = true;
                    break;
                    //System.out.println("您输入的学号已存在,请重新输入!");
                }
            }
            if(flag){
                System.out.println("您输入的学号已存在,请重新输入!");
            }else{
                break;
            }
        }

        System.out.println("请输入学生姓名:");
        String name = sc.nextLine();

        System.out.println("请输入学生年龄:");
        String age = sc.nextLine();

        System.out.println("请输入学生家庭住址:");
        String address = sc.nextLine();

        //创建学生类对象
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(age);
        student.setAddress(address);

        //添加到集合中
        array.add(student);

        //把集合中学生信息写到文件中
        writeFile(fileName,array);

        System.out.println("添加学生成功!");
    }

    //删除学生信息方法
    public static void removeStudent(String fileName) throws IOException{
        //创建键盘输入对象
        Scanner sc = new Scanner(System.in);

        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();

        //从文件中读取学生信息到集合中
        readFile(fileName,array);

        System.out.println("请输入要删除的学生学号:");
        String id = sc.nextLine();

        int index = -1;

        for(int i=0;i<array.size();i++){
            Student student = array.get(i);
            if(student.getId().equals(id)){
                index = i;
                break;
            }
        }
        if(index==-1){
            System.out.println("非常抱歉,您要删除的学生不存在,请重新输入!");
        }else{
            array.remove(index);
            //把集合中的学生信息写到文件中
            writeFile(fileName,array);
            System.out.println("删除学生成功!");
        }
    }

    //修改学生信息方法
    public static void updateStudent(String fileName) throws IOException{
        //创建键盘输入对象
        Scanner sc = new Scanner(System.in);

        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();

        //从文件中读取学生信息到集合中
        readFile(fileName,array);

        System.out.println("请输入需要修改学生的学号:");
        String id = sc.nextLine();

        int index = -1;
        for(int i=0;i<array.size();i++){
            Student student = array.get(i);
            if(student.getId().equals(id)){
                index = i;
                break;
            }
        }
        if(index==-1){
            System.out.println("非常抱歉,您要修改的学生信息不存在,请重新输入!");
        }else{
            //创建学生类对象
            Student student = new Student();

            System.out.println("请输入新的学生姓名:");
            String name = sc.nextLine();

            System.out.println("请输入新的学生年龄:");
            String age = sc.nextLine();

            System.out.println("请输入新的学生家庭住址:");
            String address = sc.nextLine();

            student.setId(id);
            student.setName(name);
            student.setAge(age);
            student.setAddress(address);

            //修改后的学生信息添加到集合中
            array.set(index,student);

            //把集合中的学生信息写到文件中
            writeFile(fileName,array);

            System.out.println("恭喜修改成功!");
        }
    }

    //从文件读取数据到集合
    public static void readFile(String fileName,ArrayList<Student> array) throws IOException{
        //创建输入缓冲流对象
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);

        String line;
        while((line=br.readLine()) != null){
            String[] datas = line.split(",");
            Student student = new Student();
            student.setId(datas[0]);
            student.setName(datas[1]);
            student.setAge(datas[2]);
            student.setAddress(datas[3]);

            array.add(student);
        }
        //释放资源
        br.close();
    }

    //把集合中的数据写到文件
    public static void writeFile(String fileName,ArrayList<Student> array) throws IOException{
        //创建输出缓冲流
        FileWriter fw = new FileWriter(fileName);
        BufferedWriter bw = new BufferedWriter(fw);

        for(int i=0;i<array.size();i++){
            Student student = array.get(i);
            StringBuilder sb = new StringBuilder();

            sb.append(student.getId()).append(",").append(student.getName()).append(",").append(student.getAge()).append(",").append(student.getAddress());

            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        //释放资源
        bw.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值