Skip to content

Commit

Permalink
feat: Adding simple portifolio and transactions list
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed May 23, 2026
1 parent 53ec0ce commit 0459af1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/main/java/millions/view/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 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 @@ -76,8 +78,8 @@ private TabPane createTabs() {
TabPane tabPane = new TabPane();
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
tabPane.getTabs().add(createStocksTab());
tabPane.getTabs().add(new Tab("Portfolio", new VBox()));
tabPane.getTabs().add(new Tab("Transactions", new VBox()));
tabPane.getTabs().add(createPortfolioTab());
tabPane.getTabs().add(createTransactionsTab());
return tabPane;
}

Expand Down Expand Up @@ -124,6 +126,16 @@ private Tab createStocksTab() {
return new Tab("Stocks", outer);
}

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

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

private void configureButtons() {
buyButton.setOnAction(event -> buySelectedStock());
sellButton.setOnAction(event -> sellSelectedShare());
Expand Down Expand Up @@ -163,6 +175,8 @@ protected void updateItem(Stock stock, boolean empty) {
private void refreshAll() {
refreshPlayerInfo();
refreshStocks();
refreshPortfolio();
refreshTransactions();
}

private void refreshPlayerInfo() {
Expand Down Expand Up @@ -249,6 +263,31 @@ private void refreshQuantityControls(Stock stock) {
}
}

private void refreshPortfolio() {
Player player = controller.getPlayer();

portfolioList
.getItems()
.setAll(
player.getPortfolio().getShares().stream()
.map(ViewUtils::formatPortfolioShare)
.toList());
}

private void refreshTransactions() {
Player player = controller.getPlayer();
if (player == null) {
return;
}

transactionsList
.getItems()
.setAll(
player.getTransactionArchive().getTransactions().stream()
.map(ViewUtils::formatTransaction)
.toList());
}

private void buySelectedStock() {
Stock selectedStock = stocksList.getSelectionModel().getSelectedItem();
if (selectedStock == null) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/millions/view/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javafx.scene.control.ListCell;
import millions.model.Share;
import millions.model.Stock;
import millions.model.Transaction;

/** Small utility helpers for view formatting and simple calculations. */
public final class ViewUtils {
Expand Down Expand Up @@ -74,4 +75,22 @@ public static int clampQuantity(int quantity, int maxBuyable) {
}
return quantity;
}

public static String formatPortfolioShare(Share share) {
return share.getStock().getSymbol()
+ "|"
+ share.getQuantity()
+ "|"
+ share.getPurchasePrice();
}

public static String formatTransaction(Transaction transaction) {
return transaction.getClass().getSimpleName()
+ "|"
+ transaction.getShare().getStock().getSymbol()
+ "|"
+ transaction.getShare().getQuantity()
+ "|"
+ transaction.getWeek();
}
}

0 comments on commit 0459af1

Please sign in to comment.