Skip to content

Commit

Permalink
Updated MainGameScene class
Browse files Browse the repository at this point in the history
for observer
  • Loading branch information
elisab3 committed May 24, 2026
1 parent d0b7b3f commit b6c27e2
Showing 1 changed file with 70 additions and 74 deletions.
144 changes: 70 additions & 74 deletions src/main/java/View/MainGameScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
import java.math.BigDecimal;
import java.util.List;

public class MainGameScene {
/**
* Main game UI. Implements ExchangeObserver so the view automatically refreshes
* whenever the Exchange notifies it of a state change (week advance or trade).
*/
public class MainGameScene implements ExchangeObserver {

private Scene scene;
private Exchange exchange;
private Player player;
private Runnable onExit;
private Label statusLabel;
// References to UI components for refreshing

// References to UI components that need refreshing
private TableView<PortfolioRow> portfolioTable;
private ListView<Share> holdingsList;
private TableView<HistoryRow> historyTable;
Expand All @@ -29,9 +34,23 @@ public MainGameScene(Exchange exchange, Player player, Runnable onExit) {
this.exchange = exchange;
this.player = player;
this.onExit = onExit;

// Register this view as an observer of the exchange
this.exchange.addObserver(this);

this.scene = createScene();
}

/**
* Called automatically by Exchange whenever its state changes.
* Refreshes all UI elements to reflect the latest data.
*/
@Override
public void onExchangeUpdated(Exchange exchange) {
updateStatus();
refreshAllUI();
}

private Scene createScene() {
VBox root = new VBox(0);

Expand All @@ -49,12 +68,13 @@ private Scene createScene() {

Button nextWeekBtn = new Button("Next week");
nextWeekBtn.getStyleClass().addAll("action-button");
nextWeekBtn.setOnAction(e -> advance());
nextWeekBtn.setOnAction(e -> exchange.advance()); // observer handles the UI update

Button exitBtn = new Button("Exit");
exitBtn.getStyleClass().add("exit-button");
exitBtn.setOnAction(e -> {
if (confirm("Exit Game?", "Final Net Worth: $" + formatMoney(getNetWorth()))) {
exchange.removeObserver(this); // clean up before closing
onExit.run();
}
});
Expand Down Expand Up @@ -156,11 +176,9 @@ private VBox createPortfolioPanel() {
alert("Error", "Select a holding to sell.");
return;
}
Transaction trans = exchange.sell(selected.s, player);
Transaction trans = exchange.sell(selected.s, player); // observer fires refresh
if (trans != null && trans.isCommitted()) {
showConfirmation("Sale successful", trans);
updateStatus();
refreshAllUI();
} else {
alert("Failed", "Could not complete the sale.");
}
Expand Down Expand Up @@ -226,14 +244,12 @@ private VBox createBuyTab() {
return;
}
BigDecimal qty = new BigDecimal(qtyField.getText());
Transaction trans = exchange.buy(s.getSymbol(), qty, player);
Transaction trans = exchange.buy(s.getSymbol(), qty, player); // observer fires refresh
if (trans != null && trans.isCommitted()) {
showConfirmation("Purchase successful", trans);
stockField.clear();
qtyField.clear();
infoLabel.setText("");
updateStatus();
refreshAllUI();
} else {
alert("Failed", "Insufficient funds or error");
}
Expand All @@ -247,62 +263,50 @@ private VBox createBuyTab() {
}

private VBox createSellTab() {
VBox box = new VBox(10);
box.getStyleClass().add("content-area");

Label heading = new Label("Your Holdings:");
VBox box = new VBox(10);
box.getStyleClass().add("content-area");

holdingsList = new ListView<>();
holdingsList.setPrefHeight(400);
Label heading = new Label("Your Holdings:");

holdingsList.setCellFactory(lv -> new ListCell<Share>() {
@Override
protected void updateItem(Share s, boolean empty) {
super.updateItem(s, empty);
holdingsList = new ListView<>();
holdingsList.setPrefHeight(400);

if (empty || s == null) {
setText(null);
} else {
setText(
s.getStock().getSymbol() + " - " +
s.getQuantity() + " @ $" +
formatMoney(s.getStock().getSalesPrice())
);
holdingsList.setCellFactory(lv -> new ListCell<Share>() {
@Override
protected void updateItem(Share s, boolean empty) {
super.updateItem(s, empty);
if (empty || s == null) {
setText(null);
} else {
setText(
s.getStock().getSymbol() + " - " +
s.getQuantity() + " @ $" +
formatMoney(s.getStock().getSalesPrice())
);
}
}
}
});

updateHoldingsList(holdingsList);

Button sellBtn = new Button("Sell Selected");

sellBtn.setOnAction(e -> {
Share selected = holdingsList.getSelectionModel().getSelectedItem();

if (selected == null) {
alert("Error", "Select a holding to sell");
return;
}

Transaction trans = exchange.sell(selected, player);

if (trans != null && trans.isCommitted()) {
showConfirmation("Sale successful", trans);
refreshAllUI();
} else {
alert("Failed", "Could not complete sale");
}
});
});

box.getChildren().addAll(
heading,
holdingsList,
sellBtn
);
updateHoldingsList(holdingsList);

return box;
}
Button sellBtn = new Button("Sell Selected");
sellBtn.setOnAction(e -> {
Share selected = holdingsList.getSelectionModel().getSelectedItem();
if (selected == null) {
alert("Error", "Select a holding to sell");
return;
}
Transaction trans = exchange.sell(selected, player); // observer fires refresh
if (trans != null && trans.isCommitted()) {
showConfirmation("Sale successful", trans);
} else {
alert("Failed", "Could not complete sale");
}
});

box.getChildren().addAll(heading, holdingsList, sellBtn);
return box;
}

private VBox createHistoryPanel() {
VBox panel = new VBox(10);
Expand All @@ -317,7 +321,8 @@ private VBox createHistoryPanel() {

weekFilterCombo.setOnAction(e -> updateHistory(
historyTable,
weekFilterCombo.getValue() == null || weekFilterCombo.getValue() == 0 ? null : weekFilterCombo.getValue()
weekFilterCombo.getValue() == null || weekFilterCombo.getValue() == 0
? null : weekFilterCombo.getValue()
));

HBox filterRow = new HBox(8);
Expand Down Expand Up @@ -394,13 +399,8 @@ private void updatePortfolio(TableView<PortfolioRow> table) {
}

private void updateHoldingsList(ListView<Share> list) {
ObservableList<Share> items =
FXCollections.observableArrayList();

items.addAll(
player.getPortfolio().getShares()
);

ObservableList<Share> items = FXCollections.observableArrayList();
items.addAll(player.getPortfolio().getShares());
list.setItems(items);
}

Expand Down Expand Up @@ -457,19 +457,15 @@ private void refreshAllUI() {
updateHoldingsList(holdingsList);
}
if (historyTable != null) {
updateHistory(historyTable, weekFilterCombo != null && weekFilterCombo.getValue() != null ? weekFilterCombo.getValue() : null);
updateHistory(historyTable,
weekFilterCombo != null && weekFilterCombo.getValue() != null
? weekFilterCombo.getValue() : null);
if (weekFilterCombo != null) {
updateWeekCombo(weekFilterCombo);
}
}
}

private void advance() {
exchange.advance();
updateStatus();
refreshAllUI();
}

private BigDecimal getNetWorth() {
return player.getMoney().add(player.getPortfolio().getNetWorth());
}
Expand Down

0 comments on commit b6c27e2

Please sign in to comment.