用java实现一个简易的学生选课系统
时间: 2023-11-07 09:04:48 浏览: 81
好的,我可以给你提供一个简单的学生选课系统的JAVA实现代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class CourseSelectionSystem {
public static void main(String[] args) {
// 定义学生列表和课程列表
ArrayList<Student> studentList = new ArrayList<>();
ArrayList<Course> courseList = new ArrayList<>();
// 初始化课程列表
courseList.add(new Course("Java程序设计", "张三", "周一 1-2节", "教1-101", 3));
courseList.add(new Course("计算机网络", "李四", "周二 3-4节", "教1-102", 2));
courseList.add(new Course("数据结构与算法", "王五", "周三 5-6节", "教1-103", 4));
// 实现一个菜单,提供学生选择课程和查询已选课程的功能
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 学生选课");
System.out.println("2. 查询已选课程");
System.out.println("3. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学号:");
int id = scanner.nextInt();
System.out.println("请选择课程编号:");
for (int i = 0; i < courseList.size(); i++) {
System.out.println((i + 1) + ". " + courseList.get(i).toString());
}
int courseIndex = scanner.nextInt() - 1;
Course selectedCourse = courseList.get(courseIndex);
if (selectedCourse.getNumberOfStudents() < selectedCourse.getCapacity()) {
Student student = new Student(name, id, selectedCourse);
studentList.add(student);
selectedCourse.addStudent();
System.out.println("选课成功!");
} else {
System.out.println("该课程已满员,选课失败!");
}
break;
case 2:
System.out.println("请输入学号:");
int searchId = scanner.nextInt();
for (Student student : studentList) {
if (student.getId() == searchId) {
System.out.println("已选课程:");
System.out.println(student.getSelectedCourse().toString());
break;
}
}
break;
case 3:
System.out.println("感谢使用本系统!");
return;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
}
class Course {
private String name;
private String teacher;
private String time;
private String location;
private int capacity;
private int numberOfStudents;
public Course(String name, String teacher, String time, String location, int capacity) {
this.name = name;
this.teacher = teacher;
this.time = time;
this.location = location;
this.capacity = capacity;
this.numberOfStudents = 0;
}
public String getName() {
return name;
}
public String getTeacher() {
return teacher;
}
public String getTime() {
return time;
}
public String getLocation() {
return location;
}
public int getCapacity() {
return capacity;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public void addStudent() {
numberOfStudents++;
}
public String toString() {
return name + " (" + teacher + ") " + time + " " + location + " " + numberOfStudents + "/" + capacity;
}
}
class Student {
private String name;
private int id;
private Course selectedCourse;
public Student(String name, int id, Course selectedCourse) {
this.name = name;
this.id = id;
this.selectedCourse = selectedCourse;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public Course getSelectedCourse() {
return selectedCourse;
}
}
```
这个代码实现了一个简单的学生选课系统,包括学生类、课程类和主函数。在主函数中,我们通过一个菜单提供学生选择课程和查询已选课程的功能。在学生选择课程时,我们会检查该课程是否已满员,如果已满员则不能选择该课程。在查询已选课程时,我们会遍历学生列表,找到该学生所选的所有课程并输出。
阅读全文