TransactionComponent.java
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;
/**
* Transaction component class.
*
* <p>
* JavaFX Component for handeling repetative tasks.
* Creates a visual representation of a given transaction.
* </p>
*
* @see Transaction
*/
public class TransactionComponent extends VBox {
/**
* Constructor for TransactionComponent
*
* @param transaction - Transaction to create component for.
*/
public TransactionComponent(Transaction transaction) {
Stock stock = transaction.getShare().getStock();
Label title;
Label name = new Label(stock.toString());
Label totalValue;
Label taxComissionValue;
Label grossValue;
Label amountOfShares;
Label saleProfit;
amountOfShares = new Label(
String.format("Shares: %.2f [%s]", transaction.getShare().getQuantity(), stock.getSymbol()));
if (transaction instanceof Purchase) {
title = new Label("PURCHASE");
PurchaseCalculator pCalc = (PurchaseCalculator) transaction.getCalculator();
totalValue = new Label(String.format("Total: %.2f USD", pCalc.calculateTotal()));
taxComissionValue = new Label(String.format(
"Tax & Comission: %.2f USD", pCalc.calculateTax().add(pCalc.calculateCommision())));
grossValue = new Label(String.format("Gross: %.2f USD", pCalc.calculateGross()));
saleProfit = new Label();
CssUtils.apply(title, CssUtils.GREEN);
} else if (transaction instanceof Sale) {
title = new Label("SALE");
CssUtils.apply(title, CssUtils.RED);
SaleCalculator sCalc = (SaleCalculator) transaction.getCalculator();
totalValue = new Label(String.format("Total: %.2f USD", sCalc.calculateTotal()));
taxComissionValue = new Label(String.format(
"Tax & Comission: %.2f USD", sCalc.calculateTax().add(sCalc.calculateCommision())));
grossValue = new Label(String.format("Gross: %.2f USD", sCalc.calculateGross()));
saleProfit = new Label(String.format("Profit: %.2f USD", sCalc.calculateProfit()));
CssUtils.apply(saleProfit, CssUtils.generateValueColors(sCalc.calculateProfit()));
}
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);
CssUtils.apply(this, CssUtils.GRAY);
CssUtils.apply(title, CssUtils.BIG_TEXT_32);
CssUtils.apply(name, CssUtils.MED_TEXT_24);
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());
}
}