基础代码学生管理系统IO版本代码实现

    publicstatic voidmain(String[] args) throws IOException{

        //定义文件路径

        String fileName= "students.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 退出");

            System.out.println("请输入你的选择:");

            //创建键盘录入对象

            Scanner sc= new Scanner(System.in);

            String choiceString= sc.nextLine();

            //switch语句实现选择

            switch(choiceString) {

            case"1":

                //查看所有学生

                findAllStudent(fileName);

                break;

            case"2":

                //添加学生

                addStudent(fileName);

                break;

            case"3":

                //删除学生

                deleteStudent(fileName);

                break;

            case"4":

                //修改学生

                updateStudent(fileName);

                break;

            case"5":

            default:

                System.out.println("谢谢你的使用");

                System.exit(0); //JVM退出

                break;

            }

        }

    }

   

    //从文件中读数据到集合

    publicstatic voidreadData(String fileName,ArrayList<Student>list) throwsIOException {

        //创建输入缓冲流对象

        BufferedReader br= new BufferedReader(new FileReader(fileName));

       

        String line;

        while((line=br.readLine())!=null) {

            String[] datas= line.split(",");

            Student s= new Student();

            s.setId(datas[0]);

            s.setName(datas[1]);

            s.setAge(datas[2]);

            s.setAddress(datas[3]);

            list.add(s);

        }

       

        br.close();

    }

   

    //把集合中的数据写入文件

    publicstatic voidwriteData(String fileName,ArrayList<Student>list) throwsIOException {

        //创建输出缓冲流对象

        BufferedWriter bw= new BufferedWriter(new FileWriter(fileName));

       

        for(int x=0; x<list.size();x++) {

            Student s= list.get(x);

            StringBuilder sb = newStringBuilder();

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

           

            bw.write(sb.toString());

            bw.newLine();

            bw.flush();

        }

       

        bw.close();

    }

   

    //修改学生

    publicstatic voidupdateStudent(String fileName) throws IOException {

        //创建集合对象

        ArrayList<Student> list = newArrayList<Student>();

        //从文件中把数据读取到集合中

        readData(fileName,list);

       

        //修改学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就修改该学生

        //创建键盘录入对象

        Scanner sc= new Scanner(System.in);

        System.out.println("请输入你要修改的学生的学号:");

        String id= sc.nextLine();

       

        //定义一个索引

        intindex = -1;

       

        //遍历集合

        for(int x=0; x<list.size();x++) {

            //获取每一个学生对象

            Student s= list.get(x);

            //拿学生对象的学号和键盘录入的学号进行比较

            if(s.getId().equals(id)){

                index= x;

                break;

            }

        }

       

        if(index == -1) {

            System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新你的选择");

        }else{

            System.out.println("请输入学生新姓名:");

            String name= sc.nextLine();

            System.out.println("请输入学生新年龄:");

            String age= sc.nextLine();

            System.out.println("请输入学生新居住地:");

            String address= sc.nextLine();

           

            //创建学生对象

            Student s= new Student();

            s.setId(id);

            s.setName(name);

            s.setAge(age);

            s.setAddress(address);

           

            //修改集合中的学生对象

            list.set(index, s);

            //把集合中的数据重新写回到文件

            writeData(fileName, list);

            //给出提示

            System.out.println("修改学生成功");

        }

    }

   

    //删除学生

    publicstatic voiddeleteStudent(String fileName) throws IOException {

        //创建集合对象

        ArrayList<Student> list = newArrayList<Student>();

        //从文件中把数据读取到集合中

        readData(fileName,list);

       

        //删除学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就删除该学生

        //创建键盘录入对象

        Scanner sc= new Scanner(System.in);

        System.out.println("请输入你要删除的学生的学号:");

        String id= sc.nextLine();

       

        //我们必须给出学号不存在的时候的提示

       

        //定义一个索引

        intindex = -1;

       

        //遍历集合

        for(int x=0; x<list.size();x++) {

            //获取到每一个学生对象

            Student s= list.get(x);

            //拿这个学生对象的学号和键盘录入的学号进行比较

            if(s.getId().equals(id)){

                index= x;

                break;

            }

        }

       

        if(index == -1) {

            System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新你的选择");

        }else{

            list.remove(index);

            //把集合中的数据重新写回到文件

            writeData(fileName, list);

            System.out.println("删除学生成功");

        }

       

    }

   

    //添加学生

    publicstatic voidaddStudent(String fileName) throws IOException {

        //创建集合对象

        ArrayList<Student> list = newArrayList<Student>();

        //从文件中把数据读取到集合中

        readData(fileName,list);

               

        //创建键盘录入对象

        Scanner sc= new Scanner(System.in);

       

        //为了让id能够被访问到,我们就把id定义在了循环的外面

        String id;

       

        //为了让代码能够回到这里,用循环

        while(true) {

            System.out.println("请输入学生学号:");

            id= sc.nextLine();

           

            //判断学号有没有被人占用

            //定义标记

            booleanflag = false;

            //遍历集合,得到每一个学生

            for(int x=0; x<list.size();x++) {

                Student s= list.get(x);

                //获取该学生的学号,和键盘录入的学号进行比较

                if(s.getId().equals(id)){

                    flag= true; //说明学号被占用了

                    break;

                }

            }

           

            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 s= new Student();

        s.setId(id);

        s.setName(name);

        s.setAge(age);

        s.setAddress(address);

       

        //把学生对象作为元素添加到集合

        list.add(s);

        //把集合中的数据重新写回到文件

        writeData(fileName, list);

       

        //给出提示

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

    }

   

    //查看所有学生

    publicstatic voidfindAllStudent(String fileName) throws IOException {

        //创建集合对象

        ArrayList<Student> list = newArrayList<Student>();

        //从文件中把数据读取到集合中

        readData(fileName,list);

       

        //首先来判断集合中是否有数据,如果没有数据,就给出提示,并让该方法不继续往下执行

        if(list.size() == 0) {

            System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择你的操作");

            return;

        }

       

        //\t 其实就是一个tab键的位置

        System.out.println("学号\t\t姓名\t年龄\t居住地");

        for(int x=0; x<list.size();x++) {

            Student s= list.get(x);

            System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());

        }

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值