Skip to content

Commit

Permalink
Feat: Made week a listenable integer. Also added a method in player f…
Browse files Browse the repository at this point in the history
…or handling transactions
  • Loading branch information
tommyah committed May 13, 2026
1 parent 1800ef9 commit acd52fa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -65,7 +71,6 @@ public Exchange(final String name, final List<Stock> 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) {
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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();
Expand Down
17 changes: 17 additions & 0 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 @@ -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());
}
}
}

0 comments on commit acd52fa

Please sign in to comment.