-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathTimeSpan.java
More file actions
62 lines (54 loc) · 1.41 KB
/
TimeSpan.java
File metadata and controls
62 lines (54 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.task02;
public class TimeSpan {
private int hours;
private int minutes;
private int seconds;
public int getHours(){
return hours;
}
public int getMinutes(){
return minutes;
}
public int getSeconds(){
return seconds;
}
public void setHours(int hours){
this.hours = hours;
correct();
}
public void setMinutes(int minutes){
this.minutes = minutes;
correct();
}
public void setSeconds(int seconds){
this.seconds = seconds;
correct();
}
public TimeSpan(int hours, int minutes, int seconds){
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
correct();
}
public void add(TimeSpan time){
this.hours += time.hours;
this.minutes += time.minutes;
this.seconds += time.seconds;
correct();
}
public void subtract(TimeSpan time){
this.hours -= time.hours;
this.minutes -= time.minutes;
this.seconds -= time.seconds;
correct();
}
public String toString(){
return String.format("%dч %dм %dс", hours, minutes, seconds);
}
private void correct(){
int totalTime = hours* 3600 + minutes * 60 + seconds;
hours = totalTime / 3600;
minutes = (totalTime % 3600) / 60;
seconds = (totalTime % 3600) % 60;
}
}