001package headfirst.observer.WeatherStationObservable;
002
003import java.util.Observable;
004import java.util.Observer;
005
006public class StatisticsDisplay implements Observer, DisplayElement {
007        private float maxTemp = 0.0f;
008        private float minTemp = 200;
009        private float tempSum= 0.0f;
010        private int numReadings;
011
012        public StatisticsDisplay(Observable observable) {
013                observable.addObserver(this);
014        }
015
016        public void update(Observable observable, Object arg) {
017                if (observable instanceof WeatherData) {
018                        WeatherData weatherData = (WeatherData)observable;
019                        float temp = weatherData.getTemperature();
020                        tempSum += temp;
021                        numReadings++;
022
023                        if (temp > maxTemp) {
024                                maxTemp = temp;
025                        }
026
027                        if (temp < minTemp) {
028                                minTemp = temp;
029                        }
030
031                        display();
032                }
033        }
034
035        public void display() {
036                System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)
037                                + "/" + maxTemp + "/" + minTemp);
038        }
039}