From acd52fab9b5c283eba47824c51e7c9d7291c9fdd Mon Sep 17 00:00:00 2001 From: = Date: Wed, 13 May 2026 10:30:20 +0200 Subject: [PATCH] Feat: Made week a listenable integer. Also added a method in player for handling transactions --- .../idatt2003/g40/mappe/engine/Exchange.java | 39 +++++++++++++++---- .../idi/idatt2003/g40/mappe/model/Player.java | 17 ++++++++ 2 files changed, 49 insertions(+), 7 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 8eec48d..84f9611 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 @@ -9,7 +9,13 @@ 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.service.TransactionCalculator; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventPublisher; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventType; import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator; +import javafx.beans.property.IntegerProperty; +import javafx.beans.property.SimpleIntegerProperty; import java.math.BigDecimal; import java.util.ArrayList; @@ -40,7 +46,7 @@ public final class Exchange { /** * Current week (set to 1 in constructor). * */ - private int week; + private final IntegerProperty week = new SimpleIntegerProperty(1); /** * Map of {@link Stock} objects. Key is stock symbol. Value is stock. @@ -65,7 +71,6 @@ public Exchange(final String name, final List stocks) throws IllegalArgum throw new IllegalArgumentException("Invalid exchange parameters!"); } this.name = name; - this.week = 1; this.stockMap = new HashMap<>(); this.random = new Random(); for (Stock stock : stocks) { @@ -88,9 +93,25 @@ public String getName() { * @return week. * */ public int getWeek() { + return week.get(); + } + + /** + * Getter method for the {@link IntegerProperty} object of week. + * + * @return week. + * */ + public IntegerProperty weekProperty() { return week; } + /** + * Advances the week. + * */ + public void nextWeek() { + week.set(week.get() + 1); + } + /** * Method for checking whether exchange has a stock. * @@ -159,8 +180,10 @@ public Transaction buy(final String symbol, Stock stock = stockMap.get(symbol); Share share = new Share(stock, quantity, stock.getSalesPrice()); TransactionCalculator calculator = new PurchaseCalculator(share); - player.withdrawMoney(calculator.calculateTotal()); - return new Purchase(share, week, calculator); + Purchase purchase = new Purchase(share, week.get(), calculator); + player.handleTransaction(purchase); + purchase.commit(player); + return purchase; } /** @@ -179,15 +202,17 @@ public Transaction sell(final Share share, final Player player) throw new IllegalArgumentException("Invalid sell!"); } TransactionCalculator calculator = new SaleCalculator(share); - player.addMoney(calculator.calculateTotal()); - return new Sale(share, week, calculator); + Sale sale = new Sale(share, week.get(), calculator); + player.handleTransaction(sale); + sale.commit(player); + return sale; } /** * Method for advancing time, increasing the amount of weeks. * */ public void advance() { - week++; + nextWeek(); 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 971313e..4fc551d 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 @@ -149,4 +149,21 @@ public BigDecimal getNetWorth() { public PlayerStatus getStatus() { return PlayerStatusController.getStatus(this); } + + /** + * Method for handling a transaction for the player. + * + * @param transaction the transaction to handle. + * */ + 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()); + } + } }