Skip to content

Commit 83d86f6

Browse files
author
java-tester-x
committed
Finish MyTime class
1 parent 1f016b7 commit 83d86f6

2 files changed

Lines changed: 233 additions & 0 deletions

File tree

src/MyTime.java

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package src;
2+
3+
4+
public class MyTime {
5+
6+
private int hour = 0;
7+
private int minute = 0;
8+
private int second = 0;
9+
10+
public MyTime(int hour, int minute, int second) {
11+
setTime(hour, minute, second);
12+
}
13+
14+
/**
15+
* [setTime description]
16+
* @param hour [description]
17+
* @param minute [description]
18+
* @param second [description]
19+
*/
20+
public void setTime(int hour, int minute, int second)
21+
{
22+
setHour(hour);
23+
setMinute(minute);
24+
setSecond(second);
25+
}
26+
27+
/**
28+
* [getHour description]
29+
* @return [description]
30+
*/
31+
public int getHour() {
32+
return hour;
33+
}
34+
35+
/**
36+
* [getMinute description]
37+
* @return [description]
38+
*/
39+
public int getMinute() {
40+
return minute;
41+
}
42+
43+
/**
44+
* [getSecond description]
45+
* @return [description]
46+
*/
47+
public int getSecond() {
48+
return second;
49+
}
50+
51+
/**
52+
* [setHour description]
53+
* @param hour [description]
54+
*/
55+
public void setHour(int hour) {
56+
if (hour < 0 || hour > 23) {
57+
throw new IllegalArgumentException("Not valid hour's value");
58+
}
59+
this.hour = hour;
60+
}
61+
62+
/**
63+
* [setMinute description]
64+
* @param minute [description]
65+
*/
66+
public void setMinute(int minute) {
67+
if (minute < 0 || minute > 59) {
68+
throw new IllegalArgumentException("Not valid minute's value");
69+
}
70+
this.minute = minute;
71+
}
72+
73+
/**
74+
* [setSecond description]
75+
* @param second [description]
76+
*/
77+
public void setSecond(int second) {
78+
if (second < 0 || second > 59) {
79+
throw new IllegalArgumentException("Not valid second's value");
80+
}
81+
this.second = second;
82+
}
83+
84+
/**
85+
* [nextSecond description]
86+
* @return [description]
87+
*/
88+
public MyTime nextSecond()
89+
{
90+
try {
91+
setTime(hour, minute, ++second);
92+
return this;
93+
}
94+
catch(IllegalArgumentException e) {}
95+
96+
second = 0;
97+
return nextMinute();
98+
}
99+
100+
/**
101+
* [nextMinute description]
102+
* @return [description]
103+
*/
104+
public MyTime nextMinute()
105+
{
106+
try {
107+
setTime(hour, ++minute, second);
108+
return this;
109+
}
110+
catch(IllegalArgumentException e) {}
111+
112+
minute = 0;
113+
return nextHour();
114+
}
115+
116+
/**
117+
* [nextHour description]
118+
* @return [description]
119+
*/
120+
public MyTime nextHour()
121+
{
122+
try {
123+
setTime(++hour, minute, second);
124+
return this;
125+
}
126+
catch(IllegalArgumentException e) {}
127+
128+
hour = 0;
129+
return this;
130+
}
131+
132+
/**
133+
* [previousSecond description]
134+
* @return [description]
135+
*/
136+
public MyTime previousSecond()
137+
{
138+
try {
139+
setTime(hour, minute, --second);
140+
return this;
141+
}
142+
catch(IllegalArgumentException e) {}
143+
144+
second = 59;
145+
return previousMinute();
146+
}
147+
148+
/**
149+
* [previousMinute description]
150+
* @return [description]
151+
*/
152+
public MyTime previousMinute() {
153+
try {
154+
setTime(hour, --minute, second);
155+
return this;
156+
}
157+
catch(IllegalArgumentException e) {}
158+
159+
minute = 59;
160+
return previousHour();
161+
}
162+
163+
/**
164+
* [previousHour description]
165+
* @return [description]
166+
*/
167+
public MyTime previousHour() {
168+
try {
169+
setTime(--hour, minute, second);
170+
return this;
171+
}
172+
catch(IllegalArgumentException e) {}
173+
174+
hour = 23;
175+
return this;
176+
}
177+
178+
/**
179+
* [toString description]
180+
* @return [description]
181+
*/
182+
public String toString() {
183+
return String.format("%1$02d:%2$02d:%3$02d", hour, minute, second);
184+
}
185+
}

src/TestMyTime.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package src;
2+
3+
public class TestMyTime {
4+
5+
public static void main(String[] args)
6+
{
7+
// additional task #1
8+
MyTime t1 = new MyTime(16, 59, 59);
9+
System.out.println("Time: "+ t1);
10+
System.out.println("Next second: " + t1.nextSecond());
11+
12+
t1 = new MyTime(16, 59, 59);
13+
System.out.println("Time: "+ t1);
14+
System.out.println("Next minute: " + t1.nextMinute());
15+
16+
t1 = new MyTime(16, 59, 59);
17+
System.out.println("Time: "+ t1);
18+
System.out.println("Next hour: " + t1.nextHour());
19+
20+
21+
22+
t1 = new MyTime(16, 59, 59);
23+
System.out.println("Time: "+ t1);
24+
System.out.println("Previous second: " + t1.previousSecond());
25+
26+
t1 = new MyTime(16, 59, 59);
27+
System.out.println("Time: "+ t1);
28+
System.out.println("Previous minute: " + t1.previousMinute());
29+
30+
t1 = new MyTime(16, 59, 59);
31+
System.out.println("Time: "+ t1);
32+
System.out.println("Previous hour: " + t1.previousHour());
33+
34+
35+
36+
t1 = new MyTime(16, 59, 0);
37+
System.out.println("Time: "+ t1);
38+
System.out.println("Previous second: " + t1.previousSecond());
39+
40+
t1 = new MyTime(16, 0, 59);
41+
System.out.println("Time: "+ t1);
42+
System.out.println("Previous minute: " + t1.previousMinute());
43+
44+
t1 = new MyTime(0, 59, 59);
45+
System.out.println("Time: "+ t1);
46+
System.out.println("Previous hour: " + t1.previousHour());
47+
}
48+
}

0 commit comments

Comments
 (0)