Skip to content

Commit

Permalink
Created StockInformationCSVReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikollai committed Mar 11, 2026
1 parent 49c4952 commit 35bf19c
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/java/millions/StockInformationCSVReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package millions;

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


public class StockInformationCSVReader {
File file;

public StockInformationCSVReader(File file) {
this.file = file;
}

public List<Stock> readFile() {
List<Stock> stocks = new ArrayList<>();
try (Reader reader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(reader)) {
List<String> lines = bufferedReader.readAllLines();
for (String line : lines) {
// Skips comment and blank lines
if ( !(line.startsWith("#") || line.isEmpty()) ) {
String[] data = line.split(",");
// Ensures only fields of the correct length are read
if (data.length == 3) {
String symbol = data[0];
String company = data[1];
BigDecimal price = new BigDecimal(data[2]);
stocks.add(new Stock(symbol, company, price));
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return stocks;
}
}

0 comments on commit 35bf19c

Please sign in to comment.