-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockGrabber.java
More file actions
90 lines (49 loc) · 1.71 KB
/
StockGrabber.java
File metadata and controls
90 lines (49 loc) · 1.71 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.ArrayList;
// Uses the Subject interface to update all Observers
public class StockGrabber implements Subject{
private ArrayList<Observer> observers;
private double ibmPrice;
private double aaplPrice;
private double googPrice;
private double intcPrice;
public StockGrabber(){
// Creates an ArrayList to hold all observers
observers = new ArrayList<Observer>();
}
public void register(Observer newObserver) {
// Adds a new observer to the ArrayList
observers.add(newObserver);
}
public void unregister(Observer deleteObserver) {
// Get the index of the observer to delete
int observerIndex = observers.indexOf(deleteObserver);
// Print out message (Have to increment index to match)
System.out.println("Observer " + (observerIndex+1) + " deleted");
// Removes observer from the ArrayList
observers.remove(observerIndex);
}
public void notifyObserver() {
// Cycle through all observers and notifies them of
// price changes
for(Observer observer : observers){
observer.update(ibmPrice, aaplPrice, googPrice, intcPrice);
}
}
// Change prices for all stocks and notifies observers of changes
public void setIBMPrice(double newIBMPrice){
this.ibmPrice = newIBMPrice;
notifyObserver();
}
public void setAAPLPrice(double newAAPLPrice){
this.aaplPrice = newAAPLPrice;
notifyObserver();
}
public void setGOOGPrice(double newGOOGPrice){
this.googPrice = newGOOGPrice;
notifyObserver();
}
public void setINTCPrice(double newINTCPrice){
this.intcPrice = newINTCPrice;
notifyObserver();
}
}