Skip to content

Commit

Permalink
feat: Adding Listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed Apr 22, 2026
1 parent df49c31 commit 2a120fe
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/main/java/millions/model/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -20,6 +21,7 @@ public class Exchange {
private Random random = new Random();
private final TransactionFactory purchaseFactory = new PurchaseFactory();
private final TransactionFactory saleFactory = new SaleFactory();
private final List<ExchangeListener> listeners = new ArrayList<>();

public Exchange(String name, List<Stock> stockList) {
this.name = name;
Expand Down Expand Up @@ -47,6 +49,7 @@ public Transaction buy(String symbol, Player player, BigDecimal quantity) {

Transaction purchase = purchaseFactory.createTransaction(shareToBuy, weekNumber);
purchase.commit(player);
notifyTransactionCompleted(purchase);

return purchase;
}
Expand All @@ -59,6 +62,7 @@ public Transaction sell(Share share, Player player) {
Transaction sale = saleFactory.createTransaction(share, weekNumber);

sale.commit(player);
notifyTransactionCompleted(sale);
return sale;
}

Expand Down Expand Up @@ -107,5 +111,26 @@ public void advance() {
.setScale(2, RoundingMode.HALF_UP));
// RoundingMode from AI suggestion
}
notifyWeekAdvanced();
}

public void addListener(ExchangeListener listener) {
listeners.add(listener);
}

public void removeListener(ExchangeListener listener) {
listeners.remove(listener);
}

private void notifyWeekAdvanced() {
for (ExchangeListener listener : listeners) {
listener.onWeekAdvanced(weekNumber);
}
}

private void notifyTransactionCompleted(Transaction transaction) {
for (ExchangeListener listener : listeners) {
listener.onTransactionCompleted(transaction);
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/millions/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class Player {
private String name;
Expand All @@ -11,6 +13,7 @@ public class Player {
private TransactionArchive transactionArchive;
// temporary attribute until a better solution is found
public int weeksTraded;
private final List<PlayerListener> listeners = new ArrayList<>();

public Player(String name, BigDecimal startingMoney) {
this.name = name;
Expand All @@ -33,13 +36,15 @@ public void addMoney(BigDecimal amount) {
throw new IllegalArgumentException("Amount cannot be null or negative");
}
this.money = this.money.add(amount);
notifyMoneyChanged();
}

public void withdrawMoney(BigDecimal amount) {
if (amount == null || amount.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Amount cannot be null or negative");
}
this.money = this.money.subtract(amount);
notifyMoneyChanged();
}

public String getStatus() {
Expand Down Expand Up @@ -70,11 +75,49 @@ public Portfolio getPortfolio() {
return this.portfolio;
}

public void addShareToPortfolio(Share share) {
this.portfolio.addShare(share);
notifyPortfolioChanged();
notifyStatusChanged();
}

public void removeShareFromPortfolio(Share share) {
this.portfolio.removeShare(share);
notifyPortfolioChanged();
notifyStatusChanged();
}

public BigDecimal getNetWorth() {
return this.money.add(this.portfolio.getNetWorth());
}

public TransactionArchive getTransactionArchive() {
return this.transactionArchive;
}

public void addListener(PlayerListener listener) {
listeners.add(listener);
}

public void removeListener(PlayerListener listener) {
listeners.remove(listener);
}

private void notifyMoneyChanged() {
for (PlayerListener listener : listeners) {
listener.onMoneyChanged(money);
}
}

private void notifyPortfolioChanged() {
for (PlayerListener listener : listeners) {
listener.onPortfolioChanged();
}
}

private void notifyStatusChanged() {
for (PlayerListener listener : listeners) {
listener.onStatusChanged(getStatus());
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/millions/model/Purchase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public void commit(Player player) {
throw new IllegalStateException("Not enought money");
}
player.withdrawMoney(getCalculator().calculateTotal());
player.getPortfolio().addShare(getShare());
// Don't reach directly to the portefolio object
player.addShareToPortfolio(getShare());
player.getTransactionArchive().add(this);
setCommitted(true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/millions/model/Sale.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void commit(Player player) {
throw new IllegalStateException("Does not own the share");
}
player.addMoney(getCalculator().calculateTotal());
player.getPortfolio().removeShare(getShare());
player.removeShareFromPortfolio(getShare());
player.getTransactionArchive().add(this);
setCommitted(true);
}
Expand Down

0 comments on commit 2a120fe

Please sign in to comment.