-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMyTime.java
More file actions
185 lines (161 loc) · 3.73 KB
/
MyTime.java
File metadata and controls
185 lines (161 loc) · 3.73 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package src;
public class MyTime {
private int hour = 0;
private int minute = 0;
private int second = 0;
public MyTime(int hour, int minute, int second) {
setTime(hour, minute, second);
}
/**
* [setTime description]
* @param hour [description]
* @param minute [description]
* @param second [description]
*/
public void setTime(int hour, int minute, int second)
{
setHour(hour);
setMinute(minute);
setSecond(second);
}
/**
* [getHour description]
* @return [description]
*/
public int getHour() {
return hour;
}
/**
* [getMinute description]
* @return [description]
*/
public int getMinute() {
return minute;
}
/**
* [getSecond description]
* @return [description]
*/
public int getSecond() {
return second;
}
/**
* [setHour description]
* @param hour [description]
*/
public void setHour(int hour) {
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException("Not valid hour's value");
}
this.hour = hour;
}
/**
* [setMinute description]
* @param minute [description]
*/
public void setMinute(int minute) {
if (minute < 0 || minute > 59) {
throw new IllegalArgumentException("Not valid minute's value");
}
this.minute = minute;
}
/**
* [setSecond description]
* @param second [description]
*/
public void setSecond(int second) {
if (second < 0 || second > 59) {
throw new IllegalArgumentException("Not valid second's value");
}
this.second = second;
}
/**
* [nextSecond description]
* @return [description]
*/
public MyTime nextSecond()
{
try {
setTime(hour, minute, ++second);
return this;
}
catch(IllegalArgumentException e) {}
second = 0;
return nextMinute();
}
/**
* [nextMinute description]
* @return [description]
*/
public MyTime nextMinute()
{
try {
setTime(hour, ++minute, second);
return this;
}
catch(IllegalArgumentException e) {}
minute = 0;
return nextHour();
}
/**
* [nextHour description]
* @return [description]
*/
public MyTime nextHour()
{
try {
setTime(++hour, minute, second);
return this;
}
catch(IllegalArgumentException e) {}
hour = 0;
return this;
}
/**
* [previousSecond description]
* @return [description]
*/
public MyTime previousSecond()
{
try {
setTime(hour, minute, --second);
return this;
}
catch(IllegalArgumentException e) {}
second = 59;
return previousMinute();
}
/**
* [previousMinute description]
* @return [description]
*/
public MyTime previousMinute() {
try {
setTime(hour, --minute, second);
return this;
}
catch(IllegalArgumentException e) {}
minute = 59;
return previousHour();
}
/**
* [previousHour description]
* @return [description]
*/
public MyTime previousHour() {
try {
setTime(--hour, minute, second);
return this;
}
catch(IllegalArgumentException e) {}
hour = 23;
return this;
}
/**
* [toString description]
* @return [description]
*/
public String toString() {
return String.format("%1$02d:%2$02d:%3$02d", hour, minute, second);
}
}