Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SUBJECT.JAVA
- 01
- // This interface handles adding, deleting and updating
- 02
- // all observers
- 03
- 04
- public interface Subject {
- 05
- 06
- public void register(Observer o);
- 07
- public void unregister(Observer o);
- 08
- public void notifyObserver();
- 09
- 10
- }
- OBSERVER.JAVA
- 1
- // The Observers update method is called when the Subject changes
- 2
- 3
- public interface Observer {
- 4
- 5
- public void update(double ibmPrice, double aaplPrice, double googPrice);
- 6
- 7
- }
- STOCKGRABBER.JAVA
- 01
- import java.util.ArrayList;
- 02
- 03
- // Uses the Subject interface to update all Observers
- 04
- 05
- public class StockGrabber implements Subject{
- 06
- 07
- private ArrayList<Observer> observers;
- 08
- private double ibmPrice;
- 09
- private double aaplPrice;
- 10
- private double googPrice;
- 11
- 12
- public StockGrabber(){
- 13
- 14
- // Creates an ArrayList to hold all observers
- 15
- 16
- observers = new ArrayList<Observer>();
- 17
- }
- 18
- 19
- public void register(Observer newObserver) {
- 20
- 21
- // Adds a new observer to the ArrayList
- 22
- 23
- observers.add(newObserver);
- 24
- 25
- }
- 26
- 27
- public void unregister(Observer deleteObserver) {
- 28
- 29
- // Get the index of the observer to delete
- 30
- 31
- int observerIndex = observers.indexOf(deleteObserver);
- 32
- 33
- // Print out message (Have to increment index to match)
- 34
- 35
- System.out.println("Observer " + (observerIndex+1) + " deleted");
- 36
- 37
- // Removes observer from the ArrayList
- 38
- 39
- observers.remove(observerIndex);
- 40
- 41
- }
- 42
- 43
- public void notifyObserver() {
- 44
- 45
- // Cycle through all observers and notifies them of
- 46
- // price changes
- 47
- 48
- for(Observer observer : observers){
- 49
- 50
- observer.update(ibmPrice, aaplPrice, googPrice);
- 51
- 52
- }
- 53
- }
- 54
- 55
- // Change prices for all stocks and notifies observers of changes
- 56
- 57
- public void setIBMPrice(double newIBMPrice){
- 58
- 59
- this.ibmPrice = newIBMPrice;
- 60
- 61
- notifyObserver();
- 62
- 63
- }
- 64
- 65
- public void setAAPLPrice(double newAAPLPrice){
- 66
- 67
- this.aaplPrice = newAAPLPrice;
- 68
- 69
- notifyObserver();
- 70
- 71
- }
- 72
- 73
- public void setGOOGPrice(double newGOOGPrice){
- 74
- 75
- this.googPrice = newGOOGPrice;
- 76
- 77
- notifyObserver();
- 78
- 79
- }
- 80
- 81
- }
- STOCKOBSERVER.JAVA
- 01
- // Represents each Observer that is monitoring changes in the subject
- 02
- 03
- public class StockObserver implements Observer {
- 04
- 05
- private double ibmPrice;
- 06
- private double aaplPrice;
- 07
- private double googPrice;
- 08
- 09
- // Static used as a counter
- 10
- 11
- private static int observerIDTracker = 0;
- 12
- 13
- // Used to track the observers
- 14
- 15
- private int observerID;
- 16
- 17
- // Will hold reference to the StockGrabber object
- 18
- 19
- private Subject stockGrabber;
- 20
- 21
- public StockObserver(Subject stockGrabber){
- 22
- 23
- // Store the reference to the stockGrabber object so
- 24
- // I can make calls to its methods
- 25
- 26
- this.stockGrabber = stockGrabber;
- 27
- 28
- // Assign an observer ID and increment the static counter
- 29
- 30
- this.observerID = ++observerIDTracker;
- 31
- 32
- // Message notifies user of new observer
- 33
- 34
- System.out.println("New Observer " + this.observerID);
- 35
- 36
- // Add the observer to the Subjects ArrayList
- 37
- 38
- stockGrabber.register(this);
- 39
- 40
- }
- 41
- 42
- // Called to update all observers
- 43
- 44
- public void update(double ibmPrice, double aaplPrice, double googPrice) {
- 45
- 46
- this.ibmPrice = ibmPrice;
- 47
- this.aaplPrice = aaplPrice;
- 48
- this.googPrice = googPrice;
- 49
- 50
- printThePrices();
- 51
- 52
- }
- 53
- 54
- public void printThePrices(){
- 55
- 56
- System.out.println(observerID + "\nIBM: " + ibmPrice + "\nAAPL: " +
- 57
- aaplPrice + "\nGOOG: " + googPrice + "\n");
- 58
- 59
- }
- 60
- 61
- }
- GRABSTOCKS.JAVA
- 01
- public class GrabStocks{
- 02
- 03
- public static void main(String[] args){
- 04
- 05
- // Create the Subject object
- 06
- // It will handle updating all observers
- 07
- // as well as deleting and adding them
- 08
- 09
- StockGrabber stockGrabber = new StockGrabber();
- 10
- 11
- // Create an Observer that will be sent updates from Subject
- 12
- 13
- StockObserver observer1 = new StockObserver(stockGrabber);
- 14
- 15
- stockGrabber.setIBMPrice(197.00);
- 16
- stockGrabber.setAAPLPrice(677.60);
- 17
- stockGrabber.setGOOGPrice(676.40);
- 18
- 19
- StockObserver observer2 = new StockObserver(stockGrabber);
- 20
- 21
- stockGrabber.setIBMPrice(197.00);
- 22
- stockGrabber.setAAPLPrice(677.60);
- 23
- stockGrabber.setGOOGPrice(676.40);
- 24
- 25
- // Delete one of the observers
- 26
- 27
- // stockGrabber.unregister(observer2);
- 28
- 29
- stockGrabber.setIBMPrice(197.00);
- 30
- stockGrabber.setAAPLPrice(677.60);
- 31
- stockGrabber.setGOOGPrice(676.40);
- 32
- 33
- // Create 3 threads using the Runnable interface
- 34
- // GetTheStock implements Runnable, so it doesn't waste
- 35
- // its one extendable class option
- 36
- 37
- Runnable getIBM = new GetTheStock(stockGrabber, 2, "IBM", 197.00);
- 38
- Runnable getAAPL = new GetTheStock(stockGrabber, 2, "AAPL", 677.60);
- 39
- Runnable getGOOG = new GetTheStock(stockGrabber, 2, "GOOG", 676.40);
- 40
- 41
- // Call for the code in run to execute
- 42
- 43
- new Thread(getIBM).start();
- 44
- new Thread(getAAPL).start();
- 45
- new Thread(getGOOG).start();
- 46
- 47
- 48
- }
- 49
- 50
- }
- GETTHESTOCK.JAVA
- view sourceprint?
- 01
- import java.text.DecimalFormat;
- 02
- 03
- public class GetTheStock implements Runnable{
- 04
- 05
- // Could be used to set how many seconds to wait
- 06
- // in Thread.sleep() below
- 07
- 08
- // private int startTime;
- 09
- private String stock;
- 10
- private double price;
- 11
- 12
- // Will hold reference to the StockGrabber object
- 13
- 14
- private Subject stockGrabber;
- 15
- 16
- public GetTheStock(Subject stockGrabber, int newStartTime, String newStock, double newPrice){
- 17
- 18
- // Store the reference to the stockGrabber object so
- 19
- // I can make calls to its methods
- 20
- 21
- this.stockGrabber = stockGrabber;
- 22
- 23
- // startTime = newStartTime; Not used to have variable sleep time
- 24
- stock = newStock;
- 25
- price = newPrice;
- 26
- 27
- }
- 28
- 29
- public void run(){
- 30
- 31
- for(int i = 1; i <= 20; i++){
- 32
- 33
- try{
- 34
- 35
- // Sleep for 2 seconds
- 36
- Thread.sleep(2000);
- 37
- 38
- // Use Thread.sleep(startTime * 1000); to
- 39
- // make sleep time variable
- 40
- }
- 41
- catch(InterruptedException e)
- 42
- {}
- 43
- 44
- // Generates a random number between -.03 and .03
- 45
- 46
- double randNum = (Math.random() * (.06)) - .03;
- 47
- 48
- // Formats decimals to 2 places
- 49
- 50
- DecimalFormat df = new DecimalFormat("#.##");
- 51
- 52
- // Change the price and then convert it back into a double
- 53
- 54
- price = Double.valueOf(df.format((price + randNum)));
- 55
- 56
- if(stock == "IBM") ((StockGrabber) stockGrabber).setIBMPrice(price);
- 57
- if(stock == "AAPL") ((StockGrabber) stockGrabber).setAAPLPrice(price);
- 58
- if(stock == "GOOG") ((StockGrabber) stockGrabber).setGOOGPrice(price);
- 59
- 60
- System.out.println(stock + ": " + df.format((price + randNum)) +
- 61
- " " + df.format(randNum));
- 62
- 63
- System.out.println();
- 64
- 65
- }
- 66
- }
- 67
- 68
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement