Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the code from the link, needed in the project: COP4814 Assignment 3 (80

ID: 3917261 • Letter: H

Question

Here is the code from the link, needed in the project:

COP4814 Assignment 3 (80 Points in total) Stock Price Observer (75 points) Using a stock portfolio model, design interfaces and classes that implement the Observer Pattern. Be sure to read Chapter 2 in Head-First Design Pattens first. A typical stock portfolio contains a collection of investments, and online brokerages let you view your investments in arn application that periodically updates the display to reflect changing stock prices. Your program will write to standard output. You will probably need to build the program in steps: 1. Create an Observer interface with a method that receives a Map of ticker symbols and prices Copy the Subject and DisplayElement interfaces from Chapter 2 with no changes. Create a PriceData class that is more or less like the WeatherData class in Chapter 2. Rather than containing temperature, humidity, and pressure, this class should contain a Map of ticker symbols and stock prices. Make other changes to the class that seem appropriate. Create a PricesDisplay class that implements the Observer and DisplayElement interfaces. PricesDisplay contains a list of ticker symbols (as strings). It is much like the CurrentConditionsDisplay class from Chapter 2. Its Display method should display each ticker symbol and price for each stock in its list of stocks. To do this, it will need to get the prices from the prices map. This class operates as your logical stock portfolio, meaning that you will not define an actual Portfolio class Download and use the StockObserverTest class from my faculty website http:/l/users.cis fu edul-crahní which creates the necessary objects and simulates the changes in stock prices. Each time it changes the prices, it calls a methoo in the PriceData class that broadcasts the new prices to all observer objects. The program's output will resemble the following, where the two observer objects (one for each portfolio) have received a series of stock price updates and displays the information on the screen in pairs with a blank line between them as follows: 2. 3. 4. 5. Portfolio #00001, ASD- 42.50, ADM- 45.00, AAC-22.20, AFAS- ?2.50 Portfolio #00002. BSD-42.52, BOM- 45.00, -AC-22.20, BEAS-52.50 Portfoli? #00001, ASD _ 43.50, ADH-44.00, AAC-25-20, AFAS-55-50 Post-011-?0002, BSD-42.00, BDM_ 42.00, BAC-19.20, BEAS-47,50 (atc. Each portfolio contains a different is of stocks Submit a ZIP file called FirstnameLastnameA3 zip containing only Java source files. Please include a copy of my StockObserverTest java file in the submission, but do not make any changes to it. Do not use the package statement in any of your files. Each class should be in a separate file. I will compile and run from the command line. There should be no other files or folders even if you are a Mac User. (5 points) A Java submission must compile and run from the command line or it will earn a 0 and will not be graded further

Explanation / Answer

Solution:

PROGRAM CODE:

PriceData.java

package stocks;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

public class PriceData implements Subject{

  

   private Map<String, Double> prices;

   private ArrayList<Observer> observers;

  

   public PriceData() {

       prices = new HashMap<String, Double>();

       observers = new ArrayList<Observer>();

   }

  

   public void pricesChanged()

   {

       notifyObserver();

   }

   @Override

   public void registerObserver(Observer o) {

       observers.add(o);

      

   }

   @Override

   public void removeObserver(Observer o) {

       int i= observers.indexOf(o);

       if(i>=0)

           observers.remove(i);

      

   }

   @Override

   public void notifyObserver() {

       for(int i=0; i<observers.size(); i++)

       {

           Observer observer = (Observer) observers.get(i);

           observer.update(prices);

       }

      

   }

  

   public void setPrices(Map<String, Double> newPrices)

   {

       this.prices = newPrices;

       pricesChanged();

   }

  

   public Map<String, Double> getPrices()

   {

       return prices;

   }

}

PricesDisplay.java

package stocks;

import java.util.HashMap;

import java.util.Map;

public class PricesDisplay implements Observer, DisplayElement{

   private String tickerSymbols[];

   private String portfolio;

   private Subject priceData;

   private Map<String, Double> prices;

  

  

   public PricesDisplay(String portfolio, PriceData pricedata)

   {

       this.portfolio = portfolio;

       this.priceData = pricedata;

       prices = new HashMap<String, Double>();

       priceData.registerObserver(this);

   }

  

   @Override

   public void display() {

      

       System.out.print("Portfolio #" + portfolio + ", ");

       for(int i=0; i<tickerSymbols.length; i++){

          System.out.print(tickerSymbols[i] + " = " + prices.get(tickerSymbols[i]) + ", ");

       }

       System.out.println();

   }

   @Override

   public void update(Map<String, Double> prices) {

       this.prices = prices;

       display();

   }

   public void setStocks(String[] strings) {

       this.tickerSymbols = strings;

      

   }

  

}

Observer.java

package stocks;

import java.util.Map;

public interface Observer {

  

   public void update(Map<String, Double> priceData);

}

Subject.java

package stocks;

public interface Subject {

  

   public void registerObserver(Observer o);

   public void removeObserver(Observer o);

   public void notifyObserver();

}

DisplayElement.java

package stocks;

public interface DisplayElement {

  

   public void display();

}

OUTPUT:

Portfolio #00001, ASD = 42.5, ADM = 52.5, AAC = 22.2, AFAS = 45.0,

Portfolio #00002, BSD = 47.5, BDM = 21.5, BAC = 32.2, BFAS = 44.0,

Portfolio #00001, ASD = 43.35, ADM = 53.550000000000004, AAC = 22.644, AFAS = 45.9,

Portfolio #00002, BSD = 48.45, BDM = 21.93, BAC = 32.844, BFAS = 44.88,

Portfolio #00001, ASD = 41.1825, ADM = 50.8725, AAC = 21.511799999999997, AFAS = 43.605,

Portfolio #00002, BSD = 46.0275, BDM = 20.833499999999997, BAC = 31.2018, BFAS = 42.636,

Portfolio #00001, ASD = 43.65345, ADM = 53.924850000000006, AAC = 22.802508, AFAS = 46.2213,

Portfolio #00002, BSD = 48.78915000000001, BDM = 22.083509999999997, BAC = 33.073908, BFAS = 45.194160000000004,

Portfolio #00001, ASD = 44.9630535, ADM = 55.54259550000001, AAC = 23.48658324, AFAS = 47.607939,

Portfolio #00002, BSD = 50.25282450000001, BDM = 22.746015299999996, BAC = 34.066125240000005, BFAS = 46.549984800000004,

**********************************************************************************************************