Java代码:学生选课信息。系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师。
时间: 2023-11-09 12:05:58 浏览: 181
下面是一份基于 Java 的学生选课信息系统的代码示例:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student {
private String id;
private String name;
private String gender;
private int grade;
private String password;
public Student(String id, String name, String gender, int grade, String password) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
this.password = password;
}
// getters and setters
}
class Course {
private String id;
private String name;
private int numSelected;
private int numAvailable;
private String teacher;
public Course(String id, String name, int numSelected, int numAvailable, String teacher) {
this.id = id;
this.name = name;
this.numSelected = numSelected;
this.numAvailable = numAvailable;
this.teacher = teacher;
}
// getters and setters
}
class CourseSelection {
private Student student;
private Course course;
public CourseSelection(Student student, Course course) {
this.student = student;
this.course = course;
}
// getters and setters
}
class CourseSelectionSystem {
private List<Student> students;
private List<Course> courses;
private List<CourseSelection> selections;
public CourseSelectionSystem() {
students = new ArrayList<>();
courses = new ArrayList<>();
selections = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void addCourse(Course course) {
courses.add(course);
}
public void selectCourse(Student student, Course course) {
if (course.getNumAvailable() > 0) {
CourseSelection selection = new CourseSelection(student, course);
selections.add(selection);
course.setNumSelected(course.getNumSelected() + 1);
course.setNumAvailable(course.getNumAvailable() - 1);
System.out.println("选课成功!");
} else {
System.out.println("选课失败,该课程已满!");
}
}
// getters and setters
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CourseSelectionSystem system = new CourseSelectionSystem();
// 添加学生
Student s1 = new Student("001", "张三", "男", 1, "123456");
Student s2 = new Student("002", "李四", "女", 2, "123456");
system.addStudent(s1);
system.addStudent(s2);
// 添加课程
Course c1 = new Course("001", "Java程序设计", 0, 2, "张老师");
Course c2 = new Course("002", "计算机网络", 0, 3, "李老师");
system.addCourse(c1);
system.addCourse(c2);
// 选课
System.out.print("请输入学号:");
String id = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
Student student = null;
for (Student s : system.getStudents()) {
if (s.getId().equals(id) && s.getPassword().equals(password)) {
student = s;
break;
}
}
if (student == null) {
System.out.println("学号或密码错误!");
return;
}
System.out.println("欢迎您," + student.getName() + "!");
System.out.println("可选课程列表:");
for (Course c : system.getCourses()) {
System.out.println(c.getId() + "\t" + c.getName() + "\t" + c.getTeacher() + "\t"
+ c.getNumSelected() + "/" + c.getNumAvailable() + "人");
}
System.out.print("请输入课程编号:");
String courseId = scanner.nextLine();
Course course = null;
for (Course c : system.getCourses()) {
if (c.getId().equals(courseId)) {
course = c;
break;
}
}
if (course == null) {
System.out.println("课程不存在!");
return;
}
system.selectCourse(student, course);
}
}
```
该代码实现了一个简单的学生选课信息系统。其中,`Student` 类表示学生,包括学号、姓名、性别、年级和密码等信息;`Course` 类表示课程,包括课程编号、课程名称、已选人数、可选人数和授课教师等信息;`CourseSelection` 类表示学生选课信息,包括学生和课程两个属性;`CourseSelectionSystem` 类表示选课系统,包括学生列表、课程列表和选课记录列表等属性,以及添加学生、添加课程和选课等方法。
在 `Main` 类中,首先创建了一个 `CourseSelectionSystem` 对象,然后添加了两个学生和两门课程。接着,通过输入学生的学号和密码,可以登录选课系统,查看可选课程列表,并选择一门课程进行选课。如果该课程已满,则选课失败。
阅读全文