Skip to content

61 add more info to transactions #62

Merged
merged 8 commits into from
May 25, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions millions/src/main/java/no/ntnu/gruppe53/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Player {

/**
* Creates a player with a given starting balance.
* <p>Adds a listener to the netWorth property to notify of changes in the player's net worth.</p>
* <p>Subscribes to the netWorth property to notify of changes in the player's net worth.</p>
*
* @param name the player's name; must not be {@code null} or blank
* @param startingMoney the initial monetary balance of the
Expand All @@ -55,8 +55,7 @@ public Player(String name, BigDecimal startingMoney) {
this.portfolio = new Portfolio();
this.transactionArchive = new TransactionArchive();

portfolio.netWorthProperty().addListener(
(obs, oldVal, newVal) -> updateNetWorth());
portfolio.netWorthProperty().subscribe(price -> updateNetWorth());
updateNetWorth();
}

Expand All @@ -72,7 +71,7 @@ public ObjectProperty<BigDecimal> getNetWorthProperty() {
/**
* Mirrors the change in the player's net worth to the observable property.
*/
public void updateNetWorth() {
private void updateNetWorth() {
netWorthProperty.set(getNetWorth());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public ObjectProperty<BigDecimal> netWorthProperty() {
/**
* Mirrors the portfolio's net worth to the observable.
*/
public void updateNetWorth() {
private void updateNetWorth() {
netWorthProperty.set(getNetWorth());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class SaleCalculator implements TransactionCalculator {
private final ObjectProperty<BigDecimal> commissionProperty = new SimpleObjectProperty<>();
private final ObjectProperty<BigDecimal> taxProperty = new SimpleObjectProperty<>();
private final ObjectProperty<BigDecimal> totalProperty = new SimpleObjectProperty<>();
private final ObjectProperty<BigDecimal> profitProperty = new SimpleObjectProperty<>();

/**
* Sets the quantity, purchase and sales price based on the given share.
Expand All @@ -36,6 +37,7 @@ public SaleCalculator(Share share) {
this.commissionProperty.set(calculateCommission());
this.taxProperty.set(calculateTax());
this.totalProperty.set(calculateTotal());
this.profitProperty.set(calculateTotal().subtract(purchasePrice.multiply(quantity)));
}

/**
Expand Down Expand Up @@ -135,4 +137,10 @@ public ObjectProperty<BigDecimal> getTaxProperty() {
public ObjectProperty<BigDecimal> getTotalProperty() {
return totalProperty;
}

/**
* Returns an observable of the calculated profit
* @return an observable of the calculated profit
*/
public ObjectProperty<BigDecimal> getProfitProperty() {return profitProperty;}
}
17 changes: 16 additions & 1 deletion millions/src/main/java/no/ntnu/gruppe53/view/PortfolioView.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class PortfolioView extends BorderPane {
private TextField commission;
private TextField tax;
private TextField total;
private TextField profit;

//PortfolioListView
private ListView<Share> portfolioListView;
Expand All @@ -66,6 +67,7 @@ public class PortfolioView extends BorderPane {
private Subscription commissionSubscription;
private Subscription taxSubscription;
private Subscription totalSubscription;
private Subscription profitSubscription;

/**
* Builds the view components and places them in the center pane.
Expand Down Expand Up @@ -204,7 +206,13 @@ protected void updateItem(Share item, boolean empty) {
total.setEditable(false);
total.setMaxWidth(200);

totalBox.getChildren().addAll(totalLabel, total);
Label profitLabel = new Label();
profitLabel.textProperty().bind(lm.bindString("portfolioProfit"));
profit = new TextField();
profit.setEditable(false);
profit.setMaxWidth(200);

totalBox.getChildren().addAll(totalLabel, total, profitLabel, profit);

sellButton = new Button();
sellButton.textProperty().bind(lm.bindString("sellButton"));
Expand Down Expand Up @@ -323,6 +331,13 @@ public void setSaleCalculator(SaleCalculator saleCalculator) {
total.setText("");
}
});
profitSubscription = saleCalculator.getProfitProperty().subscribe(newVal -> {
if (newVal != null) {
profit.setText("$ " + newVal.setScale(2, RoundingMode.HALF_EVEN));
} else {
profit.setText("");
}
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import java.math.RoundingMode;

import javafx.util.Subscription;
import no.ntnu.gruppe53.model.Player;
import no.ntnu.gruppe53.model.Transaction;
import no.ntnu.gruppe53.model.Purchase;
import no.ntnu.gruppe53.model.Sale;
import no.ntnu.gruppe53.model.*;
import no.ntnu.gruppe53.service.FormatBigDecimal;
import no.ntnu.gruppe53.service.LanguageManager;

Expand Down Expand Up @@ -96,17 +93,28 @@ protected void updateItem(Transaction item, boolean empty) {
} else {
Runnable updateTextAction = () -> {
String typeStr;
BigDecimal price = BigDecimal.ZERO;
BigDecimal profit = BigDecimal.ZERO;
// Changed to allow for more transaction types in the future
if (item instanceof Purchase) {
typeStr = lm.getString("historyTypePurchase");
} else if (item instanceof Sale) {
price = item.getShare().getPurchasePrice();
} else if (item instanceof Sale sale) {
typeStr = lm.getString("historyTypeSale");
price = item.getShare().getStock().getSalesPrice();
if (sale.getCalculator() instanceof SaleCalculator saleCalc) {
profit = saleCalc.getProfitProperty().getValue();
}
} else {
typeStr = "ERROR";
}

BigDecimal quantity = item.getShare().getQuantity();
BigDecimal gross = item.getCalculator().calculateGross();
BigDecimal commission = item.getCalculator().calculateCommission();
BigDecimal tax = item.getCalculator().calculateTax();
BigDecimal total = item.getCalculator().calculateTotal();

String symbol = item.getShare().getStock().getSymbol();

String template = lm.getString("historyCellFormat");
Expand All @@ -115,8 +123,13 @@ protected void updateItem(Transaction item, boolean empty) {
item.getWeek(),
typeStr,
symbol,
FormatBigDecimal.formatNumber(price),
FormatBigDecimal.formatNumber(quantity),
FormatBigDecimal.formatNumber(total)
FormatBigDecimal.formatNumber(gross),
FormatBigDecimal.formatNumber(commission),
FormatBigDecimal.formatNumber(tax),
FormatBigDecimal.formatNumber(total),
FormatBigDecimal.formatNumber(profit)
));
};

Expand Down
5 changes: 3 additions & 2 deletions millions/src/main/resources/i18n/lang.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Default file to use if there is an error with language locale
# File for the english language

# StartView
newGame = New Game
Expand Down Expand Up @@ -53,6 +53,7 @@ portfolioGross = Gross:
portfolioCommission = Commission:
portfolioTax = Tax:
portfolioTotal = Total:
portfolioProfit = Profit:
sellButton = Sell
portfolioShareQuantity = Qty:
portfolioBuyPrice = Buy price:
Expand All @@ -62,4 +63,4 @@ portfolioCurrentPrice = Current Price:
historyTitle=Transaction History
historyTypePurchase=Purchase
historyTypeSale=Sale
historyCellFormat=Week %d | %s | %s | Qty: %s | Total: $%s
historyCellFormat=Week %d | %s | %s | Price: %s | Qty: %s | Gross: %s | Commission: %s | Tax: %s | Total: $%s | Profit: %s
3 changes: 2 additions & 1 deletion millions/src/main/resources/i18n/lang_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ portfolioGross = Gross:
portfolioCommission = Commission:
portfolioTax = Tax:
portfolioTotal = Total:
portfolioProfit = Profit:
sellButton = Sell
portfolioShareQuantity = Qty:
portfolioBuyPrice = Buy price:
Expand All @@ -62,4 +63,4 @@ portfolioCurrentPrice = Current Price:
historyTitle=Transaction History
historyTypePurchase=Purchase
historyTypeSale=Sale
historyCellFormat=Week %d | %s | %s | Qty: %s | Total: $%s
historyCellFormat=Week %d | %s | %s | Price: %s | Qty: %s | Gross: %s | Commission: %s | Tax: %s | Total: $%s | Profit: %s
3 changes: 2 additions & 1 deletion millions/src/main/resources/i18n/lang_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ portfolioGross = Brutto:
portfolioCommission = Kurtasje:
portfolioTax = Skatt:
portfolioTotal = Total:
portfolioProfit = Profitt:
sellButton = Selg
portfolioShareQuantity = Ant:
portfolioBuyPrice = Kjøpspris:
Expand All @@ -62,4 +63,4 @@ portfolioCurrentPrice = Gjeldende pris:
historyTitle=Transaksjonshistorikk
historyTypePurchase=Kjøp
historyTypeSale=Salg
historyCellFormat=Uke %d | %s | %s | Ant: %s | Total: $%s
historyCellFormat=Uke %d | %s | %s | Pris: %s | Ant: %s | Brutto: %s | Kurtasje: %s | Skatt: %s | Total: $%s | Profit: %s