1
1
package lab_01 ;
2
2
3
- import java .io . IOException ;
4
-
5
- public class Clock {
6
- protected String type ;
7
- private String brand ;
8
- private int cost ;
9
- protected int hours , minutes ;
10
-
11
- public Clock ( String brand , int cost , int hours , int minutes ) throws Exception {
12
- this . type = "Common" ;
13
- this . brand = brand ;
14
- this . cost = cost ;
15
-
16
- setHours ( hours );
17
- setMinutes ( minutes );
3
+ import java .util . ArrayList ;
4
+
5
+ public class Clock implements IClock {
6
+ int hours = 0 ;
7
+ int minutes = 0 ;
8
+ int cost = 0 ;
9
+ String type = "Common" ;
10
+ String brand = "Classic" ;
11
+ Thread t ;
12
+ int step = 60000 ;
13
+ boolean flag = false ;
14
+ ArrayList < IAlarm > alarms ;
15
+
16
+ public Clock () {
17
+ alarms = new ArrayList < IAlarm >( );
18
18
}
19
19
20
- protected void checkValue (int value ) throws Exception {
20
+ protected void checkValue (int value ) {
21
21
if (value < 0 )
22
- throw new Exception ("Value must be not negative!" );
22
+ throw new IllegalArgumentException ("Value must be not negative!" );
23
23
}
24
24
25
+ @ Override
25
26
public void setBrand (String brand ) {
26
27
this .brand = brand ;
27
28
}
28
29
29
- public void setCost (int cost ) throws Exception {
30
+ @ Override
31
+ public void setCost (int cost ) {
30
32
checkValue (cost );
31
33
this .cost = cost ;
32
34
}
@@ -35,93 +37,114 @@ public void setHours(int hours) throws Exception {
35
37
try {
36
38
checkValue (hours );
37
39
this .hours = hours % 24 ;
38
- } catch (IOException e ) {
40
+ } catch (IllegalArgumentException e ) {
39
41
System .out .println (e .getMessage ());
40
42
}
41
43
}
42
44
43
- public void setMinutes (int minutes ) throws Exception {
45
+ @ Override
46
+ public void setMinutes (int minutes ) {
44
47
try {
45
48
checkValue (minutes );
46
49
setHours (this .hours + minutes / 60 );
47
50
this .minutes = minutes % 60 ;
48
- } catch (IOException e ) {
51
+ } catch (IllegalArgumentException e ) {
49
52
System .out .println (e .getMessage ());
50
53
}
51
54
}
52
55
56
+ @ Override
57
+ public void setSeconds (int seconds ) {
58
+ throw new IllegalArgumentException ("Seconds are not supported!" );
59
+ }
60
+
61
+ @ Override
53
62
public int getHours () {
54
63
return this .hours ;
55
64
}
56
65
66
+ @ Override
57
67
public int getMinutes () {
58
68
return this .minutes ;
59
69
}
60
70
71
+ @ Override
72
+ public int getSeconds () {
73
+ throw new IllegalArgumentException ("Seconds are not supported!" );
74
+ }
75
+
76
+ @ Override
61
77
public String getBrand () {
62
78
return this .brand ;
63
79
}
64
80
81
+ @ Override
65
82
public int getCost () {
66
83
return this .cost ;
67
84
}
68
85
69
- public void increaseTime (int value ) throws Exception {
70
- try {
86
+ public void increaseTime (int value ) {
87
+ try {
71
88
checkValue (value );
72
- setMinutes (( this .minutes + value ) );
73
- } catch (IOException e ) {
89
+ setMinutes (this .minutes + value );
90
+ } catch (IllegalArgumentException e ) {
74
91
System .out .println (e .getMessage ());
75
92
}
76
93
}
77
94
78
- public void printInformation () {
79
- System .out .println ("\t Clock type: " + this .type + "\n \t Brand: " + this .brand +"\n \t Cost: " + this .cost );
80
- printTime ();
81
- }
95
+ @ Override
96
+ public void start () throws InterruptedException {
97
+ if (t == null ) {
98
+ t = new Thread () {
99
+ @ Override
100
+ public void run () {
101
+ flag = true ;
102
+ System .out .println ("Clock is starting!" );
103
+
104
+ while (flag ) {
105
+ try {
106
+ increaseTime (1 );
107
+ Thread .sleep (step );
108
+ checkAlarms ();
109
+ } catch (InterruptedException e ) {
110
+ flag = false ;
111
+ }
112
+ }
113
+ }
114
+ };
82
115
83
- public void printTime () {
84
- System .out .println ("\t Time: " + this .hours + "h. " + this .minutes + "m." );
85
- }
86
-
87
-
88
- public static class AdvancedClock extends Clock {
89
- private int seconds ;
116
+ }
90
117
91
- public AdvancedClock ( String brand , int cost , int hours , int minutes , int seconds ) throws Exception {
92
- super ( brand , cost , hours , minutes );
118
+ t . start ();
119
+ }
93
120
94
- this .type = "Advanced" ;
95
- setSeconds (seconds );
121
+ @ Override
122
+ public void stop () {
123
+ if (t != null ) {
124
+ t .interrupt ();
125
+ t = null ;
126
+ System .out .println ("Clock is ending!" );
96
127
}
128
+ }
97
129
98
- public void setSeconds (int seconds ) throws Exception {
99
- try {
100
- checkValue (seconds );
101
- setMinutes (this .minutes + seconds / 60 );
102
- this .seconds = seconds % 60 ;
103
- } catch (IOException e ) {
104
- System .out .println (e .getMessage ());
105
- }
130
+ private void checkAlarms () {
131
+ for (IAlarm alarm : this .alarms ) {
132
+ alarm .check (this );
106
133
}
134
+ }
107
135
108
- public int getSeconds () {
109
- return this .seconds ;
110
- }
136
+ @ Override
137
+ public void addAlarm (IAlarm alarm ) {
138
+ this .alarms .add (alarm );
139
+ }
111
140
112
- @ Override
113
- public void increaseTime (int value ) throws Exception {
114
- try {
115
- checkValue (value );
116
- setSeconds ((this .seconds + value ));
117
- } catch (IOException e ) {
118
- System .out .println (e .getMessage ());
119
- }
120
- }
141
+ @ Override
142
+ public void printInformation () {
143
+ System .out .println ("\t Clock type: " + type + "\n \t Brand: " + this .brand +"\n \t Cost: " + this .cost );
144
+ printTime ();
145
+ }
121
146
122
- @ Override
123
- public void printTime () {
124
- System .out .println ("\t Advanced Time: " + this .hours + "h. " + this .minutes + "m. " + this .seconds + "s. " );
125
- }
147
+ protected void printTime () {
148
+ System .out .println ("\t Time: " + this .hours + "h. " + this .minutes + "m." );
126
149
}
127
150
}
0 commit comments