From 86b4ec2af9a43e3aa95c151d70c65b41a9f097ef Mon Sep 17 00:00:00 2001 From: = Date: Wed, 18 Mar 2026 18:50:13 +0100 Subject: [PATCH] Test: Updated test classes Added unit tests for new methods added in previous commits --- .../idi/idatt2003/g40/mappe/ExchangeTest.java | 188 ++++++++++++------ .../idi/idatt2003/g40/mappe/PlayerTest.java | 77 ++++--- .../idatt2003/g40/mappe/PortfolioTest.java | 128 +++++++----- .../idi/idatt2003/g40/mappe/StockTest.java | 134 +++++++++---- 4 files changed, 339 insertions(+), 188 deletions(-) diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ExchangeTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ExchangeTest.java index 9e11d44..5cdd13d 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ExchangeTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ExchangeTest.java @@ -1,92 +1,148 @@ package edu.ntnu.idi.idatt2003.g40.mappe; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigDecimal; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +final class ExchangeTest { + + private Stock appleStock; + + @BeforeEach + void setUp() { + appleStock = new Stock("AAPL", "Apple", new BigDecimal("100")); + } + + @Test + void constructorSetsNameWeekAndStocksCorrectly() { + Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); + + Exchange exchange = new Exchange("NASDAQ", List.of(appleStock, tesla)); + + assertEquals("NASDAQ", exchange.getName()); + assertEquals(1, exchange.getWeek()); + assertTrue(exchange.hasStock("AAPL")); + assertTrue(exchange.hasStock("TSLA")); + } + + @Test + void getStockReturnsCorrectStock() { + Exchange exchange = new Exchange("NASDAQ", List.of(appleStock)); + + Stock result = exchange.getStock("AAPL"); + + assertSame(appleStock, result); + } + + @Test + void findStocksReturnsMatchingStocksBySymbolOrCompany() { + Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); + Exchange exchange = new Exchange("NASDAQ", List.of(appleStock, tesla)); + + List resultBySymbol = exchange.findStocks("AAP"); + List resultByCompany = exchange.findStocks("tes"); + + assertEquals(1, resultBySymbol.size()); + assertTrue(resultBySymbol.contains(appleStock)); + + assertEquals(1, resultByCompany.size()); + assertTrue(resultByCompany.contains(tesla)); + } + + @Test + void buyReturnsPurchaseAndWithdrawsMoneyFromPlayer() { + Exchange exchange = new Exchange("NASDAQ", List.of(appleStock)); + 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()); + } -class ExchangeTest { + @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")); - @Test - void constructorSetsNameWeekAndStocksCorrectly() { - Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); - Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); + Transaction transaction = exchange.sell(share, player); - Exchange exchange = new Exchange("NASDAQ", List.of(apple, tesla)); + assertInstanceOf(Sale.class, transaction); + assertEquals(1, transaction.getWeek()); + assertSame(share, transaction.getShare()); + assertEquals(new BigDecimal("1267.9000"), player.getMoney()); + } - assertEquals("NASDAQ", exchange.getName()); - assertEquals(1, exchange.getWeek()); - assertTrue(exchange.hasStock("AAPL")); - assertTrue(exchange.hasStock("TSLA")); - } + @Test + void advanceIncreasesWeekAndUpdatesStockPrice() { + Exchange exchange = new Exchange("NASDAQ", List.of(appleStock)); - @Test - void getStockReturnsCorrectStock() { - Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); - Exchange exchange = new Exchange("NASDAQ", List.of(apple)); + BigDecimal oldPrice = appleStock.getSalesPrice(); + exchange.advance(); - Stock result = exchange.getStock("AAPL"); + assertEquals(2, exchange.getWeek()); + assertNotEquals(oldPrice, appleStock.getSalesPrice()); + } - assertSame(apple, result); - } + @Test + void getGainersActuallyReturnsProperGainers() { + Stock teslaStock = new Stock("TSLA", "Tesla", new BigDecimal("200.00")); + Stock pearStock = new Stock("PEAR", "Pear inc.", new BigDecimal("97.00")); - @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)); + appleStock.addNewSalesPrice(new BigDecimal("150.00")); + teslaStock.addNewSalesPrice(new BigDecimal("230.00")); + pearStock.addNewSalesPrice(new BigDecimal("112.00")); - List resultBySymbol = exchange.findStocks("AAP"); - List resultByCompany = exchange.findStocks("tes"); + Exchange exchange = new Exchange("Exchange", List.of(appleStock, teslaStock, pearStock)); - assertEquals(1, resultBySymbol.size()); - assertTrue(resultBySymbol.contains(apple)); + List actualGainers = exchange.getGainers(2); - assertEquals(1, resultByCompany.size()); - assertTrue(resultByCompany.contains(tesla)); - } + boolean actualGainersContainLimitedGainers = actualGainers.contains(teslaStock) + && actualGainers.contains(appleStock); + assertTrue(actualGainersContainLimitedGainers); - @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")); + boolean actualGainersInCorrectOrder = actualGainers.getFirst() == appleStock; + assertTrue(actualGainersInCorrectOrder); - Transaction transaction = exchange.buy("AAPL", new BigDecimal("2"), player); + boolean actualGainersNotContainsGainerOutsideOfLimit = !actualGainers.contains(pearStock); + assertTrue(actualGainersNotContainsGainerOutsideOfLimit); + } - 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 getLosersActuallyReturnsProperLosers() { + Stock teslaStock = new Stock("TSLA", "Tesla", new BigDecimal("200.00")); + Stock pearStock = new Stock("PEAR", "Pear inc.", new BigDecimal("97.00")); - @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")); + appleStock.addNewSalesPrice(new BigDecimal("50.00")); + teslaStock.addNewSalesPrice(new BigDecimal("170.00")); + pearStock.addNewSalesPrice(new BigDecimal("82.00")); - Transaction transaction = exchange.sell(share, player); + Exchange exchange = new Exchange("Exchange", List.of(appleStock, teslaStock, pearStock)); - assertInstanceOf(Sale.class, transaction); - assertEquals(1, transaction.getWeek()); - assertSame(share, transaction.getShare()); - assertEquals(new BigDecimal("1267.9000"), player.getMoney()); - } + List actualLosers = exchange.getLosers(2); - @Test - void advanceIncreasesWeekAndUpdatesStockPrice() { - Stock apple = new Stock("AAPL", "Apple", new BigDecimal("100")); - Exchange exchange = new Exchange("NASDAQ", List.of(apple)); + boolean actualLosersContainsValidLosers = actualLosers.contains(teslaStock) + && actualLosers.contains(appleStock); + assertTrue(actualLosersContainsValidLosers); - BigDecimal oldPrice = apple.getSalesPrice(); - exchange.advance(); + boolean actualLosersInCorrectOrder = actualLosers.getFirst() == appleStock; + assertTrue(actualLosersInCorrectOrder); - assertEquals(2, exchange.getWeek()); - assertNotEquals(oldPrice, apple.getSalesPrice()); - } -} \ No newline at end of file + boolean actualLosersNotContainingLoserOutsideOfLimit = !actualLosers.contains(pearStock); + assertTrue(actualLosersNotContainingLoserOutsideOfLimit); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PlayerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PlayerTest.java index f62c0d5..b73f257 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PlayerTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PlayerTest.java @@ -1,48 +1,63 @@ package edu.ntnu.idi.idatt2003.g40.mappe; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.math.BigDecimal; +import org.junit.jupiter.api.Test; + +final class PlayerTest { + + @Test + void constructorSetsNameMoneyPortfolioAndArchive() { + Player player = new Player("Alice", new BigDecimal("1000")); + + assertEquals("Alice", player.getName()); + assertEquals(new BigDecimal("1000"), player.getMoney()); + assertNotNull(player.getPortfolio()); + assertNotNull(player.getTransactionArchive()); + } -import static org.junit.jupiter.api.Assertions.*; + @Test + void addMoneyIncreasesBalance() { + Player player = new Player("Bob", new BigDecimal("500")); -class PlayerTest { + player.addMoney(new BigDecimal("200")); - @Test - void constructorSetsNameMoneyPortfolioAndArchive() { - Player player = new Player("Alice", new BigDecimal("1000")); + assertEquals(new BigDecimal("700"), player.getMoney()); + } - assertEquals("Alice", player.getName()); - assertEquals(new BigDecimal("1000"), player.getMoney()); - assertNotNull(player.getPortfolio()); - assertNotNull(player.getTransactionArchive()); - } + @Test + void withdrawMoneyDecreasesBalance() { + Player player = new Player("Charlie", new BigDecimal("500")); - @Test - void addMoneyIncreasesBalance() { - Player player = new Player("Bob", new BigDecimal("500")); + player.withdrawMoney(new BigDecimal("150")); - player.addMoney(new BigDecimal("200")); + assertEquals(new BigDecimal("350"), player.getMoney()); + } - assertEquals(new BigDecimal("700"), player.getMoney()); - } + @Test + void addAndWithdrawMoneyUpdateBalanceCorrectly() { + Player player = new Player("Dana", new BigDecimal("1000")); - @Test - void withdrawMoneyDecreasesBalance() { - Player player = new Player("Charlie", new BigDecimal("500")); + player.addMoney(new BigDecimal("250")); + player.withdrawMoney(new BigDecimal("300")); - player.withdrawMoney(new BigDecimal("150")); + assertEquals(new BigDecimal("950"), player.getMoney()); + } - assertEquals(new BigDecimal("350"), player.getMoney()); - } + @Test + void getNetWorthCalculatesCorrectly() { + Stock stock = new Stock("AAPL", "Apple inc.,", new BigDecimal("100.00")); + Player player = new Player("Bob", new BigDecimal("900")); + Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("100.00")); - @Test - void addAndWithdrawMoneyUpdateBalanceCorrectly() { - Player player = new Player("Dana", new BigDecimal("1000")); + player.getPortfolio().addShare(share); + SaleCalculator saleCalculator = new SaleCalculator(share); - player.addMoney(new BigDecimal("250")); - player.withdrawMoney(new BigDecimal("300")); + BigDecimal calculatedNetWorth = player.getMoney().add(saleCalculator.calculateTotal()); + BigDecimal actualNetWorth = player.getNetWorth(); - assertEquals(new BigDecimal("950"), player.getMoney()); - } -} \ No newline at end of file + assertEquals(calculatedNetWorth, actualNetWorth); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PortfolioTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PortfolioTest.java index c8a5e3a..93fa5f4 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PortfolioTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PortfolioTest.java @@ -1,80 +1,102 @@ package edu.ntnu.idi.idatt2003.g40.mappe; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigDecimal; import java.util.List; +import org.junit.jupiter.api.Test; + +final class PortfolioTest { + + @Test + void addShareAddsShareToPortfolio() { + Portfolio portfolio = new Portfolio(); + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + + boolean result = portfolio.addShare(share); + + assertTrue(result); + assertTrue(portfolio.contains(share)); + } + + @Test + void removeShareRemovesShareFromPortfolio() { + Portfolio portfolio = new Portfolio(); + Stock stock = new Stock("TSLA", "Tesla", new BigDecimal("200")); + Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("200")); -import static org.junit.jupiter.api.Assertions.*; + portfolio.addShare(share); + boolean result = portfolio.removeShare(share); -class PortfolioTest { + assertTrue(result); + assertFalse(portfolio.contains(share)); + } - @Test - void addShareAddsShareToPortfolio() { - Portfolio portfolio = new Portfolio(); - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + @Test + void getSharesReturnsAllShares() { + Portfolio portfolio = new Portfolio(); - boolean result = portfolio.addShare(share); + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("3"), new BigDecimal("150")); - assertTrue(result); - assertTrue(portfolio.contains(share)); - } + portfolio.addShare(share); - @Test - void removeShareRemovesShareFromPortfolio() { - Portfolio portfolio = new Portfolio(); - Stock stock = new Stock("TSLA", "Tesla", new BigDecimal("200")); - Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("200")); + List shares = portfolio.getShares(); - portfolio.addShare(share); - boolean result = portfolio.removeShare(share); + assertEquals(1, shares.size()); + assertTrue(shares.contains(share)); + } - assertTrue(result); - assertFalse(portfolio.contains(share)); - } + @Test + void getSharesWithSymbolReturnsMatchingShares() { + Portfolio portfolio = new Portfolio(); - @Test - void getSharesReturnsAllShares() { - Portfolio portfolio = new Portfolio(); + Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); + Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("3"), new BigDecimal("150")); + Share appleShare = new Share(apple, + new BigDecimal("1"), + new BigDecimal("150")); + Share teslaShare = new Share(tesla, + new BigDecimal("1"), + new BigDecimal("200")); - portfolio.addShare(share); + portfolio.addShare(appleShare); + portfolio.addShare(teslaShare); - List shares = portfolio.getShares(); + List result = portfolio.getShares("AAPL"); - assertEquals(1, shares.size()); - assertTrue(shares.contains(share)); - } + assertEquals(1, result.size()); + assertTrue(result.contains(appleShare)); + } - @Test - void getSharesWithSymbolReturnsMatchingShares() { - Portfolio portfolio = new Portfolio(); + @Test + void containsReturnsFalseWhenShareNotPresent() { + Portfolio portfolio = new Portfolio(); - Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); - Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); + Stock stock = new Stock("NVDA", "Nvidia", new BigDecimal("800")); + Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("800")); - Share appleShare = new Share(apple, new BigDecimal("1"), new BigDecimal("150")); - Share teslaShare = new Share(tesla, new BigDecimal("1"), new BigDecimal("200")); + assertFalse(portfolio.contains(share)); + } - portfolio.addShare(appleShare); - portfolio.addShare(teslaShare); + @Test + void getNetWorthReturnsNetWorth() { + Portfolio portfolio = new Portfolio(); - List result = portfolio.getShares("AAPL"); + Stock stock = new Stock("NVDA", "Nvidia", new BigDecimal("800")); + Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("800")); + portfolio.addShare(share); - assertEquals(1, result.size()); - assertTrue(result.contains(appleShare)); - } + SaleCalculator saleCalculator = new SaleCalculator(share); - @Test - void containsReturnsFalseWhenShareNotPresent() { - Portfolio portfolio = new Portfolio(); + BigDecimal calculatedNetWorth = saleCalculator.calculateTotal(); - Stock stock = new Stock("NVDA", "Nvidia", new BigDecimal("800")); - Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("800")); + BigDecimal actualNetWorth = portfolio.getNetWorth(); - assertFalse(portfolio.contains(share)); - } -} \ No newline at end of file + assertEquals(calculatedNetWorth, actualNetWorth); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/StockTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/StockTest.java index 097cd27..1aaa672 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/StockTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/StockTest.java @@ -1,58 +1,116 @@ package edu.ntnu.idi.idatt2003.g40.mappe; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigDecimal; -import java.util.NoSuchElementException; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +final class StockTest { + + private Stock testStock; + + @BeforeEach + void setUp() { + testStock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + } + + @Test + void constructorSetsSymbolAndCompany() { + assertEquals("AAPL", testStock.getSymbol()); + assertEquals("Apple Inc.", testStock.getCompany()); + } + + /** + * Tests if getting price throws error on new stock. + * + * @deprecated as of part 2. + * */ + /* + @Test + void getSalesPrice_throwsWhenNoPricesExist_currentImplementation() { + Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + + // Because constructor does not add the initial salesPrice to prices, + // prices is empty and getLast() throws NoSuchElementException. + assertThrows(NoSuchElementException.class, stock::getSalesPrice); + }*/ + + @Test + void addNewSalesPriceThenGetSalesPriceReturnsLastAddedPrice() { + testStock.addNewSalesPrice(new BigDecimal("123.45")); + + assertEquals(new BigDecimal("123.45"), testStock.getSalesPrice()); + } + + @Test + void addNewSalesPriceTwiceGetSalesPriceReturnsMostRecent() { + testStock.addNewSalesPrice(new BigDecimal("10.00")); + testStock.addNewSalesPrice(new BigDecimal("20.00")); + + assertEquals(new BigDecimal("20.00"), testStock.getSalesPrice()); + } + + @Test + void addNewSalesPriceAllowsNullCurrentImplementation() { -import static org.junit.jupiter.api.Assertions.*; + testStock.addNewSalesPrice(null); -class StockTest { + // List allows nulls; getLast returns null -> should not throw. + assertDoesNotThrow(testStock::getSalesPrice); + assertNull(testStock.getSalesPrice()); + } - @Test - void constructor_setsSymbolAndCompany() { - Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + @Test + void getHistoricalPricesGetsAllPrices() { + testStock.addNewSalesPrice(new BigDecimal("200.00")); - assertEquals("AAPL", stock.getSymbol()); - assertEquals("Apple Inc.", stock.getCompany()); - } + List historicalPrices = testStock.getHistoricalPrices(); - @Test - void getSalesPrice_throwsWhenNoPricesExist_currentImplementation() { - Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + boolean historicalPricesContainBothValues = historicalPrices.contains(new BigDecimal("100.00")) + && historicalPrices.contains(new BigDecimal("200.00")); - // Because constructor does not add the initial salesPrice to prices, - // prices is empty and getLast() throws NoSuchElementException. - assertThrows(NoSuchElementException.class, stock::getSalesPrice); - } - @Test - void addNewSalesPrice_thenGetSalesPrice_returnsLastAddedPrice() { - Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + assertTrue(historicalPricesContainBothValues); + } - stock.addNewSalesPrice(new BigDecimal("123.45")); + @Test + void getHighestAndLowestPriceReturnsProperValues() { + // Values ranging from lowest to highest. + BigDecimal lowestPrice = new BigDecimal("98.00"); + BigDecimal fillerPrice1 = new BigDecimal("99.00"); + BigDecimal fillerPrice2 = new BigDecimal("101.00"); + BigDecimal highestPrice = new BigDecimal("102.00"); - assertEquals(new BigDecimal("123.45"), stock.getSalesPrice()); - } + // Random order + testStock.addNewSalesPrice(fillerPrice2); + testStock.addNewSalesPrice(highestPrice); + testStock.addNewSalesPrice(lowestPrice); + testStock.addNewSalesPrice(fillerPrice1); - @Test - void addNewSalesPrice_twice_getSalesPrice_returnsMostRecent() { - Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + assertEquals(lowestPrice, testStock.getLowestPrice()); + assertEquals(highestPrice, testStock.getHighestPrice()); + } - stock.addNewSalesPrice(new BigDecimal("10.00")); - stock.addNewSalesPrice(new BigDecimal("20.00")); + @Test + void getLatestPriceChangeGetsLatestPriceChangeWhenMultiplePrices() { + BigDecimal price1 = new BigDecimal("98.00"); + BigDecimal price2 = new BigDecimal("102.00"); - assertEquals(new BigDecimal("20.00"), stock.getSalesPrice()); - } + BigDecimal expectedPriceChange = price2.subtract(price1); - @Test - void addNewSalesPrice_allowsNull_currentImplementation() { - Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + testStock.addNewSalesPrice(price1); + testStock.addNewSalesPrice(price2); - stock.addNewSalesPrice(null); + assertEquals(expectedPriceChange, testStock.getLatestPriceChange()); + } - // List allows nulls; getLast returns null -> should not throw. - assertDoesNotThrow(stock::getSalesPrice); - assertNull(stock.getSalesPrice()); - } + @Test + void getLatestPriceChangeReturnsZeroWhenNotMultiplePrices() { + assertEquals(BigDecimal.ZERO, testStock.getLatestPriceChange()); + } }