Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.98 KB | None | 0 0
  1. SUBJECT.JAVA
  2.  
  3. 01
  4. // This interface handles adding, deleting and updating
  5. 02
  6. // all observers
  7. 03
  8.  
  9. 04
  10. public interface Subject {
  11. 05
  12.  
  13. 06
  14. public void register(Observer o);
  15. 07
  16. public void unregister(Observer o);
  17. 08
  18. public void notifyObserver();
  19. 09
  20.  
  21. 10
  22. }
  23. OBSERVER.JAVA
  24.  
  25. 1
  26. // The Observers update method is called when the Subject changes
  27. 2
  28.  
  29. 3
  30. public interface Observer {
  31. 4
  32.  
  33. 5
  34. public void update(double ibmPrice, double aaplPrice, double googPrice);
  35. 6
  36.  
  37. 7
  38. }
  39. STOCKGRABBER.JAVA
  40.  
  41. 01
  42. import java.util.ArrayList;
  43. 02
  44.  
  45. 03
  46. // Uses the Subject interface to update all Observers
  47. 04
  48.  
  49. 05
  50. public class StockGrabber implements Subject{
  51. 06
  52.  
  53. 07
  54. private ArrayList<Observer> observers;
  55. 08
  56. private double ibmPrice;
  57. 09
  58. private double aaplPrice;
  59. 10
  60. private double googPrice;
  61. 11
  62.  
  63. 12
  64. public StockGrabber(){
  65. 13
  66.  
  67. 14
  68. // Creates an ArrayList to hold all observers
  69. 15
  70.  
  71. 16
  72. observers = new ArrayList<Observer>();
  73. 17
  74. }
  75. 18
  76.  
  77. 19
  78. public void register(Observer newObserver) {
  79. 20
  80.  
  81. 21
  82. // Adds a new observer to the ArrayList
  83. 22
  84.  
  85. 23
  86. observers.add(newObserver);
  87. 24
  88.  
  89. 25
  90. }
  91. 26
  92.  
  93. 27
  94. public void unregister(Observer deleteObserver) {
  95. 28
  96.  
  97. 29
  98. // Get the index of the observer to delete
  99. 30
  100.  
  101. 31
  102. int observerIndex = observers.indexOf(deleteObserver);
  103. 32
  104.  
  105. 33
  106. // Print out message (Have to increment index to match)
  107. 34
  108.  
  109. 35
  110. System.out.println("Observer " + (observerIndex+1) + " deleted");
  111. 36
  112.  
  113. 37
  114. // Removes observer from the ArrayList
  115. 38
  116.  
  117. 39
  118. observers.remove(observerIndex);
  119. 40
  120.  
  121. 41
  122. }
  123. 42
  124.  
  125. 43
  126. public void notifyObserver() {
  127. 44
  128.  
  129. 45
  130. // Cycle through all observers and notifies them of
  131. 46
  132. // price changes
  133. 47
  134.  
  135. 48
  136. for(Observer observer : observers){
  137. 49
  138.  
  139. 50
  140. observer.update(ibmPrice, aaplPrice, googPrice);
  141. 51
  142.  
  143. 52
  144. }
  145. 53
  146. }
  147. 54
  148.  
  149. 55
  150. // Change prices for all stocks and notifies observers of changes
  151. 56
  152.  
  153. 57
  154. public void setIBMPrice(double newIBMPrice){
  155. 58
  156.  
  157. 59
  158. this.ibmPrice = newIBMPrice;
  159. 60
  160.  
  161. 61
  162. notifyObserver();
  163. 62
  164.  
  165. 63
  166. }
  167. 64
  168.  
  169. 65
  170. public void setAAPLPrice(double newAAPLPrice){
  171. 66
  172.  
  173. 67
  174. this.aaplPrice = newAAPLPrice;
  175. 68
  176.  
  177. 69
  178. notifyObserver();
  179. 70
  180.  
  181. 71
  182. }
  183. 72
  184.  
  185. 73
  186. public void setGOOGPrice(double newGOOGPrice){
  187. 74
  188.  
  189. 75
  190. this.googPrice = newGOOGPrice;
  191. 76
  192.  
  193. 77
  194. notifyObserver();
  195. 78
  196.  
  197. 79
  198. }
  199. 80
  200.  
  201. 81
  202. }
  203. STOCKOBSERVER.JAVA
  204.  
  205. 01
  206. // Represents each Observer that is monitoring changes in the subject
  207. 02
  208.  
  209. 03
  210. public class StockObserver implements Observer {
  211. 04
  212.  
  213. 05
  214. private double ibmPrice;
  215. 06
  216. private double aaplPrice;
  217. 07
  218. private double googPrice;
  219. 08
  220.  
  221. 09
  222. // Static used as a counter
  223. 10
  224.  
  225. 11
  226. private static int observerIDTracker = 0;
  227. 12
  228.  
  229. 13
  230. // Used to track the observers
  231. 14
  232.  
  233. 15
  234. private int observerID;
  235. 16
  236.  
  237. 17
  238. // Will hold reference to the StockGrabber object
  239. 18
  240.  
  241. 19
  242. private Subject stockGrabber;
  243. 20
  244.  
  245. 21
  246. public StockObserver(Subject stockGrabber){
  247. 22
  248.  
  249. 23
  250. // Store the reference to the stockGrabber object so
  251. 24
  252. // I can make calls to its methods
  253. 25
  254.  
  255. 26
  256. this.stockGrabber = stockGrabber;
  257. 27
  258.  
  259. 28
  260. // Assign an observer ID and increment the static counter
  261. 29
  262.  
  263. 30
  264. this.observerID = ++observerIDTracker;
  265. 31
  266.  
  267. 32
  268. // Message notifies user of new observer
  269. 33
  270.  
  271. 34
  272. System.out.println("New Observer " + this.observerID);
  273. 35
  274.  
  275. 36
  276. // Add the observer to the Subjects ArrayList
  277. 37
  278.  
  279. 38
  280. stockGrabber.register(this);
  281. 39
  282.  
  283. 40
  284. }
  285. 41
  286.  
  287. 42
  288. // Called to update all observers
  289. 43
  290.  
  291. 44
  292. public void update(double ibmPrice, double aaplPrice, double googPrice) {
  293. 45
  294.  
  295. 46
  296. this.ibmPrice = ibmPrice;
  297. 47
  298. this.aaplPrice = aaplPrice;
  299. 48
  300. this.googPrice = googPrice;
  301. 49
  302.  
  303. 50
  304. printThePrices();
  305. 51
  306.  
  307. 52
  308. }
  309. 53
  310.  
  311. 54
  312. public void printThePrices(){
  313. 55
  314.  
  315. 56
  316. System.out.println(observerID + "\nIBM: " + ibmPrice + "\nAAPL: " +
  317. 57
  318. aaplPrice + "\nGOOG: " + googPrice + "\n");
  319. 58
  320.  
  321. 59
  322. }
  323. 60
  324.  
  325. 61
  326. }
  327. GRABSTOCKS.JAVA
  328.  
  329. 01
  330. public class GrabStocks{
  331. 02
  332.  
  333. 03
  334. public static void main(String[] args){
  335. 04
  336.  
  337. 05
  338. // Create the Subject object
  339. 06
  340. // It will handle updating all observers
  341. 07
  342. // as well as deleting and adding them
  343. 08
  344.  
  345. 09
  346. StockGrabber stockGrabber = new StockGrabber();
  347. 10
  348.  
  349. 11
  350. // Create an Observer that will be sent updates from Subject
  351. 12
  352.  
  353. 13
  354. StockObserver observer1 = new StockObserver(stockGrabber);
  355. 14
  356.  
  357. 15
  358. stockGrabber.setIBMPrice(197.00);
  359. 16
  360. stockGrabber.setAAPLPrice(677.60);
  361. 17
  362. stockGrabber.setGOOGPrice(676.40);
  363. 18
  364.  
  365. 19
  366. StockObserver observer2 = new StockObserver(stockGrabber);
  367. 20
  368.  
  369. 21
  370. stockGrabber.setIBMPrice(197.00);
  371. 22
  372. stockGrabber.setAAPLPrice(677.60);
  373. 23
  374. stockGrabber.setGOOGPrice(676.40);
  375. 24
  376.  
  377. 25
  378. // Delete one of the observers
  379. 26
  380.  
  381. 27
  382. // stockGrabber.unregister(observer2);
  383. 28
  384.  
  385. 29
  386. stockGrabber.setIBMPrice(197.00);
  387. 30
  388. stockGrabber.setAAPLPrice(677.60);
  389. 31
  390. stockGrabber.setGOOGPrice(676.40);
  391. 32
  392.  
  393. 33
  394. // Create 3 threads using the Runnable interface
  395. 34
  396. // GetTheStock implements Runnable, so it doesn't waste
  397. 35
  398. // its one extendable class option
  399. 36
  400.  
  401. 37
  402. Runnable getIBM = new GetTheStock(stockGrabber, 2, "IBM", 197.00);
  403. 38
  404. Runnable getAAPL = new GetTheStock(stockGrabber, 2, "AAPL", 677.60);
  405. 39
  406. Runnable getGOOG = new GetTheStock(stockGrabber, 2, "GOOG", 676.40);
  407. 40
  408.  
  409. 41
  410. // Call for the code in run to execute
  411. 42
  412.  
  413. 43
  414. new Thread(getIBM).start();
  415. 44
  416. new Thread(getAAPL).start();
  417. 45
  418. new Thread(getGOOG).start();
  419. 46
  420.  
  421. 47
  422.  
  423. 48
  424. }
  425. 49
  426.  
  427. 50
  428. }
  429. GETTHESTOCK.JAVA
  430.  
  431. view sourceprint?
  432. 01
  433. import java.text.DecimalFormat;
  434. 02
  435.  
  436. 03
  437. public class GetTheStock implements Runnable{
  438. 04
  439.  
  440. 05
  441. // Could be used to set how many seconds to wait
  442. 06
  443. // in Thread.sleep() below
  444. 07
  445.  
  446. 08
  447. // private int startTime;
  448. 09
  449. private String stock;
  450. 10
  451. private double price;
  452. 11
  453.  
  454. 12
  455. // Will hold reference to the StockGrabber object
  456. 13
  457.  
  458. 14
  459. private Subject stockGrabber;
  460. 15
  461.  
  462. 16
  463. public GetTheStock(Subject stockGrabber, int newStartTime, String newStock, double newPrice){
  464. 17
  465.  
  466. 18
  467. // Store the reference to the stockGrabber object so
  468. 19
  469. // I can make calls to its methods
  470. 20
  471.  
  472. 21
  473. this.stockGrabber = stockGrabber;
  474. 22
  475.  
  476. 23
  477. // startTime = newStartTime; Not used to have variable sleep time
  478. 24
  479. stock = newStock;
  480. 25
  481. price = newPrice;
  482. 26
  483.  
  484. 27
  485. }
  486. 28
  487.  
  488. 29
  489. public void run(){
  490. 30
  491.  
  492. 31
  493. for(int i = 1; i <= 20; i++){
  494. 32
  495.  
  496. 33
  497. try{
  498. 34
  499.  
  500. 35
  501. // Sleep for 2 seconds
  502. 36
  503. Thread.sleep(2000);
  504. 37
  505.  
  506. 38
  507. // Use Thread.sleep(startTime * 1000); to
  508. 39
  509. // make sleep time variable
  510. 40
  511. }
  512. 41
  513. catch(InterruptedException e)
  514. 42
  515. {}
  516. 43
  517.  
  518. 44
  519. // Generates a random number between -.03 and .03
  520. 45
  521.  
  522. 46
  523. double randNum = (Math.random() * (.06)) - .03;
  524. 47
  525.  
  526. 48
  527. // Formats decimals to 2 places
  528. 49
  529.  
  530. 50
  531. DecimalFormat df = new DecimalFormat("#.##");
  532. 51
  533.  
  534. 52
  535. // Change the price and then convert it back into a double
  536. 53
  537.  
  538. 54
  539. price = Double.valueOf(df.format((price + randNum)));
  540. 55
  541.  
  542. 56
  543. if(stock == "IBM") ((StockGrabber) stockGrabber).setIBMPrice(price);
  544. 57
  545. if(stock == "AAPL") ((StockGrabber) stockGrabber).setAAPLPrice(price);
  546. 58
  547. if(stock == "GOOG") ((StockGrabber) stockGrabber).setGOOGPrice(price);
  548. 59
  549.  
  550. 60
  551. System.out.println(stock + ": " + df.format((price + randNum)) +
  552. 61
  553. " " + df.format(randNum));
  554. 62
  555.  
  556. 63
  557. System.out.println();
  558. 64
  559.  
  560. 65
  561. }
  562. 66
  563. }
  564. 67
  565.  
  566. 68
  567. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement