From de5531374c2883da9427243d8d3ebeb30a7e7a2e Mon Sep 17 00:00:00 2001 From: = Date: Wed, 13 May 2026 18:33:37 +0200 Subject: [PATCH] Feat: updated model Updated certain aspects of the model to include listenable properties and more accuracy when calculating values. --- .../idatt2003/g40/mappe/engine/Exchange.java | 10 +------- .../idi/idatt2003/g40/mappe/model/Player.java | 24 +++++++++++++++---- .../idatt2003/g40/mappe/model/Portfolio.java | 4 ++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java index 84f9611..fffe422 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java @@ -105,13 +105,6 @@ public IntegerProperty weekProperty() { return week; } - /** - * Advances the week. - * */ - public void nextWeek() { - week.set(week.get() + 1); - } - /** * Method for checking whether exchange has a stock. * @@ -212,8 +205,7 @@ public Transaction sell(final Share share, final Player player) * Method for advancing time, increasing the amount of weeks. * */ public void advance() { - nextWeek(); - + week.set(week.get() + 1); for (Stock stock : stockMap.values()) { BigDecimal currentPrice = stock.getSalesPrice(); diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java index 4fc551d..fc7ff3b 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java @@ -4,6 +4,8 @@ import edu.ntnu.idi.idatt2003.g40.mappe.engine.TransactionArchive; import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator; import java.math.BigDecimal; +import javafx.beans.property.FloatProperty; +import javafx.beans.property.SimpleFloatProperty; /** * Represents a player in the system. @@ -34,6 +36,11 @@ public final class Player { * */ private BigDecimal money; + /** + * Current net-worth of player as a listenable {@link FloatProperty} object. + * */ + private final FloatProperty networthAsFloatProp = new SimpleFloatProperty(0); + /** * The players' portfolio, holding their shares. * */ @@ -60,6 +67,7 @@ public Player(final String name, final BigDecimal startingMoney) throws IllegalA this.name = name; this.startingMoney = startingMoney; this.money = this.startingMoney; + this.networthAsFloatProp.setValue(this.startingMoney); this.portfolio = new Portfolio(); this.transactionArchive = new TransactionArchive(); } @@ -107,6 +115,7 @@ public void addMoney(final BigDecimal amount) { */ public void withdrawMoney(final BigDecimal amount) { money = money.subtract(amount); + } /** @@ -134,9 +143,16 @@ public TransactionArchive getTransactionArchive() { * @return the net worth of the player. * */ public BigDecimal getNetWorth() { - BigDecimal netWorth = new BigDecimal("0"); - netWorth = netWorth.add(portfolio.getNetWorth()).add(money); - return netWorth; + return portfolio.getNetWorth().add(money); + } + + /** + * Get net-worth as a {@link FloatProperty} object, allowing listening for changes. + * + * @return FloatProperty. + * */ + public FloatProperty getNetWorthAsFloatProperty() { + return networthAsFloatProp; } /** @@ -157,7 +173,6 @@ public PlayerStatus getStatus() { * */ public void handleTransaction(final Transaction transaction) { transactionArchive.add(transaction); - if (transaction instanceof Purchase purchase) { withdrawMoney(purchase.getCalculator().calculateTotal()); portfolio.addShare(purchase.getShare()); @@ -165,5 +180,6 @@ public void handleTransaction(final Transaction transaction) { addMoney(sale.getCalculator().calculateTotal()); portfolio.removeShare(sale.getShare()); } + networthAsFloatProp.setValue(getNetWorth().floatValue()); } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Portfolio.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Portfolio.java index 756dc96..1d53998 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Portfolio.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Portfolio.java @@ -1,5 +1,6 @@ package edu.ntnu.idi.idatt2003.g40.mappe.model; +import edu.ntnu.idi.idatt2003.g40.mappe.service.PurchaseCalculator; import edu.ntnu.idi.idatt2003.g40.mappe.service.SaleCalculator; import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator; @@ -117,9 +118,8 @@ public BigDecimal getNetWorth() { BigDecimal netWorth = new BigDecimal("0"); for (Share s : shares) { - SaleCalculator calculator = new SaleCalculator(s); - netWorth = netWorth.add(calculator.calculateTotal()); + netWorth = netWorth.add(s.getStock().getSalesPrice().multiply(s.getQuantity())); } return netWorth; }