001package headfirst.observer.WeatherStation;
002
003import java.util.ArrayList;
004
005public class WeatherData implements Subject {
006        private ArrayList<Observer> observers;
007        private float temperature;
008        private float humidity;
009        private float pressure;
010
011        public WeatherData() {
012                observers = new ArrayList<Observer>();
013        }
014
015        public void registerObserver(Observer o) {
016                observers.add(o);
017        }
018
019        public void removeObserver(Observer o) {
020                int i = observers.indexOf(o);
021                if (i >= 0) {
022                        observers.remove(i);
023                }
024        }
025
026        public void notifyObservers() {
027                for (int i = 0; i < observers.size(); i++) {
028                        Observer observer = observers.get(i);
029                        observer.update(temperature, humidity, pressure);
030                }
031        }
032
033        public void measurementsChanged() {
034                notifyObservers();
035        }
036
037        public void setMeasurements(float temperature, float humidity, float pressure) {
038                this.temperature = temperature;
039                this.humidity = humidity;
040                this.pressure = pressure;
041                measurementsChanged();
042        }
043
044        // other WeatherData methods here
045
046        public float getTemperature() {
047                return temperature;
048        }
049
050        public float getHumidity() {
051                return humidity;
052        }
053
054        public float getPressure() {
055                return pressure;
056        }
057}