Skip to content

Commit

Permalink
Merge pull request #44 from IDATT2003-gruppe06/file-io-development
Browse files Browse the repository at this point in the history
File io development
  • Loading branch information
nolydvo authored May 15, 2026
2 parents d875da2 + 932bee6 commit 6ec8351
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
10 changes: 10 additions & 0 deletions src/main/java/millions/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.math.BigDecimal;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Stage;
import millions.controller.GameController;
import millions.controller.fileIO.InvalidFormatException;
import millions.view.GameView;
import millions.view.StartView;

Expand Down Expand Up @@ -33,6 +35,14 @@ public void start(Stage stage) {

Scene gameScene = new Scene(gameView, 1920, 1080);
stage.setScene(gameScene);
} catch (InvalidFormatException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error with selected file");
alert.setContentText("Please control the format of the selected file");

alert.showAndWait();

} catch (RuntimeException ex) {
System.err.println(ex);
System.exit(0);
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.stream.Collectors;
import millions.controller.fileIO.CSVStockFileParser;
import millions.controller.fileIO.InvalidFormatException;
import millions.controller.fileIO.StockFileReader;
import millions.model.Exchange;
import millions.model.Player;
Expand All @@ -21,18 +22,22 @@ public void startGame(
if (preRunWeeks < 0) {
throw new IllegalArgumentException("Pre run weeks cannot be negative");
}
try {
StockFileReader reader = new StockFileReader(stockFilePath);
List<String> lines = reader.readFile();
CSVStockFileParser parser = new CSVStockFileParser(lines);

StockFileReader reader = new StockFileReader(stockFilePath);
List<String> lines = reader.readFile();
CSVStockFileParser parser = new CSVStockFileParser(lines);
List<Stock> stocks = parser.parse();
List<Stock> stocks = parser.parse();

exchange = new Exchange("Exchange", stocks);
for (int i = 0; i < preRunWeeks; i++) {
exchange.advance();
}
exchange = new Exchange("Exchange", stocks);
for (int i = 0; i < preRunWeeks; i++) {
exchange.advance();
}

player = new Player(name, startingMoney);
player = new Player(name, startingMoney);
} catch(InvalidFormatException e) {
throw new InvalidFormatException(e.getMessage());
}
}

public Player getPlayer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public CSVStockFileParser(List<String> lines) {
if (verifyCSV(lines)) {
this.lines = lines;
} else {
// throw file format error
throw new InvalidFormatException("Incorrect format for CSV File");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package millions.controller.fileIO;

public class InvalidFormatException extends RuntimeException {
public InvalidFormatException(String message) {
super(message);
}
}
31 changes: 13 additions & 18 deletions src/test/java/millions/CSVStockFileParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,33 @@
import millions.controller.fileIO.StockFileReader;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;


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

public class CSVStockFileParserTest {
@TempDir
static Path tempDir;

static Path sharedFile;
static String exampleString;

@BeforeAll
public static void setUpTestFile() throws Exception {
sharedFile = Files.createFile(tempDir.resolve("file.csv"));
String string = "# Top 500 US Stocks by Market Cap\n";
string += "# Ticker,Name,Price\n";
string += "\n";
string += "NVDA,Nvidia,191.27\n";
string += "AAPL,Apple Inc.,276.43\n";
string += "MSFT,Microsoft,404.68\n";
Files.writeString(sharedFile, string);
exampleString = "# Top 500 US Stocks by Market Cap\n";
exampleString += "# Ticker,Name,Price\n";
exampleString += "\n";
exampleString += "NVDA,Nvidia,191.27\n";
exampleString += "AAPL,Apple Inc.,276.43\n";
exampleString += "MSFT,Microsoft,404.68\n";
}

@Test
public void parseStockFileTest(){
StockFileReader stockFileReader = new StockFileReader(sharedFile);
List<String> testList = List.of(exampleString.split("\n"));

CSVStockFileParser parser = new CSVStockFileParser(stockFileReader.readFile());
CSVStockFileParser parser = new CSVStockFileParser(testList);
assertEquals(3, parser.parse().size());
}
}

0 comments on commit 6ec8351

Please sign in to comment.