Skip to content

Commit

Permalink
Starting javaFX code
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed Apr 22, 2026
1 parent b5adc64 commit 2c0a768
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>temppackage.Main</mainClass>
<mainClass>millions.App</mainClass>
</configuration>
</plugin>
<plugin>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/millions/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package millions;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import millions.controller.GameController;
import millions.view.StartView;

public class App extends Application {

@Override
public void start(Stage stage) {
GameController controller = new GameController();
StartView startView = new StartView(stage);

Scene scene = new Scene(startView, 400, 350);
stage.setTitle("Millions");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}
33 changes: 33 additions & 0 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package millions.controller;

import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.List;
import millions.controller.fileIO.CSVStockFileParser;
import millions.controller.fileIO.StockFileReader;
import millions.model.Exchange;
import millions.model.Player;
import millions.model.Stock;

public class GameController {
private Player player;
private Exchange exchange;

public void startGame(String name, BigDecimal startingMoney, Path stockFilePath) {
StockFileReader reader = new StockFileReader(stockFilePath);
List<String> lines = reader.readFile();
CSVStockFileParser parser = new CSVStockFileParser(lines);
List<Stock> stocks = parser.parse();

player = new Player(name, startingMoney);
exchange = new Exchange("Exchange", stocks);
}

public Player getPlayer() {
return player;
}

public Exchange getExchange() {
return exchange;
}
}
94 changes: 94 additions & 0 deletions src/main/java/millions/view/StartView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package millions.view;

import java.io.File;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class StartView extends VBox {

private TextField nameField;
private TextField startingAmountField;
private File selectedFile;
private Button filepickerButton;
private Button startButton;

public StartView(Stage stage) {
setAlignment(Pos.CENTER);
setSpacing(12);
setPadding(new Insets(40));

nameField = new TextField();
nameField.setPromptText("Player name:");
nameField.setMaxWidth(250);
nameField.textProperty().addListener((obs, oldVal, newVal) -> checkStartButtonValid());

startingAmountField = new TextField();
startingAmountField.setPromptText("Starting amount:");
startingAmountField.setMaxWidth(250);
startingAmountField
.textProperty()
.addListener((obs, oldVal, newVal) -> checkStartButtonValid());

filepickerButton = new Button();
filepickerButton.setText("Pick file");
filepickerButton.setMaxWidth(250);
filepickerButton.setOnAction(
e -> {
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("CSV files", "*.csv"));
chooser.setTitle("Select stock CSV file");
File file = chooser.showOpenDialog(stage);

if (file != null) {
selectedFile = file;
filepickerButton.setText(file.getName());
}
});

startButton = new Button("Start game");
startButton.setDisable(true);

Label title = new Label("Millions");
title.setStyle("-fx-font-size: 32px; -fx-font-weight: bold;");

getChildren().addAll(title, nameField, startingAmountField, filepickerButton, startButton);
}

private void checkStartButtonValid() {
boolean valid = true;

if (nameField.getText().isBlank()) {
valid = false;
}

try {
Integer.valueOf(startingAmountField.getText());
} catch (NumberFormatException e) {
valid = false;
}

startButton.setDisable(!valid);
}

public String getName() {
return nameField.getText();
}

public String getStartingAmount() {
return startingAmountField.getText();
}

public File getSelectedFile() {
return selectedFile;
}

public Button getStartButton() {
return startButton;
}
}

0 comments on commit 2c0a768

Please sign in to comment.