diff --git a/millions/src/main/java/no/ntnu/gruppe53/model/Player.java b/millions/src/main/java/no/ntnu/gruppe53/model/Player.java index 7c2610d..21a9e9e 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/model/Player.java +++ b/millions/src/main/java/no/ntnu/gruppe53/model/Player.java @@ -30,7 +30,7 @@ public class Player { /** * Creates a player with a given starting balance. - *

Adds a listener to the netWorth property to notify of changes in the player's net worth.

+ *

Subscribes to the netWorth property to notify of changes in the player's net worth.

* * @param name the player's name; must not be {@code null} or blank * @param startingMoney the initial monetary balance of the @@ -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(); } @@ -72,7 +71,7 @@ public ObjectProperty getNetWorthProperty() { /** * Mirrors the change in the player's net worth to the observable property. */ - public void updateNetWorth() { + private void updateNetWorth() { netWorthProperty.set(getNetWorth()); } diff --git a/millions/src/main/java/no/ntnu/gruppe53/model/Portfolio.java b/millions/src/main/java/no/ntnu/gruppe53/model/Portfolio.java index 1164f6b..6a3c5f9 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/model/Portfolio.java +++ b/millions/src/main/java/no/ntnu/gruppe53/model/Portfolio.java @@ -159,7 +159,7 @@ public ObjectProperty netWorthProperty() { /** * Mirrors the portfolio's net worth to the observable. */ - public void updateNetWorth() { + private void updateNetWorth() { netWorthProperty.set(getNetWorth()); } } \ No newline at end of file diff --git a/millions/src/main/java/no/ntnu/gruppe53/model/SaleCalculator.java b/millions/src/main/java/no/ntnu/gruppe53/model/SaleCalculator.java index e96015a..c6afe07 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/model/SaleCalculator.java +++ b/millions/src/main/java/no/ntnu/gruppe53/model/SaleCalculator.java @@ -21,6 +21,7 @@ public class SaleCalculator implements TransactionCalculator { private final ObjectProperty commissionProperty = new SimpleObjectProperty<>(); private final ObjectProperty taxProperty = new SimpleObjectProperty<>(); private final ObjectProperty totalProperty = new SimpleObjectProperty<>(); + private final ObjectProperty profitProperty = new SimpleObjectProperty<>(); /** * Sets the quantity, purchase and sales price based on the given share. @@ -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))); } /** @@ -135,4 +137,10 @@ public ObjectProperty getTaxProperty() { public ObjectProperty getTotalProperty() { return totalProperty; } + + /** + * Returns an observable of the calculated profit + * @return an observable of the calculated profit + */ + public ObjectProperty getProfitProperty() {return profitProperty;} } \ No newline at end of file diff --git a/millions/src/main/java/no/ntnu/gruppe53/view/PortfolioView.java b/millions/src/main/java/no/ntnu/gruppe53/view/PortfolioView.java index 290d375..1d05348 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/view/PortfolioView.java +++ b/millions/src/main/java/no/ntnu/gruppe53/view/PortfolioView.java @@ -55,6 +55,7 @@ public class PortfolioView extends BorderPane { private TextField commission; private TextField tax; private TextField total; + private TextField profit; //PortfolioListView private ListView portfolioListView; @@ -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. @@ -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")); @@ -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(""); + } + }); } diff --git a/millions/src/main/java/no/ntnu/gruppe53/view/TransactionArchiveView.java b/millions/src/main/java/no/ntnu/gruppe53/view/TransactionArchiveView.java index fb5e11b..91e64b4 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/view/TransactionArchiveView.java +++ b/millions/src/main/java/no/ntnu/gruppe53/view/TransactionArchiveView.java @@ -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; @@ -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"); @@ -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) )); }; diff --git a/millions/src/main/resources/i18n/lang.properties b/millions/src/main/resources/i18n/lang.properties index 61c65d9..4ac2825 100644 --- a/millions/src/main/resources/i18n/lang.properties +++ b/millions/src/main/resources/i18n/lang.properties @@ -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 @@ -53,6 +53,7 @@ portfolioGross = Gross: portfolioCommission = Commission: portfolioTax = Tax: portfolioTotal = Total: +portfolioProfit = Profit: sellButton = Sell portfolioShareQuantity = Qty: portfolioBuyPrice = Buy price: @@ -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 diff --git a/millions/src/main/resources/i18n/lang_en.properties b/millions/src/main/resources/i18n/lang_en.properties index 0c7bae8..4ac2825 100644 --- a/millions/src/main/resources/i18n/lang_en.properties +++ b/millions/src/main/resources/i18n/lang_en.properties @@ -53,6 +53,7 @@ portfolioGross = Gross: portfolioCommission = Commission: portfolioTax = Tax: portfolioTotal = Total: +portfolioProfit = Profit: sellButton = Sell portfolioShareQuantity = Qty: portfolioBuyPrice = Buy price: @@ -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 diff --git a/millions/src/main/resources/i18n/lang_no.properties b/millions/src/main/resources/i18n/lang_no.properties index b61e8e7..faf680f 100644 --- a/millions/src/main/resources/i18n/lang_no.properties +++ b/millions/src/main/resources/i18n/lang_no.properties @@ -53,6 +53,7 @@ portfolioGross = Brutto: portfolioCommission = Kurtasje: portfolioTax = Skatt: portfolioTotal = Total: +portfolioProfit = Profitt: sellButton = Selg portfolioShareQuantity = Ant: portfolioBuyPrice = Kjøpspris: @@ -62,4 +63,4 @@ portfolioCurrentPrice = Gjeldende pris: historyTitle=Transaksjonshistorikk historyTypePurchase=Kjøp historyTypeSale=Salg -historyCellFormat=Uke %d | %s | %s | Ant: %s | Total: $%s \ No newline at end of file +historyCellFormat=Uke %d | %s | %s | Pris: %s | Ant: %s | Brutto: %s | Kurtasje: %s | Skatt: %s | Total: $%s | Profit: %s \ No newline at end of file