diff --git a/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java b/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java index 5a7d9b8..bcd783d 100644 --- a/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java +++ b/src/main/java/millions/controller/fileIO/CSV/CSVFileHandler.java @@ -7,6 +7,9 @@ import java.nio.file.Path; import java.util.List; +/** + *

Bundles StockFileReader and CSVStockFileParser together to reduce boilerplate code when reading stocks from a csv file

+ */ public class CSVFileHandler { StockFileReader reader; CSVStockFileParser parser; @@ -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 getStocksFromFile(Path filePath) { try { StockFileReader reader = new StockFileReader(); diff --git a/src/main/java/millions/controller/fileIO/CSV/CSVStockFileParser.java b/src/main/java/millions/controller/fileIO/CSV/CSVStockFileParser.java index eb8339f..9208bbc 100644 --- a/src/main/java/millions/controller/fileIO/CSV/CSVStockFileParser.java +++ b/src/main/java/millions/controller/fileIO/CSV/CSVStockFileParser.java @@ -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 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

lines to be parsed.
+ * Each line must contain three data fields: String,String,BigDecimal
+ * (Fields cannot be blank)
+ * blank lines or lines beginning with '#' are ignored + *

+ * @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 parse(List lines) { List stocks = new ArrayList<>(); if (verifyCSV(lines)) { diff --git a/src/main/java/millions/controller/fileIO/CSV/CSVStockFileWriter.java b/src/main/java/millions/controller/fileIO/CSV/CSVStockFileWriter.java index 0233a7c..3e7a791 100644 --- a/src/main/java/millions/controller/fileIO/CSV/CSVStockFileWriter.java +++ b/src/main/java/millions/controller/fileIO/CSV/CSVStockFileWriter.java @@ -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.
+ * Converts a list of stock objects into a writeable string with a CSV format. + * */ public class CSVStockFileWriter implements StockFileWriter { private final List stocks; private String finalString; + /** + * Constructor for CSVStockFileWriter + * @param stocks list of stocks to be formatted and written + */ public CSVStockFileWriter(List stocks) { this.stocks = stocks; } + /** + * Formats given string to CSV format to prepare for writing to file + */ @Override public void formatString() { StringBuilder builder = new StringBuilder(); @@ -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(); } diff --git a/src/main/java/millions/controller/fileIO/InvalidFormatException.java b/src/main/java/millions/controller/fileIO/InvalidFormatException.java index 6883256..a1e2136 100644 --- a/src/main/java/millions/controller/fileIO/InvalidFormatException.java +++ b/src/main/java/millions/controller/fileIO/InvalidFormatException.java @@ -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); diff --git a/src/main/java/millions/controller/fileIO/StockFileReader.java b/src/main/java/millions/controller/fileIO/StockFileReader.java index 33956b1..f873896 100644 --- a/src/main/java/millions/controller/fileIO/StockFileReader.java +++ b/src/main/java/millions/controller/fileIO/StockFileReader.java @@ -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 readFile(Path path) { File file = new File(path.toString()); List lines = new ArrayList<>();