Skip to content

Add showStockStats() and addStockStats() methods after pulling new main #41

Merged
merged 1 commit into from
May 26, 2026
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 95 additions & 7 deletions src/main/java/View/MainGameScene.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package View;

import Model.*;
import java.math.BigDecimal;
import java.util.List;

import Model.Share;
import Model.Stock;
import Model.Exchange.Exchange;
import Model.Exchange.ExchangeObserver;
import Model.Player.Player;
Expand All @@ -9,15 +13,28 @@
import Model.Sale.Sale;
import Model.Sale.SaleCalculator;
import Model.Transaction.Transaction;

import java.math.BigDecimal;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.Separator;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;


/**
Expand Down Expand Up @@ -178,8 +195,22 @@ private VBox createStocksPanel() {
searchBox.getChildren().addAll(search, searchBtn, filter);
loadStocks.run();

// Double-click a row to view price statistics
table.setOnMouseClicked(event -> {
if (event.getClickCount() == 2) {
StockRow selected = table.getSelectionModel().getSelectedItem();
if (selected != null) {
showStockStats(selected.s);
}
}
});

VBox.setVgrow(table, Priority.ALWAYS);
panel.getChildren().addAll(searchBox, table);

Label hint = new Label("Double-click a row to view price history");
hint.getStyleClass().add("file-label");
panel.getChildren().addAll(searchBox, table, hint);

return panel;
}

Expand Down Expand Up @@ -566,6 +597,63 @@ private BigDecimal getNetWorth() {
return player.getMoney().add(player.getPortfolio().getNetWorth());
}

private void showStockStats(Stock stock) {
Dialog<Void> dialog = new Dialog<>();
dialog.setTitle(stock.getSymbol() + " — " + stock.getCompany());
dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);

VBox content = new VBox(12);
content.setPrefWidth(340);

// --- Number Sumary ---
BigDecimal change = stock.getLatestPriceChange();
String changeSign = change.compareTo(BigDecimal.ZERO) >= 0 ? "+" : "";

GridPane summary = new GridPane();
summary.setHgap(16);
summary.setVgap(6);
addStatRow(summary, 0, "Current price:", "$" + formatMoney(stock.getSalesPrice()));
addStatRow(summary, 1, "Week change:", changeSign + formatMoney(change));
addStatRow(summary, 2, "All-time high:", "$" + formatMoney(stock.getHighestPrice()));
addStatRow(summary, 3, "All-time low:", "$" + formatMoney(stock.getLowestPrice()));
addStatRow(summary, 4, "Weeks tracked:", String.valueOf(stock.getHistoricalPrices().size()));

// --- Price history list ---
Label histHeading = new Label("Price history:");
histHeading.setStyle("-fx-font-weight: bold;");

ListView<String> histList = new ListView<>();
histList.setPrefHeight(180);
List<BigDecimal> prices = stock.getHistoricalPrices();
ObservableList<String> rows = FXCollections.observableArrayList();
for (int i = 0; i < prices.size(); i++) {
String change_i = "";
if (i > 0) {
BigDecimal diff = prices.get(i).subtract(prices.get(i - 1));
change_i = " (" + (diff.compareTo(BigDecimal.ZERO) >= 0 ? "+" : "") +
formatMoney(diff) + ")";
}
rows.add("Week " + (i + 1) + ": $" + formatMoney(prices.get(i)) + change_i);
}
histList.setItems(rows);

// Scroll to the most recent week
if (!rows.isEmpty()) histList.scrollTo(rows.size() - 1);

content.getChildren().addAll(summary, new Separator(), histHeading, histList);
dialog.getDialogPane().setContent(content);
dialog.showAndWait();
}

/** Adds a label and value to the given row. */
private void addStatRow(GridPane grid, int row, String labelText, String valueText) {
Label lbl = new Label(labelText);
lbl.setStyle("-fx-font-weight: bold;");
Label val = new Label(valueText);
grid.add(lbl, 0, row);
grid.add(val, 1, row);
}

private void showConfirmation(String title, Transaction t) {
StringBuilder msg = new StringBuilder();
if (t instanceof Purchase) {
Expand Down