-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherStation.java
48 lines (43 loc) · 1.16 KB
/
WeatherStation.java
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
import java.util.ArrayList;
public class WeatherStation implements Subject{
private double temperature;
private double humidity;
private ArrayList<Observer>observers;
public WeatherStation(){
observers=new ArrayList<>();
};
@Override
public void register(Observer observer){
if(!observers.contains((observer)))
{
observers.add(observer);
}
else {
System.out.println("Observer already registered");
}
}
@Override
public void unregister(Observer observer){
if(observers.contains(observer))
{
observers.remove(observer);
}
else {
System.out.println("Observer not registered");
}
}
@Override
public void notifyobs(){
for(int i=0;i<observers.size();i++)
{
Observer observer=observers.get(i);
observer.update(this.temperature,this.humidity);
}
}
@Override
public void update(double updatedTemperature,double updatedHumidity){
this.temperature=updatedTemperature;
this.humidity=updatedHumidity;
notifyobs();
}
}