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 prices;
private ArrayList observers;
public PriceData() {
prices = new HashMap();
observers = new ArrayList();
}
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 newPrices)
{
this.prices = newPrices;
pricesChanged();
}
public Map 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 prices;
public PricesDisplay(String portfolio, PriceData pricedata)
{
this.portfolio = portfolio;
this.priceData = pricedata;
prices = new HashMap();
priceData.registerObserver(this);
}
@Override
public void display() {
System.out.print(“Portfolio #” + portfolio + “, “);
for(int i=0; i 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 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,
“Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!”
What Students Are Saying About Us
.......... Customer ID: 12*** | Rating: ⭐⭐⭐⭐⭐"Honestly, I was afraid to send my paper to you, but splendidwritings.com proved they are a trustworthy service. My essay was done in less than a day, and I received a brilliant piece. I didn’t even believe it was my essay at first 🙂 Great job, thank you!"
.......... Customer ID: 14***| Rating: ⭐⭐⭐⭐⭐
"The company has some nice prices and good content. I ordered a term paper here and got a very good one. I'll keep ordering from this website."