-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockObserver.java
More file actions
64 lines (37 loc) · 1.46 KB
/
StockObserver.java
File metadata and controls
64 lines (37 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Represents each Observer that is monitoring changes in the subject
public class StockObserver implements Observer {
private double ibmPrice;
private double aaplPrice;
private double googPrice;
private double intcPrice;
// Static used as a counter
private static int observerIDTracker = 0;
// Used to track the observers
private int observerID;
// Will hold reference to the StockGrabber object
@SuppressWarnings("unused")
private Subject stockGrabber;
public StockObserver(Subject stockGrabber){
// Store the reference to the stockGrabber object so
// I can make calls to its methods
this.stockGrabber = stockGrabber;
// Assign an observer ID and increment the static counter
this.observerID = ++observerIDTracker;
// Message notifies user of new observer
System.out.println("New Observer " + this.observerID);
// Add the observer to the Subjects ArrayList
stockGrabber.register(this);
}
// Called to update all observers
public void update(double ibmPrice, double aaplPrice, double googPrice, double intcPrice) {
this.ibmPrice = ibmPrice;
this.aaplPrice = aaplPrice;
this.googPrice = googPrice;
this.intcPrice = intcPrice;
printThePrices();
}
public void printThePrices(){
System.out.println(observerID + "\nIBM: " + ibmPrice + "\nAAPL: " +
aaplPrice + "\nGOOG: " + googPrice + "\nINTC: " + intcPrice + "\n");
}
}