用java写出用户可以添加自己的课程,包括课程名称、上课时间、上课地点等,并能够设定提醒时间
时间: 2023-11-22 14:55:43 浏览: 32
以下是一个简单的 Java 代码示例,用于添加课程和设置提醒时间:
```java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Course {
private String name;
private String time;
private String location;
private Date reminderTime;
public Course(String name, String time, String location) {
this.name = name;
this.time = time;
this.location = location;
}
public String getName() {
return name;
}
public String getTime() {
return time;
}
public String getLocation() {
return location;
}
public Date getReminderTime() {
return reminderTime;
}
public void setReminderTime(Date reminderTime) {
this.reminderTime = reminderTime;
}
public static void main(String[] args) {
List<Course> courses = new ArrayList<>();
// 添加课程
Course course1 = new Course("Java Programming", "Monday 9:00-11:00", "Room 101");
Course course2 = new Course("Database Systems", "Wednesday 14:00-16:00", "Room 102");
courses.add(course1);
courses.add(course2);
// 设置提醒时间
course1.setReminderTime(new Date(System.currentTimeMillis() + 60000)); // 提前一分钟提醒
course2.setReminderTime(new Date(System.currentTimeMillis() + 180000)); // 提前三分钟提醒
// 输出课程信息
for (Course course : courses) {
System.out.println("Course Name: " + course.getName());
System.out.println("Time: " + course.getTime());
System.out.println("Location: " + course.getLocation());
if (course.getReminderTime() != null) {
System.out.println("Reminder Time: " + course.getReminderTime());
}
System.out.println();
}
}
}
```
这个示例代码创建了一个 `Course` 类,该类包含课程名称、上课时间、上课地点和提醒时间等属性。在 `main` 方法中,我们创建了两个课程对象,并将它们添加到了一个课程列表中。然后,我们设置了每个课程的提醒时间,并输出了课程信息。