学生管理系统:
创建一个Student类,包含属性:姓名、学号、年龄,成绩。
提供构造方法,用于初始化学生信息,提供get方法和set方法,用于访问和修改学生信息。
提供一个Studentmange,用于管理学生信息(添加,删除,修改,查询)。
创建一个学生信息类
//Student.java package learn.code; import java.io.StreamCorruptedException; public class Student { private String name; private String studentId; private int age; private double score; public Student(String name,String studentId,int age,double score){ this.name = name; this.studentId = studentId; this.age = age; this.score = score; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getStudentId(){ return studentId; } public void setStudentId(String studentId){ this.studentId = studentId; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } public double getScore(){ return score; } public void setScore(double score){ this.score = score; } }
创建一个学生信息管理类
//Studentmanage.java package learn.code; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; public class Studentmanage { //存储学生信息的列表 private List<Student> students; public Studentmanage(){ students = new ArrayList<>(); } //增加学生信息 public void addStudent(Student student){ students.add(student); } //根据学号删除学生信息 public void removeStudent(String studentId){ for (Student student : students ){ //判断以下当前取出的学号与传入的是否一致 if (student.getStudentId().equals(studentId)){ //在列表中删除,双重释放 students.remove(student); return; } } } //根据学号对学生信息进行修改 public void updateStudent(String studentId,String name,int age,double score){ for (Student student : students ){ //判断以下当前取出的学号与传入的是否一致 if (student.getStudentId().equals(studentId)){ student.setAge(age); student.setName(name); student.setScore(score); } } } public Student queryStudent(String studentId){ for (Student student : students ){ //判断以下当前取出的学号与传入的是否一致 if (student.getStudentId().equals(studentId)){ return student; } } return null; } }
主函数的实现
//Main.java package learn.code; public class Main { public static void main(String[] args){ //创建学生信息 Student studentA = new Student("小李","001",18,94.3); Student studentB = new Student("小王","002",18,92.8); Student studentC = new Student("小张","003",17,95); Student studentD = new Student("小周","004",18,96); //学生管理类对象创建 Studentmanage manage = new Studentmanage(); //添加学生信息 manage.addStudent(studentA); manage.addStudent(studentB); manage.addStudent(studentC); manage.addStudent(studentD); //查询学生信息 Student queryResult = manage.queryStudent("003"); System.out.println("查询结果:" + queryResult.getName() + "\t" + queryResult.getStudentId() + "\t" + queryResult.getAge() + "\t" + queryResult.getScore()); //学生信息的修改 manage.updateStudent("小草","003", 16,99); //删除学生信息 manage.removeStudent("002"); } }