Java实现学生信息管理系统之增、删、改、查

Java实现学生管理系统

      • Java实现学生管理系统
        • 需求分析
        • 代码实现
        • 运行结果

Java实现学生管理系统

需求分析

  • 添加学生:通过键盘录入学生信息,添加到集合中
  • 删除学生:通过键盘录入要删除学生的学号,将该学生对象从集合中删除
  • 修改学生:通过键盘录入要修改学生的学号,将该学生对象其他信息进行修改
  • 查看学生:将集合中的学生对象信息进行展示
  • 退出系统:结束程序

代码实现

Student类

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

测试类

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        ArrayList<Student> arrayList = new ArrayList<Student>();
        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 scanner = new Scanner(System.in);
            int a = scanner.nextInt();
            switch (a) {
                case 1:
                    //System.out.println("添加学生信息");
                    addStudent(arrayList);
                    break;
                case 2:
                    //System.out.println("删除学生信息");
                    deleteStudent(arrayList);
                    break;
                case 3:
                    // System.out.println("修改学生信息");
                    updateStudent(arrayList);
                    break;
                case 4:
                    //System.out.println("查看所有学生信息");
                    findAllStudent(arrayList);
                    break;
                case 5:
                    System.out.println("谢谢使用!!!");
                    //break;
                    System.exit(0);  //JVM退出
            }
        }

    }

    public static void addStudent(ArrayList<Student> arrayList) {
        Scanner scanner = new Scanner(System.in);

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

            boolean flag = isUsed(arrayList, id);
            if (flag) {
                System.out.println("学号已被占用,请重新输入");
            } else {
                break;
            }
        }

        System.out.println("请输入学生姓名");
        String name = scanner.nextLine();
        System.out.println("请输入学生年龄");
        String age = scanner.nextLine();
        System.out.println("请输入学生地址");
        String address = scanner.nextLine();

        Student student = new Student();
        student.setName(name);
        student.setId(id);
        student.setAge(age);
        student.setAddress(address);
        arrayList.add(student);
    }


    public static boolean isUsed(ArrayList<Student> arrayList, String id) {
        boolean flag = false;
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (student.getId().equals(id)) {
                flag = true;
                break;
            }


        }
        return flag;
    }

    public static void findAllStudent(ArrayList<Student> arrayList) {
        if (arrayList.size() == 0) {
            System.out.println("没有信息,请先添加信息");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t\t地址");
        for (int i = 0; i < arrayList.size(); i++) {
            Student s = arrayList.get(i);
            System.out.println(s.getId() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "岁\t\t" + s.getAddress());
        }
    }

    public static void deleteStudent(ArrayList<Student> arrayList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除学生的学号");
        String sid = scanner.nextLine();
        int index = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            Student s = arrayList.get(i);
            if (s.getId().equals(sid)) {
                index = i;
                break;

            }
        }
        if (index == -1) {
            System.out.println("该学生信息不存在,请先添加学生信息");
        } else {
            arrayList.remove(index);
            System.out.println("删除学生成功!!");
        }
    }


    public static void updateStudent(ArrayList<Student> arrayList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要修改学生的学号");
        String sid = scanner.nextLine();

        int index = -1;
        for (int i = 0; i < arrayList.size(); i++) {
            Student str = arrayList.get(i);
            if (str.getId().equals(sid)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("该学生信息不存在,请重新添加学生信息");
            addStudent(arrayList);
        } else {
            System.out.println("请输入新的姓名");
            String name = scanner.nextLine();
            System.out.println("请输入新的年龄");
            String age = scanner.nextLine();
            System.out.println("请输入新的地址");
            String address = scanner.nextLine();

            Student s = new Student();
            s.setId(sid);
            s.setAddress(address);
            s.setAge(age);
            s.setName(name);
            arrayList.set(index, s);
            System.out.println("修改成功");
        }
    }

}

运行结果

Java实现学生信息管理系统之增、删、改、查_第1张图片

你可能感兴趣的:(JavaLearn,java)