首页 > 代码库 > 用集合实现一个控制台版的学生管理系统

用集合实现一个控制台版的学生管理系统

public class Student {
    private int id;
    private String name;
    private int age;
    private String classNum;

    public Student() {

    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public String getClassNum() {
        return classNum;
    }

    public void setClassNum(String classNum) {
        this.classNum = classNum;
    }

    public String toString() {
        return "学号:" + this.id + "\t姓名:" + this.name + "\t年龄:" + this.age + "\t班级编号:" + this.classNum;
    }

}
import java.util.List;

public class StudentManager {
    /**
     * 显示所有学生信息的方法
     */
    public void showAllStudentInfo(List<Student> stus) {
        for (Student student : stus) {
            System.out.println(student);
        }
    }

    /**
     * 根据学员学号查询
     */
    public void getStudentById(int id, List<Student> stus) {
        int count = 0;
        for (Student student : stus) {
            if (student.getId() == id) {
                System.out.println(student);
            } else {
                count++;
            }
        }
        if (count == stus.size()) {
            System.out.println("没有该学号的学员");
        }
    }

    /**
     * 根据学员学号删除学生信息
     */
    public void deleteById(int id, List<Student> stus) {
        int index = 0;
        for (int i = 0; i < stus.size(); i++) {
            Student student = stus.get(i);
            if (student.getId() == id) {
                index = i;
                break;
            }
        }
        stus.remove(index);
    }

    /**
     * 根据学号修改学员信息
     */
    public void updateStudent(int uid, List<Student> stus, Student student) {
        int index = 0;
        for (int i = 0; i < stus.size(); i++) {
            if (stus.get(i).getId() == uid) {
                index = i;
                break;
            }
        }
        stus.set(index, student);
    }

}
import java.util.List;

public class StudentManager {
    /**
     * 显示所有学生信息的方法
     */
    public void showAllStudentInfo(List<Student> stus) {
        for (Student student : stus) {
            System.out.println(student);
        }
    }

    /**
     * 根据学员学号查询
     */
    public void getStudentById(int id, List<Student> stus) {
        int count = 0;
        for (Student student : stus) {
            if (student.getId() == id) {
                System.out.println(student);
            } else {
                count++;
            }
        }
        if (count == stus.size()) {
            System.out.println("没有该学号的学员");
        }
    }

    /**
     * 根据学员学号删除学生信息
     */
    public void deleteById(int id, List<Student> stus) {
        int index = 0;
        for (int i = 0; i < stus.size(); i++) {
            Student student = stus.get(i);
            if (student.getId() == id) {
                index = i;
                break;
            }
        }
        stus.remove(index);
    }

    /**
     * 根据学号修改学员信息
     */
    public void updateStudent(int uid, List<Student> stus, Student student) {
        int index = 0;
        for (int i = 0; i < stus.size(); i++) {
            if (stus.get(i).getId() == uid) {
                index = i;
                break;
            }
        }
        stus.set(index, student);
    }

}

 

用集合实现一个控制台版的学生管理系统