能用Java写出一个学生选课管理系统吗
时间: 2024-03-07 09:02:54 浏览: 77
当然可以!以下是一个简单的学生选课管理系统的Java代码示例,供你参考:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class CourseSelectionSystem {
private Map<String, Student> students;
private Map<String, Course> courses;
public CourseSelectionSystem() {
students = new HashMap<>();
courses = new HashMap<>();
}
public void addStudent(Student student) {
students.put(student.getId(), student);
}
public void addCourse(Course course) {
courses.put(course.getCode(), course);
}
public void assignCourse(String studentId, String courseCode) {
Student student = students.get(studentId);
Course course = courses.get(courseCode);
if (student != null && course != null) {
student.addCourse(course);
course.addStudent(student);
}
}
public void dropCourse(String studentId, String courseCode) {
Student student = students.get(studentId);
Course course = courses.get(courseCode);
if (student != null && course != null) {
student.removeCourse(course);
course.removeStudent(student);
}
}
public void printStudents() {
for (Student student : students.values()) {
System.out.println(student);
}
}
public void printCourses() {
for (Course course : courses.values()) {
System.out.println(course);
}
}
public static void main(String[] args) {
CourseSelectionSystem system = new CourseSelectionSystem();
// Add students
system.addStudent(new Student("001", "Alice"));
system.addStudent(new Student("002", "Bob"));
system.addStudent(new Student("003", "Charlie"));
// Add courses
system.addCourse(new Course("CS101", "Introduction to Computer Science"));
system.addCourse(new Course("MATH101", "Calculus I"));
system.addCourse(new Course("PHYS101", "Physics I"));
// Assign courses to students
system.assignCourse("001", "CS101");
system.assignCourse("001", "MATH101");
system.assignCourse("002", "MATH101");
system.assignCourse("003", "PHYS101");
// Drop a course
system.dropCourse("001", "MATH101");
// Print students and courses
System.out.println("Students:");
system.printStudents();
System.out.println("Courses:");
system.printCourses();
}
}
class Student {
private String id;
private String name;
private List<Course> courses;
public Student(String id, String name) {
this.id = id;
this.name = name;
courses = new ArrayList<>();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void addCourse(Course course) {
courses.add(course);
}
public void removeCourse(Course course) {
courses.remove(course);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Student ID: ").append(id).append("\n");
sb.append("Name: ").append(name).append("\n");
sb.append("Courses: ");
if (courses.isEmpty()) {
sb.append("None");
} else {
for (Course course : courses) {
sb.append(course.getCode()).append(", ");
}
sb.delete(sb.length() - 2, sb.length());
}
sb.append("\n");
return sb.toString();
}
}
class Course {
private String code;
private String name;
private List<Student> students;
public Course(String code, String name) {
this.code = code;
this.name = name;
students = new ArrayList<>();
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Course Code: ").append(code).append("\n");
sb.append("Name: ").append(name).append("\n");
sb.append("Students: ");
if (students.isEmpty()) {
sb.append("None");
} else {
for (Student student : students) {
sb.append(student.getName()).append(", ");
}
sb.delete(sb.length() - 2, sb.length());
}
sb.append("\n");
return sb.toString();
}
}
```
这个简单的学生选课管理系统包含了学生和课程两个类,并且实现了添加学生、添加课程、为学生分配课程、为学生删除课程、输出所有学生信息和输出所有课程信息等功能。你可以根据自己的需要进行修改和扩展。
阅读全文