用java编写一个简单的学生选课管理系统的代码
时间: 2024-03-12 20:44:31 浏览: 14
好的,以下是一个简单的学生选课管理系统的Java代码:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Course {
private String name;
private int capacity;
private List<String> students;
public Course(String name, int capacity) {
this.name = name;
this.capacity = capacity;
this.students = new ArrayList<>();
}
public String getName() {
return name;
}
public int getCapacity() {
return capacity;
}
public List<String> getStudents() {
return students;
}
public boolean addStudent(String studentName) {
if (students.size() < capacity) {
students.add(studentName);
return true;
}
return false;
}
public boolean removeStudent(String studentName) {
return students.remove(studentName);
}
public int getAvailableSeats() {
return capacity - students.size();
}
}
class Student {
private String name;
private List<Course> courses;
public Student(String name) {
this.name = name;
this.courses = new ArrayList<>();
}
public String getName() {
return name;
}
public List<Course> getCourses() {
return courses;
}
public boolean addCourse(Course course) {
if (course.addStudent(name)) {
courses.add(course);
return true;
}
return false;
}
public boolean dropCourse(Course course) {
if (course.removeStudent(name)) {
courses.remove(course);
return true;
}
return false;
}
}
public class CourseManagementSystem {
private List<Course> courses;
public CourseManagementSystem() {
this.courses = new ArrayList<>();
}
public void addCourse(String name, int capacity) {
courses.add(new Course(name, capacity));
}
public Course findCourse(String name) {
for (Course course : courses) {
if (course.getName().equals(name)) {
return course;
}
}
return null;
}
public void listCourses() {
System.out.println("Available courses:");
for (Course course : courses) {
int availableSeats = course.getAvailableSeats();
System.out.println(course.getName() + " (" + availableSeats + " available seats)");
}
}
public static void main(String[] args) {
CourseManagementSystem cms = new CourseManagementSystem();
cms.addCourse("Java Programming", 20);
cms.addCourse("Database Design", 15);
cms.addCourse("Web Development", 25);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter your name:");
String name = scanner.nextLine();
Student student = new Student(name);
while (true) {
System.out.println("Enter 1 to list available courses");
System.out.println("Enter 2 to register for a course");
System.out.println("Enter 3 to drop a course");
System.out.println("Enter 4 to exit");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
cms.listCourses();
} else if (choice == 2) {
System.out.println("Enter the name of the course you want to register for:");
String courseName = scanner.nextLine();
Course course = cms.findCourse(courseName);
if (course == null) {
System.out.println("Course not found");
} else if (student.addCourse(course)) {
System.out.println("Registered for " + courseName);
} else {
System.out.println("Course is full");
}
} else if (choice == 3) {
System.out.println("Enter the name of the course you want to drop:");
String courseName = scanner.nextLine();
Course course = null;
for (Course c : student.getCourses()) {
if (c.getName().equals(courseName)) {
course = c;
break;
}
}
if (course == null) {
System.out.println("You are not registered for " + courseName);
} else if (student.dropCourse(course)) {
System.out.println("Dropped " + courseName);
}
} else if (choice == 4) {
System.exit(0);
} else {
System.out.println("Invalid choice");
}
}
}
}
}
```
这个代码实现了一个简单的学生选课管理系统,可以添加课程、列出可选课程、注册课程、退选课程等功能。在主函数中,通过不停地读取用户的输入来进行操作,直到用户选择退出。