-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding remaining Listerner files and tests
- Loading branch information
martin
committed
Apr 22, 2026
1 parent
2a120fe
commit eee4b84
Showing
5 changed files
with
231 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package millions.model; | ||
|
|
||
| public interface ExchangeListener { | ||
|
|
||
| void onWeekAdvanced(int newWeek); | ||
|
|
||
| void onTransactionCompleted(Transaction transaction); | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package millions.model; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| public interface PlayerListener { | ||
|
|
||
| void onMoneyChanged(BigDecimal newBalance); | ||
|
|
||
| void onPortfolioChanged(); | ||
|
|
||
| void onStatusChanged(String newStatus); | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package millions; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import millions.model.Exchange; | ||
| import millions.model.ExchangeListener; | ||
| import millions.model.Player; | ||
| import millions.model.Purchase; | ||
| import millions.model.Sale; | ||
| import millions.model.Share; | ||
| import millions.model.Stock; | ||
| import millions.model.Transaction; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /*** | ||
| * Small test class that implements the ExchangeListener. Adds to a list, so that we can easily check lengths and values | ||
| */ | ||
| class TestExchangeListener implements ExchangeListener { | ||
| List<Integer> weekEvents = new ArrayList<>(); | ||
| List<Transaction> transactionEvents = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void onWeekAdvanced(int newWeek) { | ||
| weekEvents.add(newWeek); | ||
| } | ||
|
|
||
| @Override | ||
| public void onTransactionCompleted(Transaction transaction) { | ||
| transactionEvents.add(transaction); | ||
| } | ||
| } | ||
|
|
||
| class ExchangeListenerTest { | ||
|
|
||
| private Exchange exchange; | ||
| private Player player; | ||
| private TestExchangeListener listener; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| Stock s1 = new Stock("AAPL", "Apple Inc.", BigDecimal.valueOf(100)); | ||
| Stock s2 = new Stock("GOOG", "Alphabet Inc.", BigDecimal.valueOf(200)); | ||
| Stock s3 = new Stock("NVDA", "NVidia Inc.", BigDecimal.valueOf(200)); | ||
| exchange = new Exchange("NASDAQ", List.of(s1, s2, s3)); | ||
| player = new Player("TestPlayer", BigDecimal.valueOf(10000)); | ||
|
|
||
| listener = new TestExchangeListener(); | ||
| exchange.addListener(listener); | ||
| } | ||
|
|
||
| @Test | ||
| void advanceNotifiesListener() { | ||
| exchange.advance(); | ||
| assertEquals(1, listener.weekEvents.size()); | ||
| assertEquals(2, listener.weekEvents.getFirst()); | ||
|
|
||
| exchange.advance(); | ||
| assertEquals(2, listener.weekEvents.size()); | ||
| assertEquals(3, listener.weekEvents.get(1)); | ||
| } | ||
|
|
||
| @Test | ||
| void buyNotifiesListener() { | ||
| exchange.buy("AAPL", player, 1); | ||
| assertEquals(1, listener.transactionEvents.size()); | ||
| assertTrue(listener.transactionEvents.getFirst() instanceof Purchase); | ||
| exchange.buy("NVDA", player, 1); | ||
| assertTrue( | ||
| listener.transactionEvents.getLast().getShare().getStock().getSymbol().equals("NVDA")); | ||
| } | ||
|
|
||
| @Test | ||
| void sellNotifiesListener() { | ||
| exchange.buy("AAPL", player, 1); | ||
| listener.transactionEvents.clear(); | ||
|
|
||
| Share share = player.getPortfolio().getShares().getFirst(); | ||
| exchange.sell(share, player); | ||
|
|
||
| assertEquals(1, listener.transactionEvents.size()); | ||
| assertTrue(listener.transactionEvents.getFirst() instanceof Sale); | ||
| } | ||
|
|
||
| @Test | ||
| void removeListenerStopsNotifications() { | ||
| exchange.advance(); | ||
| assertEquals(1, listener.weekEvents.size()); | ||
|
|
||
| exchange.removeListener(listener); | ||
| exchange.advance(); | ||
| assertEquals(1, listener.weekEvents.size()); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package millions; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import millions.model.*; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /*** | ||
| * Small test class that implements the PlayerListener. Adds to a list, so that we can easily check lengths and values | ||
| */ | ||
|
|
||
| class TestPlayerListener implements PlayerListener { | ||
| List<BigDecimal> moneyEvents = new ArrayList<>(); | ||
| int portfolioChangedCount = 0; | ||
| List<String> statusEvents = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void onMoneyChanged(BigDecimal newBalance) { | ||
| moneyEvents.add(newBalance); | ||
| } | ||
|
|
||
| @Override | ||
| public void onPortfolioChanged() { | ||
| portfolioChangedCount++; | ||
| } | ||
|
|
||
| @Override | ||
| public void onStatusChanged(String newStatus) { | ||
| statusEvents.add(newStatus); | ||
| } | ||
| } | ||
|
|
||
| class PlayerListenerTest { | ||
|
|
||
| private Player player; | ||
| private TestPlayerListener listener; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| player = new Player("TestPlayer", BigDecimal.valueOf(10000)); | ||
| listener = new TestPlayerListener(); | ||
| player.addListener(listener); | ||
| } | ||
|
|
||
| @Test | ||
| void addMoneyNotifiesListener() { | ||
| player.addMoney(BigDecimal.valueOf(500)); | ||
| assertEquals(1, listener.moneyEvents.size()); | ||
| assertEquals(BigDecimal.valueOf(10500), listener.moneyEvents.getFirst()); | ||
| } | ||
|
|
||
| @Test | ||
| void withdrawMoneyNotifiesListener() { | ||
| player.withdrawMoney(BigDecimal.valueOf(300)); | ||
| assertEquals(1, listener.moneyEvents.size()); | ||
| assertEquals(BigDecimal.valueOf(9700), listener.moneyEvents.getFirst()); | ||
| } | ||
|
|
||
| @Test | ||
| void addShareNotifiesPortfolioAndStatus() { | ||
| Stock stock = new Stock("AAPL", "Apple Inc.", BigDecimal.valueOf(100)); | ||
| Share share = new Share(stock, BigDecimal.valueOf(1), BigDecimal.valueOf(100)); | ||
|
|
||
| player.addShareToPortfolio(share); | ||
| assertEquals(1, listener.portfolioChangedCount); | ||
| assertEquals(1, listener.statusEvents.size()); | ||
| } | ||
|
|
||
| @Test | ||
| void removeShareNotifiesPortfolioAndStatus() { | ||
| Stock stock = new Stock("AAPL", "Apple Inc.", BigDecimal.valueOf(100)); | ||
| Share share = new Share(stock, BigDecimal.valueOf(1), BigDecimal.valueOf(100)); | ||
|
|
||
| player.addShareToPortfolio(share); | ||
| listener.portfolioChangedCount = 0; | ||
| listener.statusEvents.clear(); | ||
|
|
||
| player.removeShareFromPortfolio(share); | ||
| assertEquals(1, listener.portfolioChangedCount); | ||
| assertEquals(1, listener.statusEvents.size()); | ||
| } | ||
|
|
||
| @Test | ||
| void removeListenerStopsNotifications() { | ||
| player.addMoney(BigDecimal.valueOf(100)); | ||
| assertEquals(1, listener.moneyEvents.size()); | ||
|
|
||
| player.removeListener(listener); | ||
| player.addMoney(BigDecimal.valueOf(100)); | ||
| assertEquals(1, listener.moneyEvents.size()); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters