-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Fileparser, file converter, dummydata
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
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/FileConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
68
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/FileParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | ||
| } | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |