Skip to content

Commit

Permalink
refactor: Correct model-view hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsa committed May 14, 2026
1 parent 848b98b commit c821d69
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
10 changes: 0 additions & 10 deletions src/main/java/edu/ntnu/idi/idatt/model/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}

/**
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/edu/ntnu/idi/idatt/session/UserSession.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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() {
Expand All @@ -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<BigDecimal> moneyProperty = new SimpleObjectProperty<>(BigDecimal.ZERO);
private final SimpleObjectProperty<BigDecimal> netWorthProperty = new SimpleObjectProperty<>(BigDecimal.ZERO);

public SimpleDoubleProperty moneyProperty() {
public SimpleObjectProperty<BigDecimal> moneyProperty() {
return moneyProperty;
}

public SimpleDoubleProperty netWorthProperty() {
public SimpleObjectProperty<BigDecimal> netWorthProperty() {
return netWorthProperty;
}

public void updateGameState() {
moneyProperty.set(player.getMoney());
netWorthProperty.set(player.getNetWorth());
SessionManager.saveSession();
}

public SessionBundle getSession() {
return new SessionBundle(player, exchange);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static Parent createMenu(String title, List<String> buttonLables, ActionE
Button progress = new Button("Advance to next week");
progress.setOnAction((e) -> {
UserSession.getInstance().getExchange().advance();
UserSession.getInstance().updateGameState();
SceneFactory.reloadCurrent();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

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

Expand Down

0 comments on commit c821d69

Please sign in to comment.