用Java代码编写一个在线学习系统程序,实现课程加入、撤销、课程信息修改、课程信息查询等功能
时间: 2023-12-12 18:04:39 浏览: 142
好的,我可以给您提供一个简单的Java代码示例来实现在线学习系统程序。以下是代码实现:
```
import java.util.ArrayList;
import java.util.List;
// 课程类
class Course {
private String courseId; // 课程编号
private String courseName; // 课程名称
private String courseIntro; // 课程介绍
private int selectedNum; // 已选人数
public Course(String courseId, String courseName, String courseIntro) {
this.courseId = courseId;
this.courseName = courseName;
this.courseIntro = courseIntro;
this.selectedNum = 0;
}
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseIntro() {
return courseIntro;
}
public void setCourseIntro(String courseIntro) {
this.courseIntro = courseIntro;
}
public int getSelectedNum() {
return selectedNum;
}
public void setSelectedNum(int selectedNum) {
this.selectedNum = selectedNum;
}
@Override
public String toString() {
return "Course{" +
"courseId='" + courseId + '\'' +
", courseName='" + courseName + '\'' +
", courseIntro='" + courseIntro + '\'' +
", selectedNum=" + selectedNum +
'}';
}
}
// 学生类
class Student {
private String studentId; // 学生编号
private String studentName; // 学生姓名
private List<Course> selectedCourses; // 已选课程列表
public Student(String studentId, String studentName) {
this.studentId = studentId;
this.studentName = studentName;
this.selectedCourses = new ArrayList<>();
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public List<Course> getSelectedCourses() {
return selectedCourses;
}
public void setSelectedCourses(List<Course> selectedCourses) {
this.selectedCourses = selectedCourses;
}
@Override
public String toString() {
return "Student{" +
"studentId='" + studentId + '\'' +
", studentName='" + studentName + '\'' +
", selectedCourses=" + selectedCourses +
'}';
}
}
// 在线学习系统类
class OnlineLearningSystem {
private List<Course> courseList; // 课程列表
private List<Student> studentList; // 学生列表
public OnlineLearningSystem() {
this.courseList = new ArrayList<>();
this.studentList = new ArrayList<>();
}
// 课程加入
public void addCourse(Course course) {
courseList.add(course);
System.out.println("课程 " + course.getCourseName() + " 添加成功!");
}
// 课程撤销
public void removeCourse(String courseId) {
Course targetCourse = null;
for (Course course : courseList) {
if (course.getCourseId().equals(courseId)) {
targetCourse = course;
break;
}
}
if (targetCourse != null) {
courseList.remove(targetCourse);
System.out.println("课程 " + targetCourse.getCourseName() + " 撤销成功!");
} else {
System.out.println("未找到编号为 " + courseId + " 的课程!");
}
}
// 课程信息修改
public void modifyCourse(String courseId, String courseName, String courseIntro) {
Course targetCourse = null;
for (Course course : courseList) {
if (course.getCourseId().equals(courseId)) {
targetCourse = course;
break;
}
}
if (targetCourse != null) {
targetCourse.setCourseName(courseName);
targetCourse.setCourseIntro(courseIntro);
System.out.println("课程 " + targetCourse.getCourseName() + " 修改成功!");
} else {
System.out.println("未找到编号为 " + courseId + " 的课程!");
}
}
// 课程信息查询
public void queryCourse(String keyword) {
List<Course> result = new ArrayList<>();
for (Course course : courseList) {
if (course.getCourseId().contains(keyword) || course.getCourseName().contains(keyword)) {
result.add(course);
}
}
if (result.size() > 0) {
System.out.println("查询结果如下:");
for (Course course : result) {
System.out.println(course);
}
} else {
System.out.println("未找到相关课程!");
}
}
// 学生选课
public void chooseCourse(String studentId, String courseId) {
Course targetCourse = null;
for (Course course : courseList) {
if (course.getCourseId().equals(courseId)) {
targetCourse = course;
break;
}
}
if (targetCourse != null) {
if (targetCourse.getSelectedNum() >= 10) {
System.out.println("课程 " + targetCourse.getCourseName() + " 选课人数已满!");
} else {
Student targetStudent = null;
for (Student student : studentList) {
if (student.getStudentId().equals(studentId)) {
targetStudent = student;
break;
}
}
if (targetStudent != null) {
if (targetStudent.getSelectedCourses().contains(targetCourse)) {
System.out.println("学生 " + targetStudent.getStudentName() + " 已选过该课程!");
} else {
targetStudent.getSelectedCourses().add(targetCourse);
targetCourse.setSelectedNum(targetCourse.getSelectedNum() + 1);
System.out.println("学生 " + targetStudent.getStudentName() + " 选课成功!");
}
} else {
System.out.println("未找到编号为 " + studentId + " 的学生!");
}
}
} else {
System.out.println("未找到编号为 " + courseId + " 的课程!");
}
}
// 学生退课
public void dropCourse(String studentId, String courseId) {
Course targetCourse = null;
for (Course course : courseList) {
if (course.getCourseId().equals(courseId)) {
targetCourse = course;
break;
}
}
if (targetCourse != null) {
Student targetStudent = null;
for (Student student : studentList) {
if (student.getStudentId().equals(studentId)) {
targetStudent = student;
break;
}
}
if (targetStudent != null) {
if (targetStudent.getSelectedCourses().contains(targetCourse)) {
targetStudent.getSelectedCourses().remove(targetCourse);
targetCourse.setSelectedNum(targetCourse.getSelectedNum() - 1);
System.out.println("学生 " + targetStudent.getStudentName() + " 退课成功!");
} else {
System.out.println("学生 " + targetStudent.getStudentName() + " 未选过该课程!");
}
} else {
System.out.println("未找到编号为 " + studentId + " 的学生!");
}
} else {
System.out.println("未找到编号为 " + courseId + " 的课程!");
}
}
// 学生已选课程查询
public void querySelectedCourse(String studentId) {
Student targetStudent = null;
for (Student student : studentList) {
if (student.getStudentId().equals(studentId)) {
targetStudent = student;
break;
}
}
if (targetStudent != null) {
if (targetStudent.getSelectedCourses().size() > 0) {
System.out.println("学生 " + targetStudent.getStudentName() + " 已选课程如下:");
for (Course course : targetStudent.getSelectedCourses()) {
System.out.println(course);
}
} else {
System.out.println("学生 " + targetStudent.getStudentName() + " 未选任何课程!");
}
} else {
System.out.println("未找到编号为 " + studentId + " 的学生!");
}
}
}
// 测试类
public class Test {
public static void main(String[] args) {
OnlineLearningSystem system = new OnlineLearningSystem();
// 添加课程
system.addCourse(new Course("C001", "Java程序设计", "Java基础知识和程序设计"));
system.addCourse(new Course("C002", "Web开发技术", "Web开发中的HTML、CSS、JavaScript等技术"));
system.addCourse(new Course("C003", "数据库设计", "数据库基础知识和设计技术"));
// 查询课程
system.queryCourse("Java");
// 修改课程信息
system.modifyCourse("C001", "Java程序设计(进阶)", "Java高级知识和程序设计");
// 学生选课
system.chooseCourse("S001", "C001");
system.chooseCourse("S001", "C002");
system.chooseCourse("S002", "C001");
system.chooseCourse("S003", "C001");
system.chooseCourse("S004", "C001");
system.chooseCourse("S005", "C001");
system.chooseCourse("S006", "C001");
system.chooseCourse("S007", "C001");
system.chooseCourse("S008", "C001");
system.chooseCourse("S009", "C001");
system.chooseCourse("S010", "C001");
system.chooseCourse("S011", "C001");
// 学生已选课程查询
system.querySelectedCourse("S001");
// 学生退课
system.dropCourse("S001", "C002");
// 课程撤销
system.removeCourse("C003");
}
}
```
以上代码实现了课程加入、撤销、课程信息修改、课程信息查询、学生选课、学生退课、学生已选课程查询等功能,您可以根据需要进行修改或完善。同时,需要注意的是,以上代码只是一个简单的实现示例,实际应用中需要考虑到更多的细节问题。
阅读全文