I work in a small program using the Observable pattern. The code that I can't modify is provided below.
public abstract class ObservableStock {
private double price;
private StockType name;
public ObservableStock(StockType name) {
this.name = name;
}
protected ObservableStock() {
}
public StockType getName() {
return name;
}
public void setPrice(double price) {
this.price = price;
}
public abstract void notifyPriceChange(double price);
public abstract void registerStockExchangeCenter(ObserverStockExchangeCenter oc);
}
public abstract class ObserverStockExchangeCenter {
protected Map<StockType, Double> ownedStock;
public ObserverStockExchangeCenter() {
this.ownedStock = new HashMap<>();
}
public void buyStock(ObservableStock s) {
ownedStock.put(s.getName(), 0.0);
System.out.println("Helo");
}
//assume we won't change values in the map
public Map<StockType, Double> getOwnedStock() {
return ownedStock;
}
public abstract void observe(ObservableStock o);
public abstract void notifyChange(StockType type, double price);
}
public enum StockType {
Amazon,
Google,
}
I can modify the classes provided below:
public class ObservableStockImpl extends ObservableStock {
private ObserverStockExchangeCenter exchangeCenter;
public final StockType stockType;
public ObservableStockImpl(StockType name) {
this.stockType = name;
}
public void notifyPriceChange(double price) {
this.exchangeCenter.getOwnedStock().put(stockType, price);
this.exchangeCenter.notifyChange(stockType, price);
}
public void registerStockExchangeCenter(ObserverStockExchangeCenter oc) {
this.exchangeCenter = oc;
}
public StockType getStockType() {
return stockType;
}
}
public class ObserverStockExchangeCenterImpl extends ObserverStockExchangeCenter {
private final List<ObservableStockImpl> observableStocks;
public ObserverStockExchangeCenterImpl() {
super();
observableStocks = new ArrayList<>();
}
public void notifyChange(StockType type, double price) {
for (ObservableStockImpl os : observableStocks) {
if (os.getStockType().equals(type)) {
os.setPrice(price);
os.notifyPriceChange(price);
}
}
}
public void observe(ObservableStock o) {
observableStocks.add((ObservableStockImpl) o);
}
@Override
public void buyStock(ObservableStock s) {
ObservableStockImpl stock = (ObservableStockImpl) s;
ownedStock.put(stock.getStockType(), 0.0);
}
}
However, I failed to pass all the tests which are below:
@Test
public void stockPriceChangeTest(){
ObservableStock amazonStock = new ObservableStockImpl(StockType.Amazon);
ObserverStockExchangeCenter NYStockCenter = new ObserverStockExchangeCenterImpl();
NYStockCenter.buyStock(amazonStock);
Map<StockType, Double> boughtStocks = NYStockCenter.getOwnedStock();
assertEquals(1, boughtStocks.size());
assertEquals(0,boughtStocks.get(StockType.Amazon),0);
amazonStock.setPrice(5);
Map<StockType, Double> boughtStocks2 = NYStockCenter.getOwnedStock();
assertEquals(1, boughtStocks2.size());
// failing below
assertEquals(5,boughtStocks2.get(StockType.Amazon),0);
}
The issue is stock price increase not reflected in the code. Can anyone help me to find what is the issue here?
Aucun commentaire:
Enregistrer un commentaire