用Java实现已有一个日期类Date,包括三个protected成员数据 int year; int month; int day; 另有一个时间类Time,包括三个protected成员数据 int hour; int minute; int second; 现需根据输入的日程的日期时间,安排前后顺序,为此以Date类和Time类为基类,建立一个日程类Schedule,包括以下新增成员: int ID;//日程的ID bool operator < (const Schedule & s2);//判断当前日程时间是否早于s2 生成以上类,并编写主函数,根据输入的各项日程信息,建立日程对象,找出需要最早安排的日程,并输出该日程对象的信息。 输入格式: 测试输入包含若干日程,每个日程占一行(日程编号ID 日程日期(****//)日程时间(::**))。当读入0时输入结束,相应的结果不要输出。 输入样例: 1 2014/06/27 08:00:01 2 2014/06/28 08:00:01 0 输出样例: The urgent schedule is No.1: 2014/6/27 8:0:1
时间: 2024-02-05 14:14:20 浏览: 89
以下是Java实现的代码:
```
import java.util.Scanner;
public class Schedule extends Date {
private int ID;
private Time time;
public Schedule(int year, int month, int day, int hour, int minute, int second, int ID) {
super(year, month, day);
this.time = new Time(hour, minute, second);
this.ID = ID;
}
public int getID() {
return ID;
}
public boolean isEarlierThan(Schedule s2) {
if (this.getYear() < s2.getYear()) {
return true;
} else if (this.getYear() == s2.getYear() && this.getMonth() < s2.getMonth()) {
return true;
} else if (this.getYear() == s2.getYear() && this.getMonth() == s2.getMonth() && this.getDay() < s2.getDay()) {
return true;
} else if (this.getYear() == s2.getYear() && this.getMonth() == s2.getMonth() && this.getDay() == s2.getDay()
&& this.time.isEarlierThan(s2.time)) {
return true;
} else {
return false;
}
}
public String toString() {
return "No." + ID + ": " + super.toString() + " " + time.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Schedule earliestSchedule = null;
while (true) {
int ID = sc.nextInt();
if (ID == 0) {
break;
}
String dateStr = sc.next();
String timeStr = sc.next();
String[] dateArr = dateStr.split("/");
int year = Integer.parseInt(dateArr[0]);
int month = Integer.parseInt(dateArr[1]);
int day = Integer.parseInt(dateArr[2]);
String[] timeArr = timeStr.split(":");
int hour = Integer.parseInt(timeArr[0]);
int minute = Integer.parseInt(timeArr[1]);
int second = Integer.parseInt(timeArr[2]);
Schedule schedule = new Schedule(year, month, day, hour, minute, second, ID);
if (earliestSchedule == null || schedule.isEarlierThan(earliestSchedule)) {
earliestSchedule = schedule;
}
}
System.out.println("The urgent schedule is " + earliestSchedule.toString());
}
}
class Time {
private int hour;
private int minute;
private int second;
public Time(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public boolean isEarlierThan(Time t2) {
if (this.hour < t2.hour) {
return true;
} else if (this.hour == t2.hour && this.minute < t2.minute) {
return true;
} else if (this.hour == t2.hour && this.minute == t2.minute && this.second < t2.second) {
return true;
} else {
return false;
}
}
public String toString() {
return String.format("%d:%d:%d", hour, minute, second);
}
}
class Date {
protected int year;
protected int month;
protected int day;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public String toString() {
return String.format("%d/%d/%d", year, month, day);
}
}
```
注意:上述代码实现的是日程时间的早晚比较,而非小于运算符重载。在Java中,小于运算符重载只能用于基本类型的比较,因此这里使用了一个名为“isEarlierThan”的方法来实现日程时间的比较。
阅读全文