public class MyTime {
private int hour;
private int minute;
private int second;
public MyTime(int hour, int minute, int second) { //构造方法
super();
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void addSecond(int sec){ //添加秒
check();
second += sec;
check();
}
public void addMinute(int min){ //添加分钟
check();
minute += min;
check();
}
public void addHour(int hou){ //添加小时
check();
hour += hou;
check();
}
public void subSecond(int sec){ //扣秒
check();
second -= sec;
check();
}
public void subMinute(int min){ //扣分钟
check();
minute -= min;
check();
}
public void subHour(int hou){ //扣小时
check();
hour -= hou;
check();
}
public void check(){ //检查时间是否符合常规
if(hour >= 24){
hour -= 24;
}
else if (hour < 0) {
hour += 24;
}
if(minute >= 60){
hour += 1;
minute %= 60;
}else if (minute < 0) {
minute += 60;
}
if(second >= 60){
minute += 1;
second %= 60;
}else if (second < 60) {
second += 60;
}
}
public void display(){ //显示时间
System.out.println(hour + "时 " + minute +"分 " + second + "秒");
}
public static void main(String[] args){ //测试代码
MyTime myTime = new MyTime(2,59,30);
myTime.addHour(21);
myTime.addSecond(45);
myTime.display();
}
}