Skip to content

Commit

Permalink
Added JavaDoc to fileIO files
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikollai committed May 18, 2026
1 parent 81a8164 commit 91ce42c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.nio.file.Path;
import java.util.List;

/**
* <p>Bundles StockFileReader and CSVStockFileParser together to reduce boilerplate code when reading stocks from a csv file</p>
*/
public class CSVFileHandler {
StockFileReader reader;
CSVStockFileParser parser;
Expand All @@ -16,6 +19,12 @@ public CSVFileHandler() {
this.parser = new CSVStockFileParser();
}

/**
* Reads and parses stocks from a csv file
* @param filePath Path to stock file
* @return list of stock object created from parsed file
* @throws InvalidFormatException Throws an InvalidFormatException received from parser
*/
public List<Stock> getStocksFromFile(Path filePath) {
try {
StockFileReader reader = new StockFileReader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,31 @@ public class CSVStockFileParser {

public CSVStockFileParser() {}

// returns true if all entries have exactly 3 data points
/**
* Verifies the amount of data fields present in supplied CSV file.
*
* @param lines Lines from csv file
* @return Boolean: True if file satisfies expected format (3 fields)
*/
public boolean verifyCSV(List<String> lines) {
return lines.stream()
.filter(l -> !(l.startsWith("#") || l.isBlank()))
.noneMatch(l -> l.split(",").length != 3);
}

/**
* Parses the supplied lines if they satisfy the correct format expectations
*
* @param lines <p>lines to be parsed. <br>
* Each line must contain three data fields: String,String,BigDecimal <br>
* (Fields cannot be blank) <br>
* blank lines or lines beginning with '#' are ignored
* </p>
* @return List of stock objects created from the supplied lines
* @throws InvalidFormatException If one or more lines contain too many or too few data fields
* @throws InvalidFormatException If the BigDecimal field on one or more lines are not compatible
* @throws InvalidFormatException Upon recieving an IllegalArgumentException (Either Symbol or Company name is blank)
*/
public List<Stock> parse(List<String> lines) {
List<Stock> stocks = new ArrayList<>();
if (verifyCSV(lines)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package millions.controller.fileIO.CSV;

import millions.controller.fileIO.StockFileWriter;
import millions.model.Stock;

import java.io.BufferedWriter;
import java.io.*;
import java.io.BufferedWriter;
import java.nio.file.Path;
import java.util.List;
import millions.controller.fileIO.StockFileWriter;
import millions.model.Stock;

//TODO: Validation of data before writing
/**
* Writes stock data to a CSV file.
* Implements StockFileWriter. <br>
* Converts a list of stock objects into a writeable string with a CSV format.
*
*/
public class CSVStockFileWriter implements StockFileWriter {
private final List<Stock> stocks;
private String finalString;

/**
* Constructor for CSVStockFileWriter
* @param stocks list of stocks to be formatted and written
*/
public CSVStockFileWriter(List<Stock> stocks) {
this.stocks = stocks;
}

/**
* Formats given string to CSV format to prepare for writing to file
*/
@Override
public void formatString() {
StringBuilder builder = new StringBuilder();
Expand All @@ -34,11 +41,18 @@ public void formatString() {
this.finalString = builder.toString();
}

/**
* Writes the saved string to a file
* @param path Path to desired file
* @return Boolean for success
*/
// TODO: Disable writing before formatting
@Override
public boolean write(Path path){
try (FileWriter fw = new FileWriter(path.toString()); BufferedWriter writer = new BufferedWriter(fw);) {
this.formatString();
writer.write(finalString);
// TODO: exception handling
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package millions.controller.fileIO;

/**
* Exception to be thrown when verifying the format of files
*/
public class InvalidFormatException extends RuntimeException {
public InvalidFormatException(String message) {
super(message);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/millions/controller/fileIO/StockFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class StockFileReader {

public StockFileReader() {}

/**
* Reads the file found at the specified path
* @param path Path to the desired file
* @return List of each line in the file as a string
*/
public List<String> readFile(Path path) {
File file = new File(path.toString());
List<String> lines = new ArrayList<>();
Expand Down

0 comments on commit 91ce42c

Please sign in to comment.