[实验任务一]:股票提醒
当股票的价格上涨或下降5%时,会通知持有该股票的股民,当股民听到价格上涨的消息时会买股票,当价格下降时会大哭一场。
实验要求:
1. 提交源代码;
2. 提交类图;
3. 注意编程规范。
源码:
package 实验22;
public class Client {
public static void main(String[]args) {
// TODO Auto-generated constructor stub
Subject f= new Subject();
GuMin g1=new GuMin("李峰股民");
GuMin g2=new GuMin("王磊股民");
f.add(g1);
f.add(g2);
System.out.println("股票升了");
f.up(0.1);
System.out.println("比率小于0.05");
System.out.println();
f.down(0.001);
System.out.println("股票降了");
f.down(0.1);
System.out.println("比率小于0.05");
f.down(0.001);
}
private static class Subject {
public void add(GuMin g1) {
}
public void up(double v) {
}
public void down(double v) {
}
}
}
package 实验22;
public class GuMin implements Observer{
private String name;
public GuMin(String n) {
// TODO Auto-generated constructor stub
name=n;
}
@Override
public void moneyUp() {
// TODO Auto-generated method stub
System.out.println(name+"买了一些股票(股票升了)");
}
@Override
public void moneyDown() {
// TODO Auto-generated method stub
System.out.println(name+"股票降价了,哭了");
}
}
package 实验22;
public class GuPiao {
public GuPiao() {
// TODO Auto-generated constructor stub
}
/*
* public void add(Observer o) { super.add(o); }
*/
public void up(double a) {
// TODO Auto-generated method stub
Observer[] olist = new Observer[0];
if(a>0.05) {
for(Observer gum : olist){
gum.moneyUp();;
}
}
}
public void down(double a) {
// TODO Auto-generated method stub
Observer[] olist = new Observer[0];
if(a>0.05) {
for(Observer gum:olist){
gum.moneyDown();
}
}
}
}
package 实验22;
public interface Observer {
public void moneyUp();
public void moneyDown();
}
package 实验22;
import java.util.ArrayList;
public abstract class Subject {
protected ArrayList<Observer> olist=new ArrayList<>();
public Subject() {
// TODO Auto-generated constructor stub
}
public void add(Observer o) {
olist.add(o);
}
public void remove(Observer o) {
olist.remove(o);
}
public abstract void up(double a);
public abstract void down(double a);
}
标签:11,27,Observer,void,stub,generated,打卡,TODO,public From: https://www.cnblogs.com/yunbianshangdadun/p/17859995.html