使用Java写一份学生管理系统代码
时间: 2023-12-02 09:05:23 浏览: 68
好的,以下是一个简单的学生管理系统的Java代码示例:
Student类:
```java
public class Student {
private String name;
private String id;
private int age;
private String gender;
public Student(String name, String id, int age, String gender) {
this.name = name;
this.id = id;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
```
StudentManagement类:
```java
import java.util.ArrayList;
public class StudentManagement {
private ArrayList<Student> students;
public StudentManagement() {
students = new ArrayList<Student>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
students.remove(student);
break;
}
}
}
public void updateStudent(String id, Student newStudent) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.set(i, newStudent);
break;
}
}
}
public Student findStudent(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
public ArrayList<Student> getStudents() {
return students;
}
}
```
StudentManagementTest类:
```java
public class StudentManagementTest {
public static void main(String[] args) {
Student s1 = new Student("Tom", "1001", 20, "Male");
Student s2 = new Student("Alice", "1002", 19, "Female");
Student s3 = new Student("Bob", "1003", 21, "Male");
StudentManagement sm = new StudentManagement();
sm.addStudent(s1);
sm.addStudent(s2);
sm.addStudent(s3);
System.out.println("All Students:");
for (Student student : sm.getStudents()) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getAge() + " " + student.getGender());
}
System.out.println("\nAfter removing student with id 1002:");
sm.removeStudent("1002");
for (Student student : sm.getStudents()) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getAge() + " " + student.getGender());
}
System.out.println("\nAfter updating student with id 1003:");
Student newStudent = new Student("Bob", "1003", 22, "Male");
sm.updateStudent("1003", newStudent);
for (Student student : sm.getStudents()) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getAge() + " " + student.getGender());
}
System.out.println("\nFind student with id 1001:");
Student student = sm.findStudent("1001");
if (student != null) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getAge() + " " + student.getGender());
} else {
System.out.println("Student not found!");
}
}
}
```
这些代码实现了添加学生、删除学生、修改学生信息、查询学生信息等操作,可以通过StudentManagementTest类进行测试。在实际应用中,可以将这些功能与GUI界面进行结合,实现一个完整的学生管理系统。
阅读全文