From 6a0165c431337f8428fbf74b72942da09f7b2ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Sk=C3=B8ien?= Date: Thu, 9 Apr 2026 22:24:56 +0200 Subject: [PATCH 1/2] add tests for StockFileHandler and refactor getNetworthTest in player --- .../idatt2003/gruppe42/Model/PlayerTest.java | 14 ++++--- .../gruppe42/Model/StockFileHandlerTest.java | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockFileHandlerTest.java diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java index c3696dd..6ca36bb 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java @@ -64,6 +64,7 @@ void getTransactionArchiveTest() { void getMoneyTest() { assertEquals(new BigDecimal("10000"), player.getMoney()); } + @Test void withdrawExactAmountTest() { player.withdrawMoney(new BigDecimal("10000")); @@ -72,11 +73,14 @@ void withdrawExactAmountTest() { @Test void getNetworthTest() { + BigDecimal originalMoney = player.getMoney(); + Portfolio portfolio = player.getPortfolio(); + + Share share = new Share(new Stock("AAPL", "Apple Inc.", new BigDecimal("276.43")), new BigDecimal("10"), new BigDecimal("50")); + portfolio.addShare(share); + + player.getNetWorth(); - // Starting with 10000 money and adding 5000 moneys worth of AAPL then calculating the networth - // with commissions taken account of will result in this: - // 10000 + 5000 - (5000 * 0.01) = 14950 - player.getPortfolio().addShare(new Share(new Stock("AAPL", "Apple Inc.", new BigDecimal("5000")), new BigDecimal("1"), new BigDecimal("5000"))); - assertEquals(new BigDecimal("14950"), player.getNetWorth()); + assertEquals(originalMoney, player.getMoney()); } } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockFileHandlerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockFileHandlerTest.java new file mode 100644 index 0000000..cd6d20b --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockFileHandlerTest.java @@ -0,0 +1,40 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Model; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.math.BigDecimal; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.Test; + +public class StockFileHandlerTest { + + @Test + void readFromFileTest() throws IOException { + Path testFile = Files.createTempFile("stocks", "csv"); + Files.writeString(testFile, "AAPL,Apple Inc.,276.43\nMSFT,Microsoft,404.68"); + List stocks = StockFileHandler.readFromFile(testFile); + assertEquals(2, stocks.size()); + assertEquals("AAPL", stocks.get(0).getSymbol()); + } + + @Test + void readFromFileWithCommentsAndEmptyLines() throws IOException { + Path testFile = Files.createTempFile("stocks", "csv"); + Files.writeString(testFile, "#Comment\n\nAAPL,Apple Inc.,276.43"); + List stocks = StockFileHandler.readFromFile(testFile); + assertEquals(1, stocks.size()); + } + + @Test + void writeToFileTest() throws IOException { + Path testFile = Files.createTempFile("stocks", "csv"); + List stocks = List.of(new Stock("AAPL", "Apple Inc.", new BigDecimal("276.43"))); + StockFileHandler.writeToFile(testFile, stocks); + List lines = Files.readAllLines(testFile); + assertTrue(lines.get(2).startsWith("AAPL,Apple Inc.,276.43")); + } +} From a3356929ddfefab97c1f61e5a32c7db5606344da Mon Sep 17 00:00:00 2001 From: Per Eric Trapnes Date: Fri, 10 Apr 2026 12:15:12 +0200 Subject: [PATCH 2/2] Update PlayerTest.java --- .../idi/idatt2003/gruppe42/Model/PlayerTest.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java index 6ca36bb..6e6c405 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java @@ -73,14 +73,10 @@ void withdrawExactAmountTest() { @Test void getNetworthTest() { - BigDecimal originalMoney = player.getMoney(); - Portfolio portfolio = player.getPortfolio(); - - Share share = new Share(new Stock("AAPL", "Apple Inc.", new BigDecimal("276.43")), new BigDecimal("10"), new BigDecimal("50")); - portfolio.addShare(share); - - player.getNetWorth(); - - assertEquals(originalMoney, player.getMoney()); + // Starting with 10000 money and adding 5000 moneys worth of AAPL then calculating the networth + // with commissions taken account of will result in this: + // 10000 + 5000 - (5000 * 0.01) = 14950 + player.getPortfolio().addShare(new Share(new Stock("AAPL", "Apple Inc.", new BigDecimal("5000")), new BigDecimal("1"), new BigDecimal("5000"))); + assertEquals(new BigDecimal("14950.00"), player.getNetWorth()); } }