Skip to content

Commit

Permalink
Added a button to redirect to stock market page from portfolio tab
Browse files Browse the repository at this point in the history
Added GameControllerTest
  • Loading branch information
Nikollai committed May 25, 2026
1 parent 9d0cbfd commit d3a095f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/main/java/millions/view/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ public class GameView extends BorderPane implements PlayerListener, ExchangeList
private final Button buyButton = new Button("Buy");
private final Button sellButton = new Button("Sell");
private final Button advanceButton = new Button("Advance week");
private final TabPane tabPane;
private boolean updatingQuantityControls;

public GameView(GameController controller) {
this.controller = controller;
this.tabPane = createTabs();
setTop(createHeader());
setCenter(createTabs());
setCenter(tabPane);
configureStocksList();
configureButtons();
configureQuantityControls();
Expand Down Expand Up @@ -169,11 +171,34 @@ protected void updateItem(String profit, boolean empty) {
setStyle(profit.startsWith("-") ? "-fx-text-fill: red;" : "-fx-text-fill: green;");
}
});

TableColumn<Share, Share> marketColumn = new TableColumn<>("Market");
marketColumn.setMinWidth(100);
marketColumn.setCellFactory(column -> {
return new TableCell<Share, Share>() {
private final Button button = new Button("Market Page");

@Override
protected void updateItem(Share share, boolean empty) {
super.updateItem(share, empty);

if (empty) {
setGraphic(null);
} else {
button.setOnAction(event -> {
Share s = getTableView().getItems().get(getIndex());
getTabPane().getSelectionModel().select(0);
stocksList.getSelectionModel().select(s.getStock());
showStockChart(s.getStock());
});
setGraphic(button);
}
}
};
});
portfolioTable
.getColumns()
.addAll(
symbolColumn, quantityColumn, purchasePriceColumn, currentPriceColumn, profitColumn);
symbolColumn, quantityColumn, purchasePriceColumn, currentPriceColumn, profitColumn, marketColumn);
return new Tab("Portfolio", portfolioTable);
}

Expand Down Expand Up @@ -457,6 +482,9 @@ private int getCurrentMaxBuyable() {
return Math.max(1, controller.getMaxBuyableQuantity(selected.getSymbol()));
}

public TabPane getTabPane() {
return this.tabPane;
}
private String formatStock(Stock stock) {
return ViewUtils.formatStock(stock);
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/millions/controller/GameControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package millions.controller;

import millions.controller.fileIO.UncheckedFileNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class GameControllerTest {
GameController gameController;
@BeforeEach
public void setUpController() {
this.gameController = new GameController();
}

@Test
public void startGameFileNotFoundTest() {
String name = "name";
BigDecimal startingMoney = new BigDecimal("100");
Path stockFilePath = Path.of("nonexistantfile");
int prerunWeeks = 1;
assertThrows(UncheckedFileNotFoundException.class, () -> {gameController.startGame(name,startingMoney,stockFilePath,prerunWeeks);});
}
}

0 comments on commit d3a095f

Please sign in to comment.