001package headfirst.observer.WeatherStationObservable; 002 003import java.util.Observable; 004import java.util.Observer; 005 006public class ForecastDisplay implements Observer, DisplayElement { 007 private float currentPressure = 29.92f; 008 private float lastPressure; 009 010 public ForecastDisplay(Observable observable) { 011 observable.addObserver(this); 012 } 013 014 public void update(Observable observable, Object arg) { 015 if (observable instanceof WeatherData) { 016 WeatherData weatherData = (WeatherData)observable; 017 lastPressure = currentPressure; 018 currentPressure = weatherData.getPressure(); 019 display(); 020 } 021 } 022 023 public void display() { 024 System.out.print("Forecast: "); 025 if (currentPressure > lastPressure) { 026 System.out.println("Improving weather on the way!"); 027 } else if (currentPressure == lastPressure) { 028 System.out.println("More of the same"); 029 } else if (currentPressure < lastPressure) { 030 System.out.println("Watch out for cooler, rainy weather"); 031 } 032 } 033}