diff --git a/src/main/java/millions/view/GameView.java b/src/main/java/millions/view/GameView.java index 64acd71..32b03ce 100644 --- a/src/main/java/millions/view/GameView.java +++ b/src/main/java/millions/view/GameView.java @@ -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; @@ -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; @@ -39,8 +43,8 @@ public class GameView extends BorderPane implements PlayerListener, ExchangeList private final TextField searchField = new TextField(); private final ListView stocksList = new ListView<>(); - private final ListView portfolioList = new ListView<>(); - private final ListView transactionsList = new ListView<>(); + private final TableView portfolioTable = new TableView<>(); + private final TableView 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(); @@ -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 symbolColumn = new TableColumn<>("Symbol"); + symbolColumn.setCellValueFactory( + data -> new SimpleStringProperty(data.getValue().getStock().getSymbol())); + + TableColumn quantityColumn = new TableColumn<>("Quantity"); + quantityColumn.setCellValueFactory( + data -> new SimpleStringProperty(data.getValue().getQuantity().toPlainString())); + + TableColumn purchasePriceColumn = new TableColumn<>("Purchase price"); + purchasePriceColumn.setCellValueFactory( + data -> new SimpleStringProperty(data.getValue().getPurchasePrice().toPlainString())); + + TableColumn currentPriceColumn = new TableColumn<>("Current price"); + currentPriceColumn.setCellValueFactory( + data -> + new SimpleStringProperty(data.getValue().getStock().getSalesPrice().toPlainString())); + + TableColumn 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 typeColumn = new TableColumn<>("Type"); + typeColumn.setCellValueFactory( + data -> new SimpleStringProperty(data.getValue().getClass().getSimpleName())); + + TableColumn symbolColumn = new TableColumn<>("Symbol"); + symbolColumn.setCellValueFactory( + data -> new SimpleStringProperty(data.getValue().getShare().getStock().getSymbol())); + + TableColumn quantityColumn = new TableColumn<>("Quantity"); + quantityColumn.setCellValueFactory( + data -> new SimpleStringProperty(data.getValue().getShare().getQuantity().toPlainString())); + + TableColumn weekColumn = new TableColumn<>("Week"); + weekColumn.setCellValueFactory( + data -> new SimpleStringProperty(String.valueOf(data.getValue().getWeek()))); + + TableColumn 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() {