Skip to content

Commit

Permalink
Changelog
Browse files Browse the repository at this point in the history
renamed StockFileReader
Added toString method to stock class
Created test for CSVStockFileParser
Fixed issue where CSVStockFileParser would only accept incorrect formats
  • Loading branch information
Nikollai committed Apr 12, 2026
1 parent 764750a commit 3acdb6a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ public CSVStockFileParser(List<String> lines) {
}

// returns true if all entries have exactly 3 data points
private static boolean verifyCSV(List<String> lines) {
public boolean verifyCSV(List<String> lines) {
return lines.stream()
.filter(l -> !(l.startsWith("#") || l.isEmpty()))
.anyMatch(l -> l.split(",").length != 3);
.filter(l -> !(l.startsWith("#") || l.isBlank()))
.noneMatch(l -> l.split(",").length != 3);

}

public List<Stock> parse() {
List<Stock> stocks = new ArrayList<>();
lines.forEach(l -> {
lines.stream().filter(l -> !((l.startsWith("#") || l.isBlank()))).forEach(l -> {
String[] split = l.split(",");
String symbol = split[0];
String company = split[1];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package millions.controller.fileIO;

import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;


public class StockInformationReader {
private final File file;
public class StockFileReader {
private final Path filePath;

public StockInformationReader(File file) {
this.file = file;
public StockFileReader(Path path) {
this.filePath = path;
}

public List<String> readFile() {
File file = new File(filePath.toString());
List<String> lines = new ArrayList<>();
try (Reader reader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(reader)) {
lines = bufferedReader.readAllLines();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/millions/model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public BigDecimal getLatestPriceChange() {

return currentPrice.subtract(lastPrice);
}

@Override
public String toString() {
return "Stock [symbol: " + symbol + ", company: " + company + ", prices: " + prices + "]";
}
}
40 changes: 40 additions & 0 deletions src/test/java/millions/CSVStockFileParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package millions;

import millions.controller.fileIO.CSVStockFileParser;
import millions.controller.fileIO.StockFileReader;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class CSVStockFileParserTest {
@TempDir
static Path tempDir;

static Path sharedFile;

@BeforeAll
public static void setUpTestFile() throws Exception {
sharedFile = Files.createFile(tempDir.resolve("file.csv"));
String string = "# Top 500 US Stocks by Market Cap\n";
string += "# Ticker,Name,Price\n";
string += "\n";
string += "NVDA,Nvidia,191.27\n";
string += "AAPL,Apple Inc.,276.43\n";
string += "MSFT,Microsoft,404.68\n";
Files.writeString(sharedFile, string);
}

@Test
public void parseStockFileTest(){
StockFileReader stockFileReader = new StockFileReader(sharedFile);
stockFileReader.readFile().forEach(System.out::println);

CSVStockFileParser parser = new CSVStockFileParser(stockFileReader.readFile());
parser.parse().forEach(s -> System.out.println(s.toString()));
}
}

0 comments on commit 3acdb6a

Please sign in to comment.