⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trend.java

📁 java中最响当当的模式:观察者模式
💻 JAVA
字号:
/**
do trend
*/
import java.io.*;
import java.lang.Double;

public class Trend implements Observer, DisplayShare
{
	private double charge=0;
	private AbsSubject myShare;
	private String trend="\0";
	/*three int variables to record in recent time the trend */
    private int recordIncreaseTime=0;
    private int recordDecreaseTime=0;
    private int recordKeepTime=0;

	public Trend(AbsSubject myShare)
	{
		/*register to the distributor*/
		this.myShare=myShare;
		myShare.registerObserver(this);
	}
	public void update(double charge)
	{
		doTrend(charge);
	}
	public void doTrend(double charge)
	{
        /*if it increases recordIncreaseTime++
        other trends set to 0
        */
		if(charge>this.charge)
		{
			recordIncreaseTime++;
			recordDecreaseTime=0;
			recordKeepTime=0;

		}
		/*the same meaning*/
		else if(charge<this.charge)
		{
			recordIncreaseTime=0;
			recordDecreaseTime++;
			recordKeepTime=0;

		}
		/*the same meaning*/
		else
		{
			recordIncreaseTime=0;
			recordDecreaseTime=0;
			recordKeepTime++;

		}
		this.charge=charge;
		/*set the trend increse decrese or no change*/
		if(recordIncreaseTime>=3)
		{
			trend="increasing";
		}
		else if(recordDecreaseTime>=3)
		{
			trend="decreasing";
		}
		else if(recordKeepTime>=3)
        {
		    trend="no change";
		}
		else
		{
            trend="\0";
		}
		display();
	}
	/*display*/
	public void display()
	{
		if(trend.equals("increasing"))
		{
			System.out.println("The charge of this Share may increase\n");
		}
		else if(trend.equals("decreasing"))
		{
			System.out.println("The charge of this Share may decrese\n");
		}
		else if(trend.equals("no change"))
		{
			System.out.println("The charge of this charge may not change\n");
		}
		else
		{
			System.out.println("No abvious trend can be seen\n");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -