Skip to content

Commit

Permalink
Feat: updated model
Browse files Browse the repository at this point in the history
Updated certain aspects of the model to include listenable properties and more accuracy when calculating values.
  • Loading branch information
tommyah committed May 13, 2026
1 parent f1a96a6 commit de55313
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();

Expand Down
24 changes: 20 additions & 4 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
* */
Expand All @@ -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();
}
Expand Down Expand Up @@ -107,6 +115,7 @@ public void addMoney(final BigDecimal amount) {
*/
public void withdrawMoney(final BigDecimal amount) {
money = money.subtract(amount);

}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -157,13 +173,13 @@ 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());
} else if (transaction instanceof Sale sale) {
addMoney(sale.getCalculator().calculateTotal());
portfolio.removeShare(sale.getShare());
}
networthAsFloatProp.setValue(getNetWorth().floatValue());
}
}
Original file line number Diff line number Diff line change
@@ -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;

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

0 comments on commit de55313

Please sign in to comment.