用java写一个教学管理系统
时间: 2024-05-05 14:15:28 浏览: 103
由于教学管理系统的具体功能和需求有很多种,因此这里提供一个简单的教学管理系统的代码示例,仅供参考。
首先,我们需要定义一些基本的实体类,如学生、教师、课程等:
```java
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String major;
// 构造函数、getters和setters省略
}
public class Teacher {
private int id;
private String name;
private int age;
private String gender;
private String department;
// 构造函数、getters和setters省略
}
public class Course {
private int id;
private String name;
private int credit;
private Teacher teacher;
private List<Student> students;
// 构造函数、getters和setters省略
}
```
然后,我们需要定义一个教学管理系统的类,包含一些基本的功能,如添加学生、删除学生、添加教师、删除教师、添加课程、删除课程等:
```java
public class TeachingManagementSystem {
private List<Student> students;
private List<Teacher> teachers;
private List<Course> courses;
public TeachingManagementSystem() {
students = new ArrayList<>();
teachers = new ArrayList<>();
courses = new ArrayList<>();
}
// 添加学生
public void addStudent(Student student) {
students.add(student);
}
// 删除学生
public void removeStudent(int id) {
for (Student student : students) {
if (student.getId() == id) {
students.remove(student);
return;
}
}
}
// 添加教师
public void addTeacher(Teacher teacher) {
teachers.add(teacher);
}
// 删除教师
public void removeTeacher(int id) {
for (Teacher teacher : teachers) {
if (teacher.getId() == id) {
teachers.remove(teacher);
return;
}
}
}
// 添加课程
public void addCourse(Course course) {
courses.add(course);
}
// 删除课程
public void removeCourse(int id) {
for (Course course : courses) {
if (course.getId() == id) {
courses.remove(course);
return;
}
}
}
// 选课
public void addStudentToCourse(int studentId, int courseId) {
for (Student student : students) {
if (student.getId() == studentId) {
for (Course course : courses) {
if (course.getId() == courseId) {
course.getStudents().add(student);
return;
}
}
}
}
}
// 退课
public void removeStudentFromCourse(int studentId, int courseId) {
for (Course course : courses) {
if (course.getId() == courseId) {
for (Student student : course.getStudents()) {
if (student.getId() == studentId) {
course.getStudents().remove(student);
return;
}
}
}
}
}
// 列出所有学生
public List<Student> listStudents() {
return students;
}
// 列出所有教师
public List<Teacher> listTeachers() {
return teachers;
}
// 列出所有课程
public List<Course> listCourses() {
return courses;
}
}
```
最后,我们可以在主函数中测试教学管理系统的功能:
```java
public static void main(String[] args) {
TeachingManagementSystem tms = new TeachingManagementSystem();
// 添加学生
Student s1 = new Student(1, "张三", 18, "男", "计算机科学与技术");
tms.addStudent(s1);
Student s2 = new Student(2, "李四", 19, "女", "软件工程");
tms.addStudent(s2);
// 添加教师
Teacher t1 = new Teacher(1, "王老师", 35, "男", "计算机科学与技术");
tms.addTeacher(t1);
// 添加课程
Course c1 = new Course(1, "Java程序设计", 3, t1, new ArrayList<>());
tms.addCourse(c1);
// 选课
tms.addStudentToCourse(1, 1);
// 列出所有学生
List<Student> students = tms.listStudents();
for (Student student : students) {
System.out.println(student.getName());
}
// 列出所有教师
List<Teacher> teachers = tms.listTeachers();
for (Teacher teacher : teachers) {
System.out.println(teacher.getName());
}
// 列出所有课程
List<Course> courses = tms.listCourses();
for (Course course : courses) {
System.out.println(course.getName());
}
}
```
阅读全文