-
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.
Merge pull request #19 from IDATT2003-Gruppe-53/13-add-file-handling-…
…for-stock-data merge 13 with main
- Loading branch information
Showing
2 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
millions/src/main/java/no/ntnu/gruppe53/FileHandler.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 |
|---|---|---|
| @@ -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<Stock> 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<Stock> readStocksFromFile(String path, String filename) { | ||
| List<Stock> 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; | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
millions/src/test/java/no/ntnu/gruppe53/FileHandlerTest.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 |
|---|---|---|
| @@ -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<Stock> stockList = new ArrayList<>(); | ||
| stockList.add(testStock1); | ||
| stockList.add(testStock2); | ||
|
|
||
| writeStocksToFile(stockList, filePathString, fileName); | ||
|
|
||
| List<String> 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<Stock> stockList = new ArrayList<>(); | ||
| stockList.add(testStock1); | ||
| stockList.add(testStock2); | ||
|
|
||
| writeStocksToFile(stockList, filePathString, fileName); | ||
|
|
||
| List <Stock> 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"); | ||
| } | ||
| } |