SHARE
TWEET

Untitled

a guest Jun 19th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This interface handles adding, deleting and updating
  2. // all observers
  3.  
  4. public interface Subject {
  5.    
  6.     public void register(Observer o);
  7.     public void unregister(Observer o);
  8.     public void notifyObserver();
  9.    
  10. }
  11.  
  12. // The Observers update method is called when the Subject changes
  13.  
  14. public interface Observer {
  15.    
  16.     public void update(double ibmPrice, double aaplPrice, double googPrice);
  17.    
  18. }
  19.  
  20. import java.util.ArrayList;
  21.  
  22. // Uses the Subject interface to update all Observers
  23.  
  24. public class StockGrabber implements Subject{
  25.    
  26.     private ArrayList<Observer> observers;
  27.     private double ibmPrice;
  28.     private double aaplPrice;
  29.     private double googPrice;
  30.    
  31.     public StockGrabber(){
  32.        
  33.         // Creates an ArrayList to hold all observers
  34.        
  35.         observers = new ArrayList<Observer>();
  36.     }
  37.    
  38.     public void register(Observer newObserver) {
  39.        
  40.         // Adds a new observer to the ArrayList
  41.        
  42.         observers.add(newObserver);
  43.        
  44.     }
  45.  
  46.     public void unregister(Observer deleteObserver) {
  47.        
  48.         // Get the index of the observer to delete
  49.        
  50.         int observerIndex = observers.indexOf(deleteObserver);
  51.        
  52.         // Print out message (Have to increment index to match)
  53.        
  54.         System.out.println("Observer " + (observerIndex+1) + " deleted");
  55.        
  56.         // Removes observer from the ArrayList
  57.        
  58.         observers.remove(observerIndex);
  59.        
  60.     }
  61.  
  62.     public void notifyObserver() {
  63.        
  64.         // Cycle through all observers and notifies them of
  65.         // price changes
  66.        
  67.         for(Observer observer : observers){
  68.            
  69.             observer.update(ibmPrice, aaplPrice, googPrice);
  70.            
  71.         }
  72.     }
  73.    
  74.     // Change prices for all stocks and notifies observers of changes
  75.    
  76.     public void setIBMPrice(double newIBMPrice){
  77.        
  78.         this.ibmPrice = newIBMPrice;
  79.        
  80.         notifyObserver();
  81.        
  82.     }
  83.    
  84.     public void setAAPLPrice(double newAAPLPrice){
  85.        
  86.         this.aaplPrice = newAAPLPrice;
  87.        
  88.         notifyObserver();
  89.        
  90.     }
  91.  
  92.     public void setGOOGPrice(double newGOOGPrice){
  93.    
  94.         this.googPrice = newGOOGPrice;
  95.    
  96.         notifyObserver();
  97.    
  98.     }
  99.    
  100. }
  101.  
  102. // Represents each Observer that is monitoring changes in the subject
  103.  
  104. public class StockObserver implements Observer {
  105.    
  106.     private double ibmPrice;
  107.     private double aaplPrice;
  108.     private double googPrice;
  109.    
  110.     // Static used as a counter
  111.    
  112.     private static int observerIDTracker = 0;
  113.    
  114.     // Used to track the observers
  115.    
  116.     private int observerID;
  117.    
  118.     // Will hold reference to the StockGrabber object
  119.    
  120.     private Subject stockGrabber;
  121.    
  122.     public StockObserver(Subject stockGrabber){
  123.        
  124.         // Store the reference to the stockGrabber object so
  125.         // I can make calls to its methods
  126.        
  127.         this.stockGrabber = stockGrabber;
  128.        
  129.         // Assign an observer ID and increment the static counter
  130.        
  131.         this.observerID = ++observerIDTracker;
  132.        
  133.         // Message notifies user of new observer
  134.        
  135.         System.out.println("New Observer " + this.observerID);
  136.        
  137.         // Add the observer to the Subjects ArrayList
  138.        
  139.         stockGrabber.register(this);
  140.        
  141.     }
  142.    
  143.     // Called to update all observers
  144.    
  145.     public void update(double ibmPrice, double aaplPrice, double googPrice) {
  146.        
  147.         this.ibmPrice = ibmPrice;
  148.         this.aaplPrice = aaplPrice;
  149.         this.googPrice = googPrice;
  150.        
  151.         printThePrices();
  152.        
  153.     }
  154.    
  155.     public void printThePrices(){
  156.        
  157.         System.out.println(observerID + "\nIBM: " + ibmPrice + "\nAAPL: " +
  158.                 aaplPrice + "\nGOOG: " + googPrice + "\n");
  159.        
  160.     }
  161.    
  162. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top