Skip to content

Commit

Permalink
feat: sell and quit button
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed May 25, 2026
1 parent 69f7abe commit 08991e1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/main/java/millions/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/millions/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void withdrawMoney(BigDecimal amount) {

/**
* Calculates the skill level of the player
*
* @return String player status level
*/
public String getStatus() {
Expand Down Expand Up @@ -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
*/
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/millions/view/ExitView.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
22 changes: 20 additions & 2 deletions src/main/java/millions/view/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -396,6 +401,19 @@ private void advanceWeek() {
showStockChart(stocksList.getSelectionModel().getSelectedItem());
}

public void sellAllShares() {
List<Share> 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;");
Expand Down

0 comments on commit 08991e1

Please sign in to comment.