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

Posted 摩西摩西点点

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用集合实现一个控制台版的学生管理系统相关的知识,希望对你有一定的参考价值。

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

}

 

以上是关于用集合实现一个控制台版的学生管理系统的主要内容,如果未能解决你的问题,请参考以下文章

创建List保存学生信息

创建List保存学生信息

Laravel之简单的学生信息管理平台

集合应用案例:编写程序实现学生信息管理系统的录入登录

java 编写的 学生信息管理系统

java学习3创建学生属性:学号姓名电话 完全实现对象集合的增删改查。(控制台来做)