码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-03-17 19:51:37      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:student   public   修改   str   inf   upd   print   lis   ntb   

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

}

 

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

标签:student   public   修改   str   inf   upd   print   lis   ntb   

原文地址:http://www.cnblogs.com/lujing-newer/p/6567700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!