Files
design-patterns/03-behavioral/observer/PortfolioObserver.java

20 lines
655 B
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package observer;
/** Updates portfolio value whenever a held stock changes price */
public class PortfolioObserver implements StockObserver {
private final String ownerName;
private final int sharesHeld;
public PortfolioObserver(String ownerName, int sharesHeld) {
this.ownerName = ownerName;
this.sharesHeld = sharesHeld;
}
@Override
public void onPriceChanged(String ticker, double oldPrice, double newPrice) {
double gain = (newPrice - oldPrice) * sharesHeld;
System.out.printf(" [Portfolio:%s] %s×%d P&L change: %+.2f%n",
ownerName, ticker, sharesHeld, gain);
}
}