-
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.
Moved parsing of stock information to csvStockFileParser and added fo…
…rmat verification to allow for other file formats in the future
- Loading branch information
Nikollai
committed
Apr 12, 2026
1 parent
e271319
commit 84c1a03
Showing
2 changed files
with
44 additions
and
17 deletions.
There are no files selected for viewing
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,40 @@ | ||
| package millions.controller; | ||
|
|
||
| import millions.model.Stock; | ||
|
|
||
| import javax.swing.*; | ||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class CSVStockFileParser { | ||
| private List<String> lines; | ||
|
|
||
| public CSVStockFileParser(List<String> lines) { | ||
| if (verifyCSV(lines)) { | ||
| this.lines = lines; | ||
| } | ||
| else { | ||
| // throw file format error | ||
| } | ||
| } | ||
|
|
||
| // returns true if all entries have exactly 3 data points | ||
| private static boolean verifyCSV(List<String> lines) { | ||
| return lines.stream() | ||
| .filter(l -> !(l.startsWith("#") || l.isEmpty())) | ||
| .anyMatch(l -> l.split(",").length != 3); | ||
| } | ||
|
|
||
| public List<Stock> parse() { | ||
| List<Stock> stocks = new ArrayList<>(); | ||
| lines.forEach(l -> { | ||
| String[] split = l.split(","); | ||
| String symbol = split[0]; | ||
| String company = split[1]; | ||
| BigDecimal price = new BigDecimal(split[2]); | ||
| stocks.add(new Stock(symbol, company, price)); | ||
| }); | ||
| return stocks; | ||
| } | ||
| } |
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