-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
84 lines (67 loc) · 1.37 KB
/
Clock.java
File metadata and controls
84 lines (67 loc) · 1.37 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
package clock;
public class Clock {
private ClockState state;
private int hour;
private int minute;
private int second;
private ClockObserver observer;
private final int MAXHOUR = 12;
private final int MAXMINUTE = 60;
private final int MAXSECOND = 60;
public Clock(){
setState(new DisplayTimeState());
hour = 0;
minute = 0;
second = 0;
}
public void increment(){
state.increment(this);
}
public void decrement(){
state.decrement(this);
}
public void changeMode(){
state.changeMode(this);
}
public void cancel(){
state.cancel(this);
}
public void timerTick(){
state.timerTick(this);
}
public void resigterObserver(ClockObserver observer) {
this.observer = observer;
}
public void setState(ClockState newState){
this.state = newState;
}
public StateName getStateName(){
return this.state.getName();
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = (hour+MAXHOUR)%MAXHOUR;
notifyObserver();
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = (minute+MAXMINUTE)%MAXMINUTE;
notifyObserver();
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = (second+MAXSECOND)%MAXSECOND;
notifyObserver();
}
private void notifyObserver(){
if(observer != null){
observer.update();
}
}
}