Skip to content

Commit

Permalink
Created CSVFileHandler to reduce overhead code when reading stocks fr…
Browse files Browse the repository at this point in the history
…om a CSV file

Also changed where file information is passed to filereader and csvparser respectively to accommodate this change
  • Loading branch information
Nikollai committed May 18, 2026
1 parent 932bee6 commit 2f63d03
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 25 deletions.
9 changes: 4 additions & 5 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import millions.controller.fileIO.CSVFileHandler;
import millions.controller.fileIO.CSVStockFileParser;
import millions.controller.fileIO.InvalidFormatException;
import millions.controller.fileIO.StockFileReader;
Expand All @@ -23,11 +25,8 @@ public void startGame(
throw new IllegalArgumentException("Pre run weeks cannot be negative");
}
try {
StockFileReader reader = new StockFileReader(stockFilePath);
List<String> lines = reader.readFile();
CSVStockFileParser parser = new CSVStockFileParser(lines);

List<Stock> stocks = parser.parse();
CSVFileHandler fileHandler = new CSVFileHandler();
List<Stock> stocks = fileHandler.getStocksFromFile(stockFilePath);

exchange = new Exchange("Exchange", stocks);
for (int i = 0; i < preRunWeeks; i++) {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/millions/controller/fileIO/CSVFileHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package millions.controller.fileIO;

import millions.controller.fileIO.StockFileReader;
import millions.controller.fileIO.CSVStockFileParser;
import millions.model.Stock;

import java.nio.file.Path;
import java.util.List;

public class CSVFileHandler {
StockFileReader reader;
CSVStockFileParser parser;

public CSVFileHandler() {
this.reader = new StockFileReader();
this.parser = new CSVStockFileParser();
}

public List<Stock> getStocksFromFile(Path filePath) {
try {
StockFileReader reader = new StockFileReader();
List<String> lines = reader.readFile(filePath);

CSVStockFileParser parser = new CSVStockFileParser();
return parser.parse(lines);

} catch (InvalidFormatException e) {
throw new InvalidFormatException(e.getMessage());
}
}
}
15 changes: 7 additions & 8 deletions src/main/java/millions/controller/fileIO/CSVStockFileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
public class CSVStockFileParser {
private List<String> lines;

public CSVStockFileParser(List<String> lines) {
if (verifyCSV(lines)) {
this.lines = lines;
} else {
throw new InvalidFormatException("Incorrect format for CSV File");
}
}
public CSVStockFileParser() {}

// returns true if all entries have exactly 3 data points
public boolean verifyCSV(List<String> lines) {
Expand All @@ -24,7 +18,12 @@ public boolean verifyCSV(List<String> lines) {
.noneMatch(l -> l.split(",").length != 3);
}

public List<Stock> parse() {
public List<Stock> parse(List<String> lines) {
if (verifyCSV(lines)) {
this.lines = lines;
} else {
throw new InvalidFormatException("Incorrect format for CSV File");
}
List<Stock> stocks = new ArrayList<>();
lines.stream()
.filter(l -> !((l.startsWith("#") || l.isBlank())))
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/millions/controller/fileIO/StockFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
* Reads a file and returns its lines as a list of strings.
*/
public class StockFileReader {
private final Path filePath;

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

public List<String> readFile() {
File file = new File(filePath.toString());
public List<String> readFile(Path path) {
File file = new File(path.toString());
List<String> lines = new ArrayList<>();
try (Reader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader)) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/millions/CSVStockFileParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void setUpTestFile() throws Exception {
public void parseStockFileTest(){
List<String> testList = List.of(exampleString.split("\n"));

CSVStockFileParser parser = new CSVStockFileParser(testList);
assertEquals(3, parser.parse().size());
CSVStockFileParser parser = new CSVStockFileParser();
assertEquals(3, parser.parse(testList).size());
}
}
4 changes: 2 additions & 2 deletions src/test/java/millions/CSVStockFileWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testWrite() {
CSVStockFileWriter csvStockFileWriter = new CSVStockFileWriter(stocks);
csvStockFileWriter.write(tempDir.resolve("stocks.csv"));

StockFileReader stockFileReader = new StockFileReader(tempDir.resolve("stocks.csv"));
assertEquals(3, stockFileReader.readFile().size());
StockFileReader stockFileReader = new StockFileReader();
assertEquals(3, stockFileReader.readFile(tempDir.resolve("stocks.csv")).size());
}
}
4 changes: 2 additions & 2 deletions src/test/java/millions/StockFileReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void setUpTestFile() throws Exception {

@Test
public void testReadStockFile() {
StockFileReader stockFileReader = new StockFileReader(sharedFile);
assertEquals(6, stockFileReader.readFile().size());
StockFileReader stockFileReader = new StockFileReader();
assertEquals(6, stockFileReader.readFile(sharedFile).size());
}
}

0 comments on commit 2f63d03

Please sign in to comment.