用Java代码写一个学生选课管理系统
时间: 2024-01-26 15:04:52 浏览: 89
好的,我可以为您提供一个学生选课管理系统的Java代码框架,您可以根据自己的需要进行修改和完善。
```
import java.util.ArrayList;
import java.util.Scanner;
public class Course {
private String name;
private String teacher;
private int credit;
private int capacity;
private int enrolled;
public Course(String name, String teacher, int credit, int capacity) {
this.name = name;
this.teacher = teacher;
this.credit = credit;
this.capacity = capacity;
enrolled = 0;
}
public String getName() {
return name;
}
public String getTeacher() {
return teacher;
}
public int getCredit() {
return credit;
}
public int getCapacity() {
return capacity;
}
public int getEnrolled() {
return enrolled;
}
public void setEnrolled(int enrolled) {
this.enrolled = enrolled;
}
}
public class Student {
private String name;
private ArrayList<Course> courses;
public Student(String name) {
this.name = name;
courses = new ArrayList<>();
}
public String getName() {
return name;
}
public void addCourse(Course course) {
courses.add(course);
}
public void removeCourse(Course course) {
courses.remove(course);
}
public ArrayList<Course> getCourses() {
return courses;
}
public int getTotalCredit() {
int totalCredit = 0;
for (Course course : courses) {
totalCredit += course.getCredit();
}
return totalCredit;
}
}
public class CourseManagementSystem {
private ArrayList<Course> courses;
private ArrayList<Student> students;
public CourseManagementSystem() {
courses = new ArrayList<>();
students = new ArrayList<>();
}
public void addCourse(Course course) {
courses.add(course);
}
public void removeCourse(Course course) {
courses.remove(course);
}
public ArrayList<Course> getCourses() {
return courses;
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public ArrayList<Student> getStudents() {
return students;
}
public void enroll(Student student, Course course) {
if (course.getEnrolled() < course.getCapacity()) {
student.addCourse(course);
course.setEnrolled(course.getEnrolled() + 1);
} else {
System.out.println("Sorry, the course is full.");
}
}
public void withdraw(Student student, Course course) {
student.removeCourse(course);
course.setEnrolled(course.getEnrolled() - 1);
}
}
public class Main {
public static void main(String[] args) {
CourseManagementSystem cms = new CourseManagementSystem();
Course c1 = new Course("Java Programming", "John Smith", 3, 30);
Course c2 = new Course("Database Management", "Mary Johnson", 3, 25);
Course c3 = new Course("Web Development", "Bob Brown", 4, 35);
cms.addCourse(c1);
cms.addCourse(c2);
cms.addCourse(c3);
Student s1 = new Student("Tom");
Student s2 = new Student("Jerry");
cms.addStudent(s1);
cms.addStudent(s2);
cms.enroll(s1, c1);
cms.enroll(s1, c2);
cms.enroll(s2, c1);
cms.enroll(s2, c3);
System.out.println("Courses:");
for (Course course : cms.getCourses()) {
System.out.println(course.getName() + " - " + course.getTeacher() + " - " + course.getEnrolled() + "/" + course.getCapacity());
}
System.out.println();
System.out.println("Students:");
for (Student student : cms.getStudents()) {
System.out.println(student.getName() + " - Total Credit: " + student.getTotalCredit());
System.out.println("Enrolled Courses:");
for (Course course : student.getCourses()) {
System.out.println(course.getName() + " - " + course.getTeacher() + " - " + course.getCredit());
}
System.out.println();
}
cms.withdraw(s1, c1);
System.out.println("Courses:");
for (Course course : cms.getCourses()) {
System.out.println(course.getName() + " - " + course.getTeacher() + " - " + course.getEnrolled() + "/" + course.getCapacity());
}
System.out.println();
System.out.println("Students:");
for (Student student : cms.getStudents()) {
System.out.println(student.getName() + " - Total Credit: " + student.getTotalCredit());
System.out.println("Enrolled Courses:");
for (Course course : student.getCourses()) {
System.out.println(course.getName() + " - " + course.getTeacher() + " - " + course.getCredit());
}
System.out.println();
}
}
}
```
这段代码实现了一个简单的学生选课管理系统,包括课程和学生两个类,以及一个管理系统类。您可以根据自己的需求进行修改和完善。
阅读全文