用java编写一个在线学习系统时,实现课程加入、撤销、课程信息修改、课程信息查询等功能,请写出调试情况
时间: 2024-03-20 10:42:18 浏览: 16
好的,下面是一个简单的Java代码示例,用于实现在线学习系统中的课程管理功能:
```java
import java.util.ArrayList;
public class CourseManagementSystem {
private ArrayList<Course> courses = new ArrayList<Course>();
// 添加课程
public void addCourse(Course course) {
courses.add(course);
System.out.println("添加课程成功!");
}
// 撤销课程
public void deleteCourse(Course course) {
boolean success = courses.remove(course);
if (success) {
System.out.println("撤销课程成功!");
} else {
System.out.println("该课程不存在,无法进行撤销操作!");
}
}
// 修改课程信息
public void updateCourse(Course course, String newName, String newTeacher, String newTime) {
int index = courses.indexOf(course);
if (index >= 0) {
Course updatedCourse = courses.get(index);
updatedCourse.setName(newName);
updatedCourse.setTeacher(newTeacher);
updatedCourse.setTime(newTime);
System.out.println("修改课程信息成功!");
} else {
System.out.println("该课程不存在,无法进行修改操作!");
}
}
// 查询课程信息
public void queryCourse(Course course) {
int index = courses.indexOf(course);
if (index >= 0) {
Course queriedCourse = courses.get(index);
System.out.println("查询结果为:课程名称为" + queriedCourse.getName() + ",授课教师为" + queriedCourse.getTeacher() + ",上课时间为" + queriedCourse.getTime());
} else {
System.out.println("该课程不存在,无法进行查询操作!");
}
}
}
class Course {
private String name;
private String teacher;
private String time;
public Course(String name, String teacher, String time) {
this.name = name;
this.teacher = teacher;
this.time = time;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
```
调试情况如下:
假设我们已经实例化了一个CourseManagementSystem对象,现在我们需要进行一些调试,以确保代码可以按照预期执行。
首先,我们可以尝试添加一门新课程:
```java
Course course1 = new Course("Java编程", "张老师", "周二下午");
mySystem.addCourse(course1);
```
输出结果应该为:“添加课程成功!”
然后,我们可以尝试撤销这门课程:
```java
mySystem.deleteCourse(course1);
```
输出结果应该为:“撤销课程成功!”
接下来,我们可以尝试修改一门课程的信息:
```java
mySystem.updateCourse(course2, "Python编程", "李老师", "周三下午");
```
输出结果应该为:“修改课程信息成功!”
最后,我们可以尝试查询一门课程的信息:
```java
mySystem.queryCourse(course2);
```
输出结果应该为:“查询结果为:课程名称为Python编程,授课教师为李老师,上课时间为周三下午”。
如果以上步骤都能够顺利执行,那么我们可以认为代码已经成功实现了课程管理功能。