Skip to content

Commit

Permalink
Fixed CSVStockFileWriter
Browse files Browse the repository at this point in the history
Created CSVStockFileWriterTest
Made some small changes to StockFileWriter and StockFileReaderTest
  • Loading branch information
Nikollai committed Apr 13, 2026
1 parent 49ac7b1 commit 9f127ac
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public boolean verifyCSV(List<String> lines) {

public List<Stock> parse() {
List<Stock> stocks = new ArrayList<>();
lines.stream().filter(l -> !((l.startsWith("#") || l.isBlank()))).forEach(l -> {
lines.stream()
.filter(l -> !((l.startsWith("#") || l.isBlank())))
.forEach(l -> {
String[] split = l.split(",");
String symbol = split[0];
String company = split[1];
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/millions/controller/fileIO/CSVStockFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import millions.model.Stock;

import java.io.BufferedWriter;
import java.io.*;
import java.nio.file.Path;
import java.util.List;

//TODO: Validation of data before writing
public class CSVStockFileWriter implements StockFileWriter {
List<Stock> stocks;
String finalString;
private final List<Stock> stocks;
private String finalString;

public CSVStockFileWriter(List<Stock> stocks) {
this.stocks = stocks;
Expand All @@ -20,14 +24,20 @@ public void formatString() {
builder.append(",");
builder.append(stock.getCompany());
builder.append(",");
builder.append(stock.getLatestPriceChange().toString());
builder.append(stock.getSalesPrice().toString());
builder.append("\n");
});
this.finalString = builder.toString();
}

@Override
public boolean write(){
// TODO: handle file creation/file selection when writing to file
public boolean write(Path path){
try (FileWriter fw = new FileWriter(path.toString()); BufferedWriter writer = new BufferedWriter(fw);) {
this.formatString();
writer.write(finalString);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package millions.controller.fileIO;

import java.nio.file.Path;

public interface StockFileWriter {
public void formatString();
public boolean write();
public boolean write(Path path);
}
43 changes: 43 additions & 0 deletions src/test/java/millions/CSVStockFileWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package millions;

import millions.controller.fileIO.CSVStockFileWriter;
import millions.controller.fileIO.StockFileReader;
import millions.model.Stock;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CSVStockFileWriterTest {
@TempDir
static Path tempDir;

List<Stock> stocks;

@BeforeEach
void setup() {
Stock s1 = new Stock("PEAR", "Pear Inc.", BigDecimal.valueOf(300));
Stock s2 = new Stock("DOGL", "DOOGLE Inc.", BigDecimal.valueOf(200.00));
Stock s3 = new Stock("MSFT", "EpsteinSoft Inc.", BigDecimal.valueOf(0.02));

this.stocks = List.of(s1, s2, s3);
}

@Test
public void testWrite() {
for(Stock stock : this.stocks) {
System.out.println(stock.toString());
}
CSVStockFileWriter csvStockFileWriter = new CSVStockFileWriter(stocks);
csvStockFileWriter.write(tempDir.resolve("stocks.csv"));

StockFileReader stockFileReader = new StockFileReader(tempDir.resolve("stocks.csv"));
assertEquals(3, stockFileReader.readFile().size());
}
}
2 changes: 1 addition & 1 deletion src/test/java/millions/StockFileReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void setUpTestFile() throws Exception {
}

@Test
public void testReadStockFile() throws Exception {
public void testReadStockFile() {
StockFileReader stockFileReader = new StockFileReader(sharedFile);
assertEquals(6, stockFileReader.readFile().size());
}
Expand Down

0 comments on commit 9f127ac

Please sign in to comment.