Skip to content

Commit

Permalink
Feat: Fileparser, file converter, dummydata
Browse files Browse the repository at this point in the history
Added a file parser class that is responsible for parsing a .txt file that has stocks.

Added a file converter class that is responsible for converting a list of strings (from file parser) to a list of stock objects.

Added dummydata.txt which includes some stocks
  • Loading branch information
tommyah committed Mar 11, 2026
1 parent 9f6e12b commit 20c8938
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/FileConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.ntnu.idi.idatt2003.g40.mappe;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class FileConverter {

public List<Stock> getStocksFromFile(FileParser fileParser) throws IOException {
List<String> strings = fileParser.readFile();
List<Stock> stocksFromFile = new ArrayList<>();

strings.forEach(s -> {
String[] lineElements = s.split(",");
String stockSymbol = lineElements[0].trim();
String stockName = lineElements[1].trim();
BigDecimal stockPrice = new BigDecimal(lineElements[2].trim());
stocksFromFile.add(new Stock(stockSymbol, stockName, stockPrice));
});
return stocksFromFile;
}
}
68 changes: 68 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/FileParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package edu.ntnu.idi.idatt2003.g40.mappe;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.Predicate;

public class FileParser {

private final String pathName;

private enum ParserRuleSet {
NOT_EMPTY(s -> !s.trim().isEmpty()),
NOT_COMMENT(s -> !s.startsWith("#")),
VALID_FORMAT(NOT_EMPTY.rule.and(NOT_COMMENT.rule)),
VALID_CODE(s -> s.matches("[A-Z]{4}")),
VALID_NAME(s -> s.matches(".*")),
CAN_PARSE_TO_BIG_DECIMAL(s -> {
try {
new BigDecimal(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}),
VALID_PRICE_FORMAT(s -> s.matches("[^a-zA-Z]+")),
VALID_PRICE(VALID_PRICE_FORMAT.rule.and(CAN_PARSE_TO_BIG_DECIMAL.rule));

private final Predicate<String> rule;

ParserRuleSet(final Predicate<String> rule) {
this.rule = rule;
}
}

public FileParser(final String pathName) {
this.pathName = pathName;
}

public List<String> readFile() throws IOException {
try {
Path path = Paths.get(pathName);
List<String> allLines = Files.readAllLines(path);
List<String> readableLines = allLines.stream().filter(ParserRuleSet.VALID_FORMAT.rule).toList();

// Valid lines (following the correct regular expressions)
return readableLines.stream().filter(s -> {
String[] parts = s.trim().split(",");

if (parts.length != 3) {
return false;
}

boolean validCode = ParserRuleSet.VALID_CODE.rule.test(parts[0].trim());
boolean validName = ParserRuleSet.VALID_NAME.rule.test(parts[1].trim());
boolean validPrice = ParserRuleSet.VALID_PRICE.rule.test(parts[2].trim());

return validCode && validName && validPrice;
}).toList();

} catch (IOException e) {
throw new IOException("File parser could not parse file!");
}
}
}
11 changes: 11 additions & 0 deletions src/main/resources/dummydata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#THIS IS A COMMENT. Below me is an empty line

AAPL, Apple Inc., 276.43
NVID, Nvidida Corporation, 241.591

#Above me are some valid formats.
#Below me are some invalid formats

COOLI, This is a cool name, 252.2

COOL, This is a cool name, 252.2a

0 comments on commit 20c8938

Please sign in to comment.