学生信息管理系统

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

public class Demo {
    public static void main(String[] args) throws IOException{
        menu();
    }
    public static void menu()throws IOException{
        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);
        int n= sc.nextInt();
        while (true){
            switch (n){
                case 1:
                    addStudent();
                    System.out.println("1.增加学生");
                    System.out.println("2.删除学生");
                    System.out.println("3.查看学生信息");
                    System.out.println("4.修改学生信息");
                    System.out.println("5.退出");
                    System.out.println("请输入你选择序号");
                    break;
                case 2:
                    deleteStudent();
                    System.out.println("1.增加学生");
                    System.out.println("2.删除学生");
                    System.out.println("3.查看学生信息");
                    System.out.println("4.修改学生信息");
                    System.out.println("5.退出");
                    System.out.println("请输入你选择序号");
                    break;
                case 3:
                    chaKanStudent();
                    System.out.println("1.增加学生");
                    System.out.println("2.删除学生");
                    System.out.println("3.查看学生信息");
                    System.out.println("4.修改学生信息");
                    System.out.println("5.退出");
                    System.out.println("请输入你选择序号");
                    break;
                case 4:
                    xiuGaiStudent();
                    System.out.println("1.增加学生");
                    System.out.println("2.删除学生");
                    System.out.println("3.查看学生信息");
                    System.out.println("4.修改学生信息");
                    System.out.println("5.退出");
                    System.out.println("请输入你选择序号");
                    break;
                case 5:
                    System.out.println("谢谢使用学生管理系统!");
                    System.exit(-1);
                default:
                    System.out.println("请输入正确的序号");
                    System.out.println("序号的范围是:1~5");
                    System.out.println("1.增加学生");
                    System.out.println("2.删除学生");
                    System.out.println("3.查看学生信息");
                    System.out.println("4.修改学生信息");
                    System.out.println("5.退出");
                    System.out.println("请输入你选择序号");
                    break;
            }
            n= sc.nextInt();
        }
    }

    //增加学生信息
    public static void addStudent()throws IOException {
        //创建字符缓冲输出流,写数据
        Scanner sc1=new Scanner(System.in);
        Scanner sc2=new Scanner(System.in);
        Student student=new Student();
        System.out.println("请输入新增加的学生的姓名:");
        student.setName(sc2.nextLine());
        System.out.println("请输入新增加的学生的年龄:");
        student.setAge(sc1.nextInt());
        System.out.println("请输入新增加的学生的学号:");
        student.setNumber(sc2.nextLine());
        System.out.println("请输入新增加的学生的家庭住址:");
        student.setAddress(sc2.nextLine());
        System.out.println("请输入新增加的学生的电话:");
        student.setTelePhone(sc2.nextLine());
        BufferedWriter bw=new BufferedWriter(new FileWriter("书本的课后习题\\xinXi1.txt",true));//有学生姓名的文件夹,把学生信息写入的文件夹
        bw.write(student.getName()+","+student.getAge()+","+student.getNumber()+","+student.getAddress()+","+student.getTelePhone());
        bw.newLine();
        bw.flush();
        bw.close();
        System.out.println("学生添加成功!");
    }

    //删除学生信息
    public static void deleteStudent()throws IOException {
        //在数据中查找知否有该数据

        Scanner sc1 = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        System.out.println("请输入删除的学生的姓名:");
        String name = sc2.nextLine();
        System.out.println("请输入删除的学生的年龄:");
        int age = sc1.nextInt();
        System.out.println("请输入删除的学生的学号:");
        String number = sc2.nextLine();
        System.out.println("请输入删除的学生的家庭住址:");
        String address = sc2.nextLine();
        System.out.println("请输入删除的学生的电话:");
        String telePhone = sc2.nextLine();

        BufferedReader br = new BufferedReader(new FileReader("书本的课后习题\\xinXi1.txt"));
        int n = 1;
        String line;
        ArrayList<Student> arrayList = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            Student s=new Student();
            String[] studentList = line.split(",");
            s.setName(studentList[0]);
            s.setAge(Integer.parseInt(studentList[1]));
            s.setNumber(studentList[2]);
            s.setAddress(studentList[3]);
            s.setTelePhone(studentList[4]);
            if ((studentList[0].equals(name)) && (Integer.parseInt(studentList[1]) == age)
                    && (studentList[2].equals(number)) && (studentList[3].equals(address))
                    && (studentList[4].equals(telePhone))) {
                n=0;
            }else {
                arrayList.add(s);
            }
        }
        if(n==1){
            System.out.println("请检查你所输入的信息是否有误!");
            System.out.println("删除失败!");
        }else {
            System.out.println("删除成功!");
        }
        br.close();
        BufferedWriter bw=new BufferedWriter(new FileWriter("书本的课后习题\\xinXi1.txt"));
        for(Student student:arrayList){
            bw.write(student.getName()+","+student.getAge()+","+student.getNumber()+","+student.getAddress()+","+student.getTelePhone());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }


    //修改学生信息
    public static void xiuGaiStudent()throws IOException{
        Scanner sc1 = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        Student s=new Student();
        System.out.println("请输入要修改的学生的姓名:");
        s.setName(sc2.nextLine());
        System.out.println("请输入要修改的学生的年龄:");
        s.setAge(sc1.nextInt());
        System.out.println("请输入要修改的学生的学号:");
        s.setNumber(sc2.nextLine());
        System.out.println("请输入要修改的学生的家庭住址:");
        s.setAddress(sc2.nextLine());
        System.out.println("请输入要修改的学生的电话:");
        s.setTelePhone(sc2.nextLine());

        ArrayList<Student>arrayList=new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("书本的课后习题\\xinXi1.txt"));
        String line;
        int n=1;
        while ((line = br.readLine()) != null) {
            String[] studentList = line.split(",");
            Student s1=new Student();
            s1.setName(studentList[0]);
            s1.setAge(Integer.parseInt(studentList[1]));
            s1.setNumber(studentList[2]);
            s1.setAddress(studentList[3]);
            s1.setTelePhone(studentList[4]);
            if ((studentList[0].equals(s.getName())) && (Integer.parseInt(studentList[1]) == s.getAge())
                    && (studentList[2].equals(s.getNumber())) && (studentList[3].equals(s.getAddress()))
                    && (studentList[4].equals(s.getTelePhone()))) {
                Scanner sc3=new Scanner(System.in);
                Scanner sc4=new Scanner(System.in);
                Student student=new Student();
                System.out.println("请输入修改后学生的姓名");
                student.setName(sc3.nextLine());
                System.out.println("请输入要修改后的学生的年龄:");
                student.setAge(sc4.nextInt());
                System.out.println("请输入要修改后的学生的学号:");
                student.setNumber(sc3.nextLine());
                System.out.println("请输入要修改后的学生的家庭住址:");
                student.setAddress(sc3.nextLine());
                System.out.println("请输入要修改后的学生的电话:");
                student.setTelePhone(sc3.nextLine());
                arrayList.add(student);
                n=0;
            }else {
                arrayList.add(s1);
            }
        }
        if(n==0){
            System.out.println("信息修改成功!");
        }else {
            System.out.println("请检查你所要修改的学生的信息是否有误!");
            System.out.println("信息修改失败!");
        }
        br.close();
        BufferedWriter bw=new BufferedWriter(new FileWriter("书本的课后习题\\xinXi1.txt"));
        for(Student student:arrayList){
            bw.write(student.getName()+","+student.getAge()+","+student.getNumber()+","+student.getAddress()+","+student.getTelePhone());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }


    //查看学生信息。
    public static void chaKanStudent()throws IOException{
        BufferedReader br = new BufferedReader(new FileReader("书本的课后习题\\xinXi1.txt"));
        String line;
        while ((line= br.readLine())!=null){
            Student s=new Student();
            String[] studentList = line.split(",");
            s.setName(studentList[0]);
            s.setAge(Integer.parseInt(studentList[1]));
            s.setNumber(studentList[2]);
            s.setAddress(studentList[3]);
            s.setTelePhone(studentList[4]);
            System.out.println("姓名:"+s.getName()+", 年龄:"+s.getAge()+" ,学号:"+s.getNumber()+" ,家庭住址:"+s.getAddress()+" ,家庭电话:"+s.getTelePhone());
        }
        br.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我要学好编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值