Skip to content

Commit

Permalink
feat: Implement TransactionComponent class
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsa committed May 13, 2026
1 parent 6830e37 commit c151758
Showing 1 changed file with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package edu.ntnu.idi.idatt.view.components.elements;

import java.util.List;

import edu.ntnu.idi.idatt.model.market.Stock;
import edu.ntnu.idi.idatt.model.transaction.Purchase;
import edu.ntnu.idi.idatt.model.transaction.Sale;
import edu.ntnu.idi.idatt.model.transaction.Transaction;
import edu.ntnu.idi.idatt.service.transaction.PurchaseCalculator;
import edu.ntnu.idi.idatt.service.transaction.SaleCalculator;
import edu.ntnu.idi.idatt.view.components.ui.UICompositor;
import edu.ntnu.idi.idatt.view.util.CssUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

public class TransactionComponent extends VBox {

public TransactionComponent(Transaction transaction) {

Stock stock = transaction.getShare().getStock();

Label title;
Label name = new Label(stock.getCompany() + " (" + stock.getSymbol() + ")");
Label totalValue;
Label taxComissionValue;
Label grossValue;
Label amountOfShares;
Label saleProfit;

if (transaction instanceof Purchase) {
title = new Label("PURCHASE");
CssUtils.apply(title, CssUtils.GREEN);

PurchaseCalculator calculator = (PurchaseCalculator) transaction.getCalculator();
totalValue = new Label("Total: " + calculator.calculateTotal() + " USD");
taxComissionValue = new Label(
"Tax & Comission: " + calculator.calculateTax().add(calculator.calculateCommision()) + " USD");
grossValue = new Label("Gross: " + calculator.calculateGross() + " USD");
amountOfShares = new Label("Shares: " + transaction.getShare().getQuantity() + " [" + stock.getSymbol() + "]");
saleProfit = new Label();

} else if (transaction instanceof Sale) {
title = new Label("SALE");
CssUtils.apply(title, CssUtils.RED);

SaleCalculator calculator = (SaleCalculator) transaction.getCalculator();
totalValue = new Label("Total: " + calculator.calculateTotal() + " USD");
taxComissionValue = new Label(
"Tax & Comission: " + calculator.calculateTax().add(calculator.calculateCommision()) + " USD");
grossValue = new Label("Gross: " + calculator.calculateGross() + " USD");
amountOfShares = new Label("Shares: " + transaction.getShare().getQuantity() + " [" + stock.getSymbol() + "]");
saleProfit = new Label("Profit: " + calculator.calculateProfit() + " USD");

CssUtils.apply(saleProfit, CssUtils.generateValueColors(calculator.calculateProfit().doubleValue()));
}

else {
System.out.println("Failed to initialize transactionComponent!");
return;
}

this.setPrefSize(400, Double.MAX_VALUE);
this.setMinWidth(400);
this.setPadding(new Insets(40));
this.setAlignment(Pos.TOP_CENTER);

// TODO: CHANGE AND IN STOCKComponent
this.setStyle("-fx-background-color: #404950;");

CssUtils.apply(title, CssUtils.BIG_TEXT_32);
CssUtils.apply(name, CssUtils.BIG_TEXT_32);
List<Label> labels = List.of(totalValue, taxComissionValue, grossValue, amountOfShares, saleProfit);
labels.forEach(l -> CssUtils.apply(l, CssUtils.MED_TEXT_16));

UICompositor transactionComponent = new UICompositor.Builder()
.parent(new VBox())
.growWithAlignment(Pos.CENTER)
.addContent(title)
.addContent(name)
.filler()
.addContent(totalValue)
.addContent(taxComissionValue)
.addContent(grossValue)
.addContent(amountOfShares)
.addContent(saleProfit)
.build();

this.getChildren().add(transactionComponent.makeUI());

}

}

0 comments on commit c151758

Please sign in to comment.