Skip to content

Commit

Permalink
feat: Adding transactions table with colors
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed May 25, 2026
1 parent 1e156e1 commit 9d4abda
Showing 1 changed file with 78 additions and 6 deletions.
84 changes: 78 additions & 6 deletions src/main/java/millions/view/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.util.List;
import javafx.beans.property.SimpleStringProperty; // For data binding for table string
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
Expand All @@ -13,6 +14,9 @@
import javafx.scene.control.Slider;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.BorderPane;
Expand All @@ -39,8 +43,8 @@ public class GameView extends BorderPane implements PlayerListener, ExchangeList

private final TextField searchField = new TextField();
private final ListView<Stock> stocksList = new ListView<>();
private final ListView<String> portfolioList = new ListView<>();
private final ListView<String> transactionsList = new ListView<>();
private final TableView<Share> portfolioTable = new TableView<>();
private final TableView<Transaction> transactionsTable = new TableView<>();
private final Label selectedStockLabel = new Label("Select a stock to see chart");
private final Label ownedQuantityLabel = new Label("Owned: 0");
private final Label actionStatusLabel = new Label();
Expand Down Expand Up @@ -127,13 +131,81 @@ private Tab createStocksTab() {
}

private Tab createPortfolioTab() {
portfolioList.setPlaceholder(new Label("No shares yet"));
return new Tab("Portfolio", portfolioList);
portfolioTable.setPlaceholder(new Label("No shares yet"));

TableColumn<Share, String> symbolColumn = new TableColumn<>("Symbol");
symbolColumn.setCellValueFactory(
data -> new SimpleStringProperty(data.getValue().getStock().getSymbol()));

TableColumn<Share, String> quantityColumn = new TableColumn<>("Quantity");
quantityColumn.setCellValueFactory(
data -> new SimpleStringProperty(data.getValue().getQuantity().toPlainString()));

TableColumn<Share, String> purchasePriceColumn = new TableColumn<>("Purchase price");
purchasePriceColumn.setCellValueFactory(
data -> new SimpleStringProperty(data.getValue().getPurchasePrice().toPlainString()));

TableColumn<Share, String> currentPriceColumn = new TableColumn<>("Current price");
currentPriceColumn.setCellValueFactory(
data ->
new SimpleStringProperty(data.getValue().getStock().getSalesPrice().toPlainString()));

TableColumn<Share, String> profitColumn = new TableColumn<>("Profit");
profitColumn.setCellValueFactory(
data ->
new SimpleStringProperty(ViewUtils.getShareProfit(data.getValue()).toPlainString()));
profitColumn.setCellFactory(
column ->
new TableCell<>() {
@Override
protected void updateItem(String profit, boolean empty) {
super.updateItem(profit, empty);
if (empty) {
setText(null);
setStyle("");
return;
}
setText(profit);
setStyle(profit.startsWith("-") ? "-fx-text-fill: red;" : "-fx-text-fill: green;");
}
});

portfolioTable
.getColumns()
.addAll(
symbolColumn, quantityColumn, purchasePriceColumn, currentPriceColumn, profitColumn);
return new Tab("Portfolio", portfolioTable);
}

private Tab createTransactionsTab() {
transactionsList.setPlaceholder(new Label("No transactions yet"));
return new Tab("Transactions", transactionsList);
transactionsTable.setPlaceholder(new Label("No transactions yet"));

TableColumn<Transaction, String> typeColumn = new TableColumn<>("Type");
typeColumn.setCellValueFactory(
data -> new SimpleStringProperty(data.getValue().getClass().getSimpleName()));

TableColumn<Transaction, String> symbolColumn = new TableColumn<>("Symbol");
symbolColumn.setCellValueFactory(
data -> new SimpleStringProperty(data.getValue().getShare().getStock().getSymbol()));

TableColumn<Transaction, String> quantityColumn = new TableColumn<>("Quantity");
quantityColumn.setCellValueFactory(
data -> new SimpleStringProperty(data.getValue().getShare().getQuantity().toPlainString()));

TableColumn<Transaction, String> weekColumn = new TableColumn<>("Week");
weekColumn.setCellValueFactory(
data -> new SimpleStringProperty(String.valueOf(data.getValue().getWeek())));

TableColumn<Transaction, String> totalColumn = new TableColumn<>("Total");
totalColumn.setCellValueFactory(
data ->
new SimpleStringProperty(
data.getValue().getCalculator().calculateTotal().toPlainString()));

transactionsTable
.getColumns()
.addAll(typeColumn, symbolColumn, quantityColumn, weekColumn, totalColumn);
return new Tab("Transactions", transactionsTable);
}

private void configureButtons() {
Expand Down

0 comments on commit 9d4abda

Please sign in to comment.