-
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.
- Loading branch information
EspenTinius
committed
Mar 14, 2026
1 parent
6931e12
commit 1ad6d0a
Showing
1 changed file
with
92 additions
and
3 deletions.
There are no files selected for viewing
95 changes: 92 additions & 3 deletions
95
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ExchangeTest.java
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 |
|---|---|---|
| @@ -1,3 +1,92 @@ | ||
| public class ExchangeTest { | ||
|
|
||
| } | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class ExchangeTest { | ||
|
|
||
| @Test | ||
| void constructorSetsNameWeekAndStocksCorrectly() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
|
|
||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple, tesla)); | ||
|
|
||
| assertEquals("NASDAQ", exchange.getName()); | ||
| assertEquals(1, exchange.getWeek()); | ||
| assertTrue(exchange.hasStock("AAPL")); | ||
| assertTrue(exchange.hasStock("TSLA")); | ||
| } | ||
|
|
||
| @Test | ||
| void getStockReturnsCorrectStock() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
|
|
||
| Stock result = exchange.getStock("AAPL"); | ||
|
|
||
| assertSame(apple, result); | ||
| } | ||
|
|
||
| @Test | ||
| void findStocksReturnsMatchingStocksBySymbolOrCompany() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple, tesla)); | ||
|
|
||
| List<Stock> resultBySymbol = exchange.findStocks("AAP"); | ||
| List<Stock> resultByCompany = exchange.findStocks("tes"); | ||
|
|
||
| assertEquals(1, resultBySymbol.size()); | ||
| assertTrue(resultBySymbol.contains(apple)); | ||
|
|
||
| assertEquals(1, resultByCompany.size()); | ||
| assertTrue(resultByCompany.contains(tesla)); | ||
| } | ||
|
|
||
| @Test | ||
| void buyReturnsPurchaseAndWithdrawsMoneyFromPlayer() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("100")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
|
|
||
| Transaction transaction = exchange.buy("AAPL", new BigDecimal("2"), player); | ||
|
|
||
| assertInstanceOf(Purchase.class, transaction); | ||
| assertEquals(1, transaction.getWeek()); | ||
| assertEquals(new BigDecimal("2"), transaction.getShare().getQuantity()); | ||
| assertEquals(new BigDecimal("100"), transaction.getShare().getPurchasePrice()); | ||
| assertEquals(new BigDecimal("799.000"), player.getMoney()); | ||
| } | ||
|
|
||
| @Test | ||
| void sellReturnsSaleAndAddsMoneyToPlayer() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
| Share share = new Share(apple, new BigDecimal("2"), new BigDecimal("100")); | ||
|
|
||
| Transaction transaction = exchange.sell(share, player); | ||
|
|
||
| assertInstanceOf(Sale.class, transaction); | ||
| assertEquals(1, transaction.getWeek()); | ||
| assertSame(share, transaction.getShare()); | ||
| assertEquals(new BigDecimal("1267.9000"), player.getMoney()); | ||
| } | ||
|
|
||
| @Test | ||
| void advanceIncreasesWeekAndUpdatesStockPrice() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("100")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
|
|
||
| BigDecimal oldPrice = apple.getSalesPrice(); | ||
| exchange.advance(); | ||
|
|
||
| assertEquals(2, exchange.getWeek()); | ||
| assertNotEquals(oldPrice, apple.getSalesPrice()); | ||
| } | ||
| } |