From c821d693e8c20058a4ee70c9c4a4eb4ac8b631c4 Mon Sep 17 00:00:00 2001 From: pawelsa Date: Thu, 14 May 2026 21:54:26 +0200 Subject: [PATCH] refactor: Correct model-view hooks. --- .../ntnu/idi/idatt/model/player/Player.java | 10 ------ .../ntnu/idi/idatt/session/UserSession.java | 21 +++++++++---- .../idatt/view/components/ui/UIFactory.java | 1 + .../view/primary/stock/StockController.java | 31 +++++++++++++------ 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt/model/player/Player.java b/src/main/java/edu/ntnu/idi/idatt/model/player/Player.java index 7af1065..f75b184 100644 --- a/src/main/java/edu/ntnu/idi/idatt/model/player/Player.java +++ b/src/main/java/edu/ntnu/idi/idatt/model/player/Player.java @@ -2,7 +2,6 @@ import edu.ntnu.idi.idatt.model.portfolio.Portfolio; import edu.ntnu.idi.idatt.model.transaction.TransactionArchive; -import edu.ntnu.idi.idatt.session.UserSession; import java.math.BigDecimal; @@ -50,19 +49,10 @@ public TransactionArchive getTransactionArchive() { public void addMoney(BigDecimal amount) { this.money = this.money.add(amount); - UserSession.getInstance().moneyProperty().set(this.money.doubleValue()); // Workaround instead of PropertyChange due - // to JSON saving - UserSession.getInstance().netWorthProperty().set(this.getNetWorth().doubleValue()); // Portfolio doesnt change - // without monetary change - // (either buy or sale) so thats - // the decision to hook net - // worth here and under. } public void withdrawMoney(BigDecimal amount) { this.money = this.money.subtract(amount); - UserSession.getInstance().moneyProperty().set(this.money.doubleValue()); // Hooks - UserSession.getInstance().netWorthProperty().set(this.getNetWorth().doubleValue()); } /** diff --git a/src/main/java/edu/ntnu/idi/idatt/session/UserSession.java b/src/main/java/edu/ntnu/idi/idatt/session/UserSession.java index 3dcf5ab..2fef3e5 100644 --- a/src/main/java/edu/ntnu/idi/idatt/session/UserSession.java +++ b/src/main/java/edu/ntnu/idi/idatt/session/UserSession.java @@ -1,8 +1,11 @@ package edu.ntnu.idi.idatt.session; +import java.math.BigDecimal; + import edu.ntnu.idi.idatt.model.Exchange; import edu.ntnu.idi.idatt.model.player.Player; -import javafx.beans.property.SimpleDoubleProperty; +import edu.ntnu.idi.idatt.storage.SessionManager; +import javafx.beans.property.SimpleObjectProperty; public class UserSession { @@ -29,7 +32,7 @@ public Player getPlayer() { public void setPlayer(Player player) { this.player = player; - moneyProperty.set(player.getMoney().doubleValue()); // Startup hook + updateGameState(); // Startup hook } public Exchange getExchange() { @@ -40,17 +43,23 @@ public void setExchange(Exchange exchange) { this.exchange = exchange; } - private final SimpleDoubleProperty moneyProperty = new SimpleDoubleProperty(); - private final SimpleDoubleProperty netWorthProperty = new SimpleDoubleProperty(); + private final SimpleObjectProperty moneyProperty = new SimpleObjectProperty<>(BigDecimal.ZERO); + private final SimpleObjectProperty netWorthProperty = new SimpleObjectProperty<>(BigDecimal.ZERO); - public SimpleDoubleProperty moneyProperty() { + public SimpleObjectProperty moneyProperty() { return moneyProperty; } - public SimpleDoubleProperty netWorthProperty() { + public SimpleObjectProperty netWorthProperty() { return netWorthProperty; } + public void updateGameState() { + moneyProperty.set(player.getMoney()); + netWorthProperty.set(player.getNetWorth()); + SessionManager.saveSession(); + } + public SessionBundle getSession() { return new SessionBundle(player, exchange); } diff --git a/src/main/java/edu/ntnu/idi/idatt/view/components/ui/UIFactory.java b/src/main/java/edu/ntnu/idi/idatt/view/components/ui/UIFactory.java index ed6aeab..2be519b 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/components/ui/UIFactory.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/components/ui/UIFactory.java @@ -144,6 +144,7 @@ public static Parent createMenu(String title, List buttonLables, ActionE Button progress = new Button("Advance to next week"); progress.setOnAction((e) -> { UserSession.getInstance().getExchange().advance(); + UserSession.getInstance().updateGameState(); SceneFactory.reloadCurrent(); }); diff --git a/src/main/java/edu/ntnu/idi/idatt/view/primary/stock/StockController.java b/src/main/java/edu/ntnu/idi/idatt/view/primary/stock/StockController.java index 078074a..0d80656 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/primary/stock/StockController.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/primary/stock/StockController.java @@ -9,6 +9,7 @@ import edu.ntnu.idi.idatt.session.UserSession; import edu.ntnu.idi.idatt.storage.SessionManager; import edu.ntnu.idi.idatt.view.components.AbstractController; +import edu.ntnu.idi.idatt.view.components.ui.UIAlert; import edu.ntnu.idi.idatt.view.util.CssUtils; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; @@ -95,7 +96,20 @@ public void buyButtonClicked() { if (purchase.compareTo(BigDecimal.ZERO) <= 0) { // Flush after color change and new press, while still calculating everything. model.getResultMessageColorProperty().set(CssUtils.RED); - model.getResultMessage().set("Balance too low!"); + model.getResultMessage().set("Balance too low."); + return; + } + + UIAlert buyConfirmation = new UIAlert("Confirmation required", + "Do you wish to proceed the purchase?", + String.format("Purchasing %.2f [%s] for %.2f $", + share.getQuantity(), share.getStock().getSymbol(), purchaseCalculator.calculateTotal())); + + boolean result = buyConfirmation.displayAwaitResponse(); + + if (!result) { + model.getResultMessageColorProperty().set(CssUtils.RED); + model.getResultMessage().set("Transaction canceled."); return; } @@ -104,8 +118,7 @@ public void buyButtonClicked() { this.setTotalProfits(); model.getResultMessageColorProperty().set(CssUtils.GREEN); model.getResultMessage().set("Purchase completed!"); - SessionManager.saveSession(); - + session.updateGameState(); } private void displayBuyInfo(String amountString) { @@ -168,8 +181,8 @@ public void setAllTimeScore() { } public void setOwnedAmount() { - double ownedAmount = session.getPlayer().getPortfolio().getOwnedAmount( - this.stock.getSymbol()).doubleValue(); + BigDecimal ownedAmount = session.getPlayer().getPortfolio().getOwnedAmount( + this.stock.getSymbol()); String symbol = this.stock.getSymbol(); String format = String.format("%.2f [%s]", ownedAmount, symbol); @@ -178,10 +191,10 @@ public void setOwnedAmount() { } public void setTotalProfits() { - double profit = session.getPlayer().getPortfolio().getProfitFromStock( - this.stock.getSymbol()).doubleValue(); - double profitPercent = session.getPlayer().getPortfolio().getChangeFromStock( - this.stock.getSymbol()).doubleValue(); + BigDecimal profit = session.getPlayer().getPortfolio().getProfitFromStock( + this.stock.getSymbol()); + BigDecimal profitPercent = session.getPlayer().getPortfolio().getChangeFromStock( + this.stock.getSymbol()); String format = String.format("%.2f $ (%.2f %%)", profit, profitPercent);