From 08991e16ffd610d3726d2ebf15ea42de0cfcf8fc Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 25 May 2026 20:05:43 +0200 Subject: [PATCH] feat: sell and quit button --- src/main/java/millions/App.java | 6 +++++- src/main/java/millions/model/Player.java | 8 +++++++ src/main/java/millions/view/ExitView.java | 26 +++++++++++++++++++++++ src/main/java/millions/view/GameView.java | 22 +++++++++++++++++-- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/main/java/millions/view/ExitView.java diff --git a/src/main/java/millions/App.java b/src/main/java/millions/App.java index 9a83f2c..942953a 100644 --- a/src/main/java/millions/App.java +++ b/src/main/java/millions/App.java @@ -13,6 +13,7 @@ import millions.controller.fileIO.CSV.CSVStockFileWriter; import millions.controller.fileIO.InvalidFormatException; import millions.controller.fileIO.UncheckedFileNotFoundException; +import millions.view.ExitView; import millions.view.GameView; import millions.view.StartView; @@ -35,7 +36,10 @@ public void start(Stage stage) { startView.getSelectedFile().toPath(), startView.getPreRunWeeks()); - GameView gameView = new GameView(controller); + GameView gameView = + new GameView( + controller, + () -> stage.setScene(new Scene(new ExitView(controller.getPlayer()), 400, 250))); controller.getPlayer().addListener(gameView); controller.getExchange().addListener(gameView); diff --git a/src/main/java/millions/model/Player.java b/src/main/java/millions/model/Player.java index e4abeae..515926e 100644 --- a/src/main/java/millions/model/Player.java +++ b/src/main/java/millions/model/Player.java @@ -63,6 +63,7 @@ public void withdrawMoney(BigDecimal amount) { /** * Calculates the skill level of the player + * * @return String player status level */ public String getStatus() { @@ -101,6 +102,13 @@ public Portfolio getPortfolio() { return this.portfolio; } + /** + * @return player startingMoney + */ + public BigDecimal getStartingMoneh() { + return this.startingMoney; + } + /** * @param share Share to be added */ diff --git a/src/main/java/millions/view/ExitView.java b/src/main/java/millions/view/ExitView.java new file mode 100644 index 0000000..bd87dff --- /dev/null +++ b/src/main/java/millions/view/ExitView.java @@ -0,0 +1,26 @@ +package millions.view; + +import javafx.application.Platform; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.VBox; +import millions.model.Player; + +public class ExitView extends VBox { + + public ExitView(Player player) { + setSpacing(12); + + Label title = new Label("Game over"); + Label weeks = new Label("Weeks traded: " + player.getTransactionArchive().countDistinctWeeks()); + Label trades = new Label("Trades: " + player.getTransactionArchive().getTransactions().size()); + Label netWorth = new Label("Final net worth: " + player.getNetWorth()); + Label netProfit = + new Label("Total profit: " + player.getNetWorth().subtract(player.getStartingMoneh())); + + Button quitButton = new Button("Quit"); + quitButton.setOnAction(event -> Platform.exit()); + + getChildren().addAll(title, weeks, trades, netWorth, quitButton, netProfit); + } +} diff --git a/src/main/java/millions/view/GameView.java b/src/main/java/millions/view/GameView.java index ad63e4e..494a660 100644 --- a/src/main/java/millions/view/GameView.java +++ b/src/main/java/millions/view/GameView.java @@ -57,10 +57,13 @@ public class GameView extends BorderPane implements PlayerListener, ExchangeList private final Button buyButton = new Button("Buy"); private final Button sellButton = new Button("Sell"); private final Button advanceButton = new Button("Advance week"); + private final Button sellAllAndQuitButton = new Button("Sell all & quit"); + private final Runnable onSellAllAndQuit; private boolean updatingQuantityControls; - public GameView(GameController controller) { + public GameView(GameController controller, Runnable onSellAllAndQuit) { this.controller = controller; + this.onSellAllAndQuit = onSellAllAndQuit; setTop(createHeader()); setCenter(createTabs()); configureStocksList(); @@ -124,7 +127,8 @@ private Tab createStocksTab() { quantitySlider, buyButton, sellBox, - advanceButton); + advanceButton, + sellAllAndQuitButton); HBox content = new HBox(12, leftPane, rightPane); VBox outer = new VBox(12, content, actionBar, actionStatusLabel); return new Tab("Stocks", outer); @@ -212,6 +216,7 @@ private void configureButtons() { buyButton.setOnAction(event -> buySelectedStock()); sellButton.setOnAction(event -> sellSelectedShare()); advanceButton.setOnAction(event -> advanceWeek()); + sellAllAndQuitButton.setOnAction(event -> sellAllAndQuit()); } private void configureQuantityControls() { @@ -396,6 +401,19 @@ private void advanceWeek() { showStockChart(stocksList.getSelectionModel().getSelectedItem()); } + public void sellAllShares() { + List shares = new java.util.ArrayList<>(controller.getPlayer().getPortfolio().getShares()); + for (Share share : shares) { + controller.sellShare(share); + } + refreshAll(); + } + + private void sellAllAndQuit() { + sellAllShares(); + onSellAllAndQuit.run(); + } + private void setActionStatus(String message, boolean success) { actionStatusLabel.setText(message); actionStatusLabel.setStyle(success ? "-fx-text-fill: green;" : "-fx-text-fill: red;");