Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     import java.util.ArrayList;
  2.  
  3.     public class StockMediator implements Mediator {
  4.  
  5.         private ArrayList<Colleague> colleagues;
  6.         private ArrayList<Stock> stonks;
  7.  
  8.         public StockMediator() {
  9.             colleagues = new ArrayList<>();
  10.             stonks = new ArrayList<>();
  11.         }
  12.  
  13.         public void addColleague(Colleague newColleague) {
  14.             colleagues.add(newColleague);
  15.         }
  16.  
  17.         public void addStock(Stock stock){
  18.             stonks.add(stock);
  19.         }
  20.  
  21.         public void stockChange() {
  22.             for (Stock stock : stonks) {
  23.                 if (stock.getFreshness()) {
  24.                     for (Colleague coll : colleagues) {
  25.                         coll.getNewPrice(stock.getStockPrice(), stock.getstockName());
  26.                     }
  27.                     stock.setOld();
  28.                 }
  29.             }
  30.  
  31.         }
  32.     }
  33.  
  34.  
  35.     public interface Mediator {
  36.  
  37.         public void stockChange();
  38.  
  39.         public void addColleague(Colleague colleague);
  40.  
  41.     }
  42.  
  43.  
  44.  
  45.     public abstract class Colleague {
  46.  
  47.         private Mediator mediator;
  48.  
  49.         public Colleague(Mediator newMediator) {
  50.             mediator = newMediator;
  51.             mediator.addColleague(this);
  52.         }
  53.  
  54.         public void getNewPrice(int newPrice, String name) {
  55.             System.out.println("New price for stock " + name + ": " + newPrice);
  56.         }
  57.     }
  58.  
  59.  
  60.  
  61.  
  62.     public class ŻakStox extends Colleague {
  63.  
  64.         public ŻakStox(Mediator newMediator) {
  65.             super(newMediator);
  66.  
  67.             System.out.println("ŻakStox signed up with the stockexchange\n");
  68.  
  69.         }
  70.  
  71.     }
  72.  
  73.  
  74.  
  75.     public class Stock {
  76.  
  77.         private String stockName = "";
  78.         private int stockPrice = 0;
  79.         private boolean ifNew = true;
  80.  
  81.  
  82.         public Stock(String stockName, int stockPrice) {
  83.  
  84.             this.stockName = stockName;
  85.             this.stockPrice = stockPrice;
  86.  
  87.         }
  88.  
  89.         public String getstockName() {
  90.             return stockName;
  91.         }
  92.  
  93.         public int getStockPrice() {
  94.         return stockPrice;
  95.         }
  96.  
  97.         public void setOld() {
  98.             this.ifNew = false;
  99.         }
  100.  
  101.         public boolean getFreshness() { return ifNew; }
  102.  
  103.         public void setNewPrice(int newPrice) {
  104.             ifNew = true;
  105.             stockPrice = newPrice;
  106.         }
  107.     }
  108.  
  109.  
  110. import java.util.ArrayList;
  111.  
  112. public class Main {
  113.  
  114.     public static void main(String[] args) {
  115.         StockMediator mediator = new StockMediator();
  116.  
  117.         ŻakStox żak = new ŻakStox(mediator);
  118.         ŻakStox żak1 = new ŻakStox(mediator);
  119.         Stock stock = new Stock("PWr", 666);
  120.         Stock stock1 = new Stock("Zaliczcie mi mpis", 13);
  121.         mediator.addStock(stock);
  122.         mediator.addStock(stock1);
  123.  
  124.         mediator.stockChange();
  125.  
  126.         stock.setNewPrice(15);
  127.         mediator.stockChange();
  128.  
  129.         stock.setNewPrice(737);
  130.         stock1.setNewPrice(42);
  131.         mediator.stockChange();
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement