天气预报-Java

天气预报-Java
Display.java

public interface Display {
	public abstract void disPlay();
}

Observers.java

public interface Observers {
	public  abstract void update(float temperature, float humidity, float pressure);
	//温度、湿度、气压
}

Subject.java

public interface Subject {
	public abstract void registterObserver(Observers ob);
	public abstract void remove(Observers ob);
	public abstract void notifyObserver();
}

CurrentConditions.java

public class CurrentConditions implements Observers,Display{
	private float temperature; //温度
	private float humidity;   //湿度
	private float pressure;    //气压
	Subject  sub;
	public CurrentConditions(Subject  sub){//构造方法
		this.sub=sub;
		sub.registterObserver(this);
	}
	//方法重写
	public void disPlay() {
		System.out.println("温度:"+this.temperature+";湿度:"+humidity+";气压:"+pressure); 
	}
	public void update(float temperature, float humidity, float pressure) {
		this.temperature=temperature;
		this.humidity=humidity;
		this.pressure=pressure;
		this.disPlay();
	}
}

ForecastDisplay.java

public class ForecastDisplay implements Observers,Display{
	private float currentTemperature; //温度
	private float lastTemperature; //温度
	private float humidity;   //湿度
	private float pressure;    //气压
	Subject  sub;
	public ForecastDisplay(Subject  sub){
		this.sub=sub;
		sub.registterObserver(this);
	}
	//方法重写,重写Display、Observers
	public void disPlay() {
		if(lastTemperature>currentTemperature){
			System.out.println("天气降温");
		}
		else if(lastTemperature==currentTemperature){
			System.out.println("不变");
		}
		else{
			System.out.println("天气回暖");
		}
	}
	public void update(float temperature, float humidity, float pressure) {
		lastTemperature=currentTemperature;
		currentTemperature=temperature;
		this.humidity=humidity;
		this.pressure=pressure;
		disPlay();
	}
}

StatisticsDisplay.java

public class StatisticsDisplay implements Observers,Display{
	private float temperature; //温度
	private float maxT=-100;
	private float minT=250;
	private float sum;
	private int count ;
	private float humidity;   //湿度
	private float pressure;    //气压
	Subject  sub;
	
	public StatisticsDisplay(Subject  sub){//构造方法
		this.sub=sub;
		sub.registterObserver(this);
	}
	public void disPlay() {//方法重写,重写Display接口的方法
		System.out.println("最大值:"+maxT+";最小值"+minT+";评价温度:"+sum/count);
	}
	public void update(float temperature, float humidity, float pressure) {//方法重写,重写Observers
		this.temperature=temperature;
		this.humidity=humidity;
		this.pressure=pressure;
		if(temperature>maxT)
			maxT=temperature;
		if(temperature<minT)
			minT=temperature;
		sum=sum+temperature;
		count++;
		disPlay();	
	}
}

WeatherData.java

import java.util.ArrayList;
public class WeatherData implements Subject{
	private float temperature; //温度
	private float humidity;   //湿度
	private float pressure;    //气压
	
	private ArrayList<Observers>  list;
	
	public WeatherData(){//构造方法
		list=new ArrayList<Observers>();
	}
	//get方法
	public float getTemperature(){ 
		return temperature;
	} 
	public float getHumidity(){
		return humidity;
	} 
	public float getPressure(){
		return pressure;
	}
	//
	public void measurementsChanged(){ 
		this.notifyObserver();	
	}
	//方法重写
	public void registterObserver(Observers ob) {
		list.add(ob);
	}
	public void remove(Observers ob) {
		boolean flag=list.remove(ob);
		if(!flag){
			System.out.println("不需要注销");
		}
	}
	public void notifyObserver() {
		for (Observers one : list) {
			one.update(temperature, humidity, pressure);
		}
	}
	
	public void setMeasurements(float t,float h,float p){
		this.temperature=t;
		this.humidity =h;
		this.pressure=p;
		measurementsChanged();
	}
}
public class Test {

	public static void main(String[] args) {
		WeatherData weather=new WeatherData();
		CurrentConditions current=new CurrentConditions(weather);
		ForecastDisplay fore=new ForecastDisplay(weather);
		StatisticsDisplay stat=new StatisticsDisplay(weather);
		
		weather.setMeasurements(15, 70, 1010);
		try {
			Thread.sleep(1000*5);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		weather.setMeasurements(14, 69, 1010);
		try {
			Thread.sleep(1000*5);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		weather.setMeasurements(18, 69, 1010);
	}
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值