From 0515cb2a0c77fd04d4a2e0093e90428808fc916c Mon Sep 17 00:00:00 2001 From: Roar Date: Thu, 26 Mar 2026 18:22:55 +0100 Subject: [PATCH 1/5] Added FileHandler Added a class for handling read and writing of stocks from and to .csv. Currently has method to read a csv file and output to console. --- .../java/no/ntnu/gruppe53/FileHandler.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 millions/src/main/java/no/ntnu/gruppe53/FileHandler.java diff --git a/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java new file mode 100644 index 0000000..96ddbd7 --- /dev/null +++ b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java @@ -0,0 +1,52 @@ +package no.ntnu.gruppe53; + +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +public class FileHandler { + public static void readStockFromFile(String path, String filename) { + List stocks = new ArrayList<>(); + String pathString = path + filename; + Path filePath = Paths.get(pathString); + + try (BufferedReader reader = Files.newBufferedReader(filePath, Charset.defaultCharset())) { + String line; + while ((line = reader.readLine()) != null) { + int counter = 0; + String trimValue = line.trim(); + + if (trimValue.isBlank() || (line.charAt(0) == '#')) { + continue; + } + + String[] values = line.split(","); + + for (String value : values) { + if (values.length != 3) { + System.out.println("ERROR: Bad line in csv: "); + System.out.print(line); + System.out.println("There should be 3 values total: " + + "symbol, name, price."); + } + + trimValue = value.trim(); + + System.out.print(counter); + System.out.print(trimValue + "|"); + counter++; + } + System.out.println(); + } + } catch (IOException e) { + System.out.println("ERROR: Something went wrong while reading the csv file."); + System.out.println("Message: " + e.getMessage()); + } + } + +} From 7376bd4f6050fee5a69351b878cbd1483a930ef9 Mon Sep 17 00:00:00 2001 From: Roar Date: Thu, 26 Mar 2026 18:58:55 +0100 Subject: [PATCH 2/5] Updated FileHandler Changed the name of the read method to readStocksFromFile. Method now reads from the csv correctly and does checks for bad lines, and outputs the correct lines as Stock objects to a list, that it returns. --- .../java/no/ntnu/gruppe53/FileHandler.java | 56 +++++++++++++++---- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java index 96ddbd7..02a52ee 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java +++ b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java @@ -2,6 +2,7 @@ import java.io.BufferedReader; import java.io.IOException; +import java.math.BigDecimal; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; @@ -9,15 +10,24 @@ import java.util.ArrayList; import java.util.List; + public class FileHandler { - public static void readStockFromFile(String path, String filename) { + + public static List readStocksFromFile(String path, String filename) { List stocks = new ArrayList<>(); String pathString = path + filename; Path filePath = Paths.get(pathString); try (BufferedReader reader = Files.newBufferedReader(filePath, Charset.defaultCharset())) { String line; + String symbol; + String name; + BigDecimal price; + while ((line = reader.readLine()) != null) { + symbol = null; + name = null; + price = null; int counter = 0; String trimValue = line.trim(); @@ -27,26 +37,50 @@ public static void readStockFromFile(String path, String filename) { String[] values = line.split(","); - for (String value : values) { - if (values.length != 3) { - System.out.println("ERROR: Bad line in csv: "); - System.out.print(line); - System.out.println("There should be 3 values total: " + - "symbol, name, price."); - } + if (values.length != 3) { + System.out.println("ERROR: Bad line in csv: "); + System.out.print(line); + System.out.println("There should be 3 values total: " + + "symbol, name, price."); + continue; + } + for (String value : values) { trimValue = value.trim(); - System.out.print(counter); - System.out.print(trimValue + "|"); + switch (counter) { + case 0: + symbol = trimValue; + break; + case 1: + name = trimValue; + break; + case 2: + try { + price = new BigDecimal(trimValue); + } + catch (NumberFormatException e) { + System.out.println("ERROR: Not a number. Last column in .csv file " + + "needs to be the price of the stock (number). Skipping..."); + price = null; + break; + } + } counter++; } - System.out.println(); + if (symbol == null || symbol.isBlank() || name == null || name.isBlank() || + price == null || price.compareTo(BigDecimal.ZERO) <= 0) { + continue; + } + stocks.add(new Stock(symbol, name, price)); } + } catch (IOException e) { System.out.println("ERROR: Something went wrong while reading the csv file."); System.out.println("Message: " + e.getMessage()); } + + return stocks; } } From 692a7a741ce38a4448159489a950f1360af8b94c Mon Sep 17 00:00:00 2001 From: Roar Date: Thu, 26 Mar 2026 20:00:24 +0100 Subject: [PATCH 3/5] Updated FileHandler Added method writeStocksToFile that converts a list of stocks to a .csv file on a format that readStocksFromFile() accepts. --- .../java/no/ntnu/gruppe53/FileHandler.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java index 02a52ee..dedff0a 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java +++ b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java @@ -1,6 +1,7 @@ package no.ntnu.gruppe53; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.IOException; import java.math.BigDecimal; import java.nio.charset.Charset; @@ -13,6 +14,32 @@ public class FileHandler { + public static void writeStocksToFile(List stockList, String path, String filename) { + String pathString = path + filename; + Path filePath = Paths.get(pathString); + + try (BufferedWriter writer = Files.newBufferedWriter(filePath, Charset.defaultCharset())) { + if (!Files.exists(filePath)) { + Files.createDirectories(filePath); + } + + writer.write("#Ticker,Name,Price"); + writer.newLine(); + writer.newLine(); + + for (Stock stock : stockList) { + String line = String.join(",", stock.getSymbol(), stock.getCompany(), + stock.getSalesPrice().toString()); + writer.write(line); + writer.newLine(); + } + + } catch (IOException e) { + System.out.println("ERROR: Something went wrong while writing the csv file."); + System.out.println("Message: " + e.getMessage()); + } + } + public static List readStocksFromFile(String path, String filename) { List stocks = new ArrayList<>(); String pathString = path + filename; From 093822982fea37342d2e62905704bce686945cbd Mon Sep 17 00:00:00 2001 From: Roar Date: Thu, 26 Mar 2026 20:09:52 +0100 Subject: [PATCH 4/5] Added FileHandlerTest Added positive tests for the methods in FileHandler. --- .../no/ntnu/gruppe53/FileHandlerTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 millions/src/test/java/no/ntnu/gruppe53/FileHandlerTest.java diff --git a/millions/src/test/java/no/ntnu/gruppe53/FileHandlerTest.java b/millions/src/test/java/no/ntnu/gruppe53/FileHandlerTest.java new file mode 100644 index 0000000..76c4914 --- /dev/null +++ b/millions/src/test/java/no/ntnu/gruppe53/FileHandlerTest.java @@ -0,0 +1,90 @@ +package no.ntnu.gruppe53; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.math.BigDecimal; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +import static no.ntnu.gruppe53.FileHandler.*; +import static org.junit.jupiter.api.Assertions.*; + +class FileHandlerTest { + @Test + void writeTest() throws IOException { + String filePathString = System.getProperty("user.dir") + "\\target\\test-output\\"; + String fileName = "stockTest.csv"; + + + String symbol1 = "AAPL"; + String company1 = "Apple Inc."; + BigDecimal price1 = new BigDecimal("100"); + + String symbol2 = "NVDA"; + String company2 = "Nvidia"; + BigDecimal price2 = new BigDecimal("191.27"); + + Stock testStock1 = new Stock(symbol1,company1, price1); + Stock testStock2 = new Stock(symbol2, company2, price2); + + List stockList = new ArrayList<>(); + stockList.add(testStock1); + stockList.add(testStock2); + + writeStocksToFile(stockList, filePathString, fileName); + + List lines = Files.readAllLines(Paths.get(filePathString + fileName)); + + assertTrue(Files.exists(Paths.get(filePathString + fileName)), + "File should have been created successfully."); + assertEquals("#Ticker,Name,Price",lines.getFirst(), "First line " + + "should contain correct headers."); + assertEquals("", lines.get(1), "Second line should be empty."); + assertEquals(symbol1 + "," + company1 + "," + price1, lines.get(2), + "Third line should contain correct values for first stock."); + assertEquals(symbol2 + "," + company2 + "," + price2, lines.getLast(), + "Last line should contain correct values for second stock."); + } + + @Test + void readTest() { + String filePathString = System.getProperty("user.dir") + "\\target\\test-output\\"; + String fileName = "stockTest.csv"; + + + String symbol1 = "AAPL"; + String company1 = "Apple Inc."; + BigDecimal price1 = new BigDecimal("100"); + + String symbol2 = "NVDA"; + String company2 = "Nvidia"; + BigDecimal price2 = new BigDecimal("191.27"); + + Stock testStock1 = new Stock(symbol1,company1, price1); + Stock testStock2 = new Stock(symbol2, company2, price2); + + List stockList = new ArrayList<>(); + stockList.add(testStock1); + stockList.add(testStock2); + + writeStocksToFile(stockList, filePathString, fileName); + + List readStocks = readStocksFromFile(filePathString, fileName); + + assertEquals(symbol1, readStocks.getFirst().getSymbol(), + "First stock's symbol should be AAPL."); + assertEquals(company1, readStocks.getFirst().getCompany(), + "First stock's company name should be Apple Inc."); + assertEquals(0, price1.compareTo(readStocks.getFirst().getSalesPrice()), + "First stock's price should be 100."); + assertEquals(symbol2, readStocks.getLast().getSymbol(), + "Second stock's symbol should be NVDA."); + assertEquals(company2, readStocks.getLast().getCompany(), + "Second stock's company name should be Nvidia."); + assertEquals(0, price2.compareTo(readStocks.getLast().getSalesPrice()), + "Second stock's price should be 191.27"); + } +} \ No newline at end of file From 07f133ab64e7dbbd9fe98f033120c9c5e68a9177 Mon Sep 17 00:00:00 2001 From: Roar Date: Fri, 27 Mar 2026 12:15:03 +0100 Subject: [PATCH 5/5] Updated FileHandler Added JavaDoc. --- .../java/no/ntnu/gruppe53/FileHandler.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java index dedff0a..8133e72 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java +++ b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java @@ -11,9 +11,19 @@ import java.util.ArrayList; import java.util.List; +/** + * Handles file operations for the application. + */ public class FileHandler { + /** + * Writes a list of {@link Stock}s to a text-file. Class is structed for {@code .csv} files. + * + * @param stockList the list of stocks to be converted to text + * @param path output path for the file + * @param filename name of the file (not including extension) + */ public static void writeStocksToFile(List stockList, String path, String filename) { String pathString = path + filename; Path filePath = Paths.get(pathString); @@ -40,6 +50,13 @@ public static void writeStocksToFile(List stockList, String path, String } } + /** + * Reads a .csv file and converts it to a list of {@code Stock} objects. + * + * @param path path to the file to be read + * @param filename name of the file to be read (including extension) + * @return a list of {@code Stock} objects converted from text + */ public static List readStocksFromFile(String path, String filename) { List stocks = new ArrayList<>(); String pathString = path + filename; @@ -101,13 +118,11 @@ public static List readStocksFromFile(String path, String filename) { } stocks.add(new Stock(symbol, name, price)); } - - } catch (IOException e) { + } + catch (IOException e) { System.out.println("ERROR: Something went wrong while reading the csv file."); System.out.println("Message: " + e.getMessage()); } - return stocks; } - }