Skip to content

Commit

Permalink
Merge branch '26-add-stock-price-statistics' of git.ntnu.no:solvena/P…
Browse files Browse the repository at this point in the history
…rogrammering2_mappe_v26 into 40-add-an-end-of-game-report
  • Loading branch information
Solveig Natvig committed May 26, 2026
2 parents 5343ffd + 9c80788 commit 2e14d49
Show file tree
Hide file tree
Showing 25 changed files with 186 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package Model;

package Model.Exchange;

import Model.Player.Player;
import Model.Purchase.Purchase;
import Model.Share;
import Model.Stock;
import Model.Transaction.Transaction;
import Model.Transaction.TransactionFactory;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;



/**
* Represents a stock exchange where players can buy and sell stocks.
* The Exchange maintains a collection of stocks, advances through weeks,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Model;
package Model.Exchange;

/**
* Observer interface for receiving notifications from the Exchange.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package Model;
package Model.Player;

import Model.Portfolio;
import Model.Transaction.TransactionArchive;
import java.math.BigDecimal;


/**
* Represents a player in the stock trading game.
* A Player manages their personal portfolio of shares, cash balance, and transaction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Model;
package Model.Player;

/**
* Enum representing the player's trading status level.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/Model/Portfolio.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package Model;


import Model.Sale.SaleCalculator;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
* Represents a portfolio of shares held by a player.
* A Portfolio manages a collection of Share objects and provides methods to add,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package Model;
package Model.Purchase;

import Model.Player.Player;
import Model.Share;
import Model.Transaction.Transaction;
import java.math.BigDecimal;


/**
* Represents a stock purchase transaction.
* A Purchase transaction represents a player buying shares. When committed,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Model;
package Model.Purchase;

import Model.Share;
import Model.Transaction.TransactionCalculator;
import java.math.BigDecimal;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package Model;
package Model.Sale;

import Model.Player.Player;
import Model.Share;
import Model.Transaction.Transaction;
import java.math.BigDecimal;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Model;
package Model.Sale;

import Model.Share;
import Model.Transaction.TransactionCalculator;
import java.math.BigDecimal;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package Model;
package Model.Transaction;

import Model.Player.Player;
import Model.Share;


/**
* Abstract base class for all transactions (purchases and sales).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package Model;
package Model.Transaction;

import Model.Purchase.Purchase;
import Model.Sale.Sale;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;



/**
* Maintains a chronological archive of all transactions executed by a player.
* The TransactionArchive allows retrieval of transactions by week and type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Model;
package Model.Transaction;

import java.math.BigDecimal;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package Model;
package Model.Transaction;

import Model.Purchase.Purchase;
import Model.Sale.Sale;
import Model.Share;


/**
* Factory for creating transaction objects.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/View/GameSetupScene.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package View;

import Controller.StockFileHandler;
import Model.Exchange;
import Model.Stock;
import Model.Exchange.Exchange;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;
Expand Down
105 changes: 101 additions & 4 deletions src/main/java/View/MainGameScene.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
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;
import Model.Player.PlayerStatus;
import Model.Purchase.Purchase;
import Model.Sale.Sale;
import Model.Sale.SaleCalculator;
import Model.Transaction.Transaction;
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 @@ -169,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 @@ -557,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
2 changes: 2 additions & 0 deletions src/main/java/View/StockTradingGameApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import Controller.StockFileHandler;
import Model.*;
import Model.Exchange.Exchange;
import Model.Player.Player;
import javafx.application.Application;
import javafx.stage.Stage;

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/ExchangeTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import static org.junit.jupiter.api.Assertions.*;

import Model.Exchange;
import Model.Player;
import Model.Purchase;
import Model.Sale;
import Model.Exchange.Exchange;
import Model.Player.Player;
import Model.Purchase.Purchase;
import Model.Sale.Sale;
import Model.Share;
import Model.Stock;
import Model.Transaction;
import Model.Transaction.Transaction;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/PlayerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import static org.junit.jupiter.api.Assertions.*;
import Model.Player.Player;

import Model.Player;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/PurchaseTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import static org.junit.jupiter.api.Assertions.*;

import Model.Player;
import Model.Purchase;
import Model.Player.Player;
import Model.Purchase.Purchase;
import Model.Share;
import Model.Stock;
import java.math.BigDecimal;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/SaleTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import static org.junit.jupiter.api.Assertions.*;

import Model.Player;
import Model.Sale;
import Model.Player.Player;
import Model.Sale.Sale;
import Model.Share;
import Model.Stock;
import java.math.BigDecimal;
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/StockTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

import Model.Stock;

import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Unit tests for the Stock class.
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/TransactionArchiveTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import static org.junit.jupiter.api.Assertions.*;

import Model.Player;
import Model.Purchase;
import Model.Sale;
import Model.Player.Player;
import Model.Purchase.Purchase;
import Model.Sale.Sale;
import Model.Share;
import Model.Stock;
import Model.TransactionArchive;
import Model.Transaction.TransactionArchive;
import java.math.BigDecimal;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Loading

0 comments on commit 2e14d49

Please sign in to comment.