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..8133e72 --- /dev/null +++ b/millions/src/main/java/no/ntnu/gruppe53/FileHandler.java @@ -0,0 +1,128 @@ +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; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +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); + + 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()); + } + } + + /** + * 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; + 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(); + + if (trimValue.isBlank() || (line.charAt(0) == '#')) { + continue; + } + + String[] values = line.split(","); + + 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(); + + 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++; + } + 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; + } +} 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