Skip to content

Commit 3741799

Browse files
authored
Feature/lab 02 (#3)
* [Lab_01] Added Increase, change time with tests, Readme * Added getters, setters and test for it * [Lab 01] Added improvements * [Lab 01] Removed target folder * Added alarms * start * Lab 02 * removed useless files
1 parent b1c3c84 commit 3741799

24 files changed

+1019
-162
lines changed
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package lab_01;
2+
3+
public class Alarm implements IAlarm {
4+
int hours;
5+
int minutes;
6+
7+
protected void checkValue(int value) {
8+
if (value < 0)
9+
throw new IllegalArgumentException("Value must be not negative!");
10+
}
11+
12+
@Override
13+
public void setHours(int hours) {
14+
try {
15+
checkValue(hours);
16+
this.hours = hours % 24;
17+
} catch (IllegalArgumentException e) {
18+
System.out.println(e.getMessage());
19+
}
20+
}
21+
22+
@Override
23+
public void setMinutes(int minutes) {
24+
try {
25+
checkValue(minutes);
26+
setHours(this.hours + minutes / 60);
27+
this.minutes = minutes % 60;
28+
} catch (IllegalArgumentException e) {
29+
System.out.println(e.getMessage());
30+
}
31+
}
32+
33+
@Override
34+
public void setSeconds(int seconds) {
35+
throw new IllegalArgumentException("Seconds are not supported!");
36+
}
37+
38+
@Override
39+
public int getHours() {
40+
return this.hours;
41+
}
42+
43+
@Override
44+
public int getMinutes() {
45+
return this.minutes;
46+
}
47+
48+
@Override
49+
public int getSeconds() {
50+
throw new IllegalArgumentException("Seconds are not supported!");
51+
}
52+
53+
@Override
54+
public void check(IClock clock) {
55+
if (this.hours == clock.getHours() && this.minutes == clock.getMinutes()) {
56+
System.out.println("!!!ALARM!!!");
57+
clock.printInformation();
58+
}
59+
}
60+
61+
@Override
62+
public void printTime() {
63+
System.out.println("\tAlarm: " + this.hours + "h. " + this.minutes + "m.");
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package lab_01;
2+
3+
public class AlarmAdvanced extends Alarm {
4+
int seconds;
5+
6+
@Override
7+
public void setSeconds(int seconds) {
8+
try {
9+
checkValue(seconds);
10+
setMinutes(this.minutes + seconds / 60);
11+
this.seconds = seconds % 60;
12+
} catch (IllegalArgumentException e) {
13+
System.out.println(e.getMessage());
14+
}
15+
}
16+
17+
@Override
18+
public int getSeconds() {
19+
return this.seconds;
20+
}
21+
22+
@Override
23+
public void check(IClock clock) {
24+
// System.out.println("this: " + this.hours + " " + this.minutes + " " + this.seconds + "\n" + "clock: " + clock.getHours() + " " + clock.getMinutes() + " " + clock.getSeconds());
25+
26+
if (this.hours == clock.getHours() && this.minutes == clock.getMinutes() && this.seconds == clock.getSeconds()) {
27+
System.out.println("!!!ALARM!!!");
28+
clock.printInformation();
29+
}
30+
}
31+
32+
@Override
33+
public void printTime() {
34+
System.out.println("\tAlarm: " + this.hours + "h. " + this.minutes + "m. " + this.seconds + "s.");
35+
}
36+
}
+87-64
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
package lab_01;
22

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>();
1818
}
1919

20-
protected void checkValue(int value) throws Exception {
20+
protected void checkValue(int value) {
2121
if (value < 0)
22-
throw new Exception("Value must be not negative!");
22+
throw new IllegalArgumentException("Value must be not negative!");
2323
}
2424

25+
@Override
2526
public void setBrand(String brand) {
2627
this.brand = brand;
2728
}
2829

29-
public void setCost(int cost) throws Exception {
30+
@Override
31+
public void setCost(int cost) {
3032
checkValue(cost);
3133
this.cost = cost;
3234
}
@@ -35,93 +37,114 @@ public void setHours(int hours) throws Exception {
3537
try {
3638
checkValue(hours);
3739
this.hours = hours % 24;
38-
} catch (IOException e) {
40+
} catch (IllegalArgumentException e) {
3941
System.out.println(e.getMessage());
4042
}
4143
}
4244

43-
public void setMinutes(int minutes) throws Exception {
45+
@Override
46+
public void setMinutes(int minutes) {
4447
try {
4548
checkValue(minutes);
4649
setHours(this.hours + minutes / 60);
4750
this.minutes = minutes % 60;
48-
} catch (IOException e) {
51+
} catch (IllegalArgumentException e) {
4952
System.out.println(e.getMessage());
5053
}
5154
}
5255

56+
@Override
57+
public void setSeconds(int seconds) {
58+
throw new IllegalArgumentException("Seconds are not supported!");
59+
}
60+
61+
@Override
5362
public int getHours() {
5463
return this.hours;
5564
}
5665

66+
@Override
5767
public int getMinutes() {
5868
return this.minutes;
5969
}
6070

71+
@Override
72+
public int getSeconds() {
73+
throw new IllegalArgumentException("Seconds are not supported!");
74+
}
75+
76+
@Override
6177
public String getBrand() {
6278
return this.brand;
6379
}
6480

81+
@Override
6582
public int getCost() {
6683
return this.cost;
6784
}
6885

69-
public void increaseTime(int value) throws Exception {
70-
try {
86+
public void increaseTime(int value) {
87+
try {
7188
checkValue(value);
72-
setMinutes((this.minutes + value));
73-
} catch (IOException e) {
89+
setMinutes(this.minutes + value);
90+
} catch (IllegalArgumentException e) {
7491
System.out.println(e.getMessage());
7592
}
7693
}
7794

78-
public void printInformation() {
79-
System.out.println("\tClock type: " + this.type + "\n\tBrand: " + this.brand +"\n\tCost: " + 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+
};
82115

83-
public void printTime() {
84-
System.out.println("\tTime: " + this.hours + "h. " + this.minutes + "m.");
85-
}
86-
87-
88-
public static class AdvancedClock extends Clock {
89-
private int seconds;
116+
}
90117

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+
}
93120

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!");
96127
}
128+
}
97129

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);
106133
}
134+
}
107135

108-
public int getSeconds() {
109-
return this.seconds;
110-
}
136+
@Override
137+
public void addAlarm(IAlarm alarm) {
138+
this.alarms.add(alarm);
139+
}
111140

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("\tClock type: " + type + "\n\tBrand: " + this.brand +"\n\tCost: " + this.cost);
144+
printTime();
145+
}
121146

122-
@Override
123-
public void printTime() {
124-
System.out.println("\tAdvanced Time: " + this.hours + "h. " + this.minutes + "m. " + this.seconds + "s. ");
125-
}
147+
protected void printTime() {
148+
System.out.println("\tTime: " + this.hours + "h. " + this.minutes + "m.");
126149
}
127150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package lab_01;
2+
3+
public class ClockAdvanced extends Clock {
4+
int seconds = 0;
5+
6+
public ClockAdvanced() {
7+
super();
8+
9+
this.type = "Advanced";
10+
this.step = 1000;
11+
}
12+
13+
@Override
14+
public void setSeconds(int seconds) {
15+
try {
16+
checkValue(seconds);
17+
setMinutes(this.minutes + seconds / 60);
18+
this.seconds = seconds % 60;
19+
} catch (IllegalArgumentException e) {
20+
System.out.println(e.getMessage());
21+
}
22+
}
23+
24+
@Override
25+
public int getSeconds() {
26+
return this.seconds;
27+
}
28+
29+
@Override
30+
public void increaseTime(int value) {
31+
try {
32+
checkValue(value);
33+
setSeconds((this.seconds + value));
34+
} catch (IllegalArgumentException e) {
35+
System.out.println(e.getMessage());
36+
}
37+
}
38+
39+
@Override
40+
protected void printTime() {
41+
System.out.println("\tAdvanced Time: " + this.hours + "h. " + this.minutes + "m. " + this.seconds + "s. ");
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package lab_01;
2+
3+
public enum ClockType {
4+
Common,
5+
Advanced
6+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package lab_01;
2+
3+
public class FAlarm {
4+
public static IAlarm build(ClockType type) {
5+
if (type == ClockType.Common)
6+
return new Alarm();
7+
else if (type == ClockType.Advanced)
8+
return new AlarmAdvanced();
9+
10+
return null;
11+
}
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package lab_01;
2+
3+
public class FClock {
4+
public static IClock build(ClockType type) {
5+
if (type == ClockType.Common)
6+
return new Clock();
7+
else if (type == ClockType.Advanced)
8+
return new ClockAdvanced();
9+
10+
return null;
11+
}
12+
}

0 commit comments

Comments
 (0)