Skip to content

Commit

Permalink
Merge pull request #162 from Team-40-IDATT2003/161-transaction-report…
Browse files Browse the repository at this point in the history
…-when-buyingselling

161 transaction report when buyingselling
  • Loading branch information
etsorens authored May 27, 2026
2 parents 3e7d752 + cd794db commit aa6bad9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public Transaction sell(BigDecimal amount,
List<Share> matchingShares = player.getPortfolio().getShares(stockSymbol);

if (matchingShares.isEmpty()) {
throw new IllegalArgumentException("Player does not own any shares of this stock!");
throw new IllegalArgumentException("No owned shares found of this stock!");
}

BigDecimal totalOwned = matchingShares.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.dashboard;

import edu.ntnu.idi.idatt2003.g40.mappe.engine.Exchange;
import edu.ntnu.idi.idatt2003.g40.mappe.exceptions.NotEnoughMoneyException;
import 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;
Expand Down Expand Up @@ -244,29 +245,39 @@ protected void initInteractions() {
if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())
&& Float.parseFloat(getViewElement().getQuantityInputField().getText()) > 0) {
BigDecimal amountToBuy = new BigDecimal(getViewElement().getQuantityInputField().getText());
Transaction purchase = exchange.buy(
getViewElement().getCurrentStock().getSymbol(),
amountToBuy,
player
);
if (purchase.isCommited()) {
getViewElement().addOwnedShares(purchase.getShare().getQuantity().floatValue());
updatePreviews();
try {
Transaction purchase = exchange.buy(
getViewElement().getCurrentStock().getSymbol(),
amountToBuy,
player
);
if (purchase.isCommited()) {
getViewElement().addOwnedShares(purchase.getShare().getQuantity().floatValue());
displayTransactionReport(purchase, "Purchase successful!", null);
updatePreviews();
}
} catch (NotEnoughMoneyException e) {
displayTransactionReport(null, "Purchase failed!", e.getMessage());
}
}
});

getViewElement().setOnAction(DashBoardActions.SELL_SHARES, () -> {
if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())
&& Float.parseFloat(getViewElement().getQuantityInputField().getText()) > 0) {
Transaction sale = exchange.sell(
new BigDecimal(getViewElement().getQuantityInputField().getText()),
getViewElement().getCurrentStock().getSymbol(),
player);
try {
Transaction sale = exchange.sell(
new BigDecimal(getViewElement().getQuantityInputField().getText()),
getViewElement().getCurrentStock().getSymbol(),
player);

if(sale.isCommited()) {
getViewElement().addOwnedShares(-sale.getShare().getQuantity().floatValue());
displayTransactionReport(sale, "Sale successful!", null);
updatePreviews();
}
} catch (IllegalArgumentException e) {
displayTransactionReport(null, "Sale failed!", e.getMessage());
}
}
});
Expand Down Expand Up @@ -379,7 +390,33 @@ public <T> void handleEvent(final EventData<T> data) {
player.getPortfolio().getTotalShareQuantityBySymbol(s.getSymbol()).floatValue()
);
}

updatePreviews();
}

/**
* Method for displaying a report after performing a transaction attempt (buy/sell).
*
* @param transaction the transaction that occurred, or null if an error stopped execution.
* @param title the title header of the dialog window.
* @param errorMsg the error message to show if the transaction failed.
* */
public void displayTransactionReport(final Transaction transaction, final String title, final String errorMsg) {
if (transaction != null && transaction.isCommited()) {
var calc = transaction.getCalculator();
float tax = calc.calculateTax().floatValue();

getViewElement().showTransactionReport(
title,
transaction.getShare().getStock().getSymbol(),
transaction.getShare().getQuantity().floatValue(),
calc.calculateGross().floatValue(),
calc.calculateCommission().floatValue(),
tax,
calc.calculateTotal().floatValue(),
null
);
} else {
getViewElement().showTransactionReport(title, "", 0f, 0f, 0f, 0f, 0f, errorMsg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Separator;
Expand Down Expand Up @@ -662,4 +665,65 @@ private String formatShares(final float amount) {
String formatted = String.format(java.util.Locale.US, "%.3f", amount);
return formatted.replaceAll("0+$", "").replaceAll("\\.$", "");
}

/**
* Displays a clean transaction report pop-up detailing the financial breakdown.
*
* @param title The title of the dialog box
* (e.g., "Purchase Confirmed").
* @param symbol The stock ticker symbol.
* @param qty The amount of shares processed.
* @param gross The gross value of the transaction.
* @param commission The commission fee charged.
* @param tax The capital gains tax charged.
* @param total The net total amount.
* @param errorMessage Optional error message if transaction did not
* succesfully commit.
*/
public void showTransactionReport(final String title,
final String symbol,
final float qty,
final float gross,
final float commission,
final float tax,
final float total,
final String errorMessage) {
Dialog<Void> dialog = new Dialog<>();
dialog.setTitle(title);

DialogPane dialogPane = dialog.getDialogPane();
dialogPane.getButtonTypes().add(ButtonType.CLOSE);

//NOTE: Requires hard coding styling due to DialogPane behavior.
VBox container = new VBox(10);
container.setStyle("-fx-padding: 20; -fx-min-width: 320;");

if (errorMessage != null && !errorMessage.isEmpty()) {
Label errorLabel = new Label(errorMessage);
errorLabel.setStyle("-fx-text-fill: red; -fx-font-weight: bold; -fx-font-size: 14px;");
container.getChildren().add(errorLabel);
} else {
Label headerLabel = new Label(symbol + " Transaction Summary");
headerLabel.setStyle("-fx-font-weight: bold; -fx-font-size: 14px;");
container.getChildren().addAll(headerLabel, new Separator());

String[][] rows = {
{"Quantity:", String.format("%.3f", qty)},
{"Gross:", String.format("%.2f NOK", gross)},
{"Tax:", String.format("%.2f NOK", tax)},
{"Commission fee:", String.format("%.2f NOK", commission)},
{"Total:", String.format("%.2f NOK", total)}
};

for (String[] rowData : rows) {
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
HBox row = new HBox(new Label(rowData[0]), spacer, new Label(rowData[1]));
container.getChildren().add(row);
}
}

dialogPane.setContent(container);
dialog.showAndWait();
}
}

0 comments on commit aa6bad9

Please sign in to comment.