Skip to content

markedet #103

Merged
merged 1 commit into from
May 15, 2026
Merged
Show file tree
Hide file tree
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
24 changes: 21 additions & 3 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.financialsummary.SummaryView;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.dashboard.DashBoardController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.dashboard.DashBoardView;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.market.MarketController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.market.MarketView;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.topbar.TopBarController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.topbar.TopBarView;
import javafx.application.Application;
Expand Down Expand Up @@ -92,23 +94,39 @@ public void start(final Stage stage) throws Exception {

// Top bar with summary section
TopBarView topBarView = new TopBarView(summaryView);
new TopBarController(topBarView, eventManager);
TopBarController topBarController = new TopBarController(topBarView, eventManager);

// Top bar without summary section
TopBarView topBarView2 = new TopBarView();
new TopBarController(topBarView2, eventManager);

// Stats page
// Stats page (dashboard, default center-view in-game)
DashBoardView dashBoardView = new DashBoardView();
new DashBoardController(dashBoardView,
eventManager,
player,
exchange,
stocksInFile);

// Market page (vises i samme InGameView, byttes til via Market-knappen)
MarketView marketView = new MarketView();
new MarketController(marketView,
eventManager,
player,
exchange,
stocksInFile);

// In-game (Change "topBarView" to "topBarView2" if no summary section).
// Dashboard er default center-view.
InGameView inGameView = new InGameView(topBarView, dashBoardView.getRootPane());

// Wire top bar buttons til å bytte mellom dashboard og market.
topBarController.setMarketIntegration(
inGameView::changeCenterView,
dashBoardView.getRootPane(),
marketView.getRootPane()
);

// Register all views
viewManager.addView(mainMenuView);
viewManager.addView(playGameView);
Expand All @@ -118,4 +136,4 @@ public void start(final Stage stage) throws Exception {

stage.show();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.market;

/**
* Enum representing all interactable actions in {@link MarketView}.
*
* <p>The market is embedded as the center-view inside
* {@link edu.ntnu.idi.idatt2003.g40.mappe.view.ingame.InGameView}, so
* navigation (close, settings, etc.) is handled by the shared top bar.
* Only the filter-bar sort buttons are local to this widget.</p>
* */
public enum MarketActions {
/** Sort stocks alphabetically by ticker. */
SORT_TICKER,
/** Sort stocks by current price (descending). */
SORT_PRICE,
/** Sort stocks by latest change (descending). */
SORT_CHANGE,
/** Sort stocks by amount owned (descending). */
SORT_OWNED;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.market;

import edu.ntnu.idi.idatt2003.g40.mappe.engine.Exchange;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Player;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Share;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager;
import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewController;

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

/**
* Controller for {@link MarketView}.
*
* <p>Wires up the sort buttons and listens to {@link Exchange} weeks and
* {@link Player} net worth so the grid (prices, change %, owned counts)
* stays in sync as the game advances.</p>
* */
public class MarketController extends ViewController<MarketView> {

/** The {@link Player} owning shares displayed in the market. */
private final Player player;

/** The {@link Exchange} the market is connected to. */
private final Exchange exchange;

/** Stocks shown in the market grid. */
private final List<Stock> stockList;

/**
* Constructor.
*
* @param viewElement the {@link MarketView} this controller is attached to.
* @param eventManager the active {@link EventManager}.
* @param player the {@link Player} whose ownership is displayed.
* @param exchange the {@link Exchange} the market connects to.
* @param stockList the stocks to display.
*
* @throws IllegalArgumentException if any argument is invalid.
* */
public MarketController(final MarketView viewElement,
final EventManager eventManager,
final Player player,
final Exchange exchange,
final List<Stock> stockList)
throws IllegalArgumentException {
this.player = player;
this.exchange = exchange;
this.stockList = stockList;
super(viewElement, eventManager);
}

/** {@inheritDoc} */
@Override
protected void initInteractions() {
getViewElement().setOwnedLookup(this::ownedQuantity);
getViewElement().setStocks(stockList);

getViewElement().setOnAction(MarketActions.SORT_TICKER,
() -> getViewElement().setSort(MarketSort.TICKER));
getViewElement().setOnAction(MarketActions.SORT_PRICE,
() -> getViewElement().setSort(MarketSort.PRICE));
getViewElement().setOnAction(MarketActions.SORT_CHANGE,
() -> getViewElement().setSort(MarketSort.CHANGE));
getViewElement().setOnAction(MarketActions.SORT_OWNED,
() -> getViewElement().setSort(MarketSort.OWNED));

exchange.weekProperty().addListener((observable, o, n) -> {
getViewElement().renderStocks();
});

player.getNetWorthAsFloatProperty().addListener((observable, o, n) -> {
getViewElement().renderStocks();
});
}

/**
* Returns the total quantity of shares the player owns of a given symbol.
*
* @param symbol the stock symbol.
*
* @return the total quantity, or 0 if the symbol is invalid.
* */
private int ownedQuantity(final String symbol) {
if (!Validator.VALID_STOCK_NAME.isValid(symbol)) {
return 0;
}
return player.getPortfolio().getShares(symbol).stream()
.map(Share::getQuantity)
.reduce(BigDecimal.ZERO, BigDecimal::add)
.intValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.market;

/**
* Enum representing the available sorting modes in {@link MarketView}.
* */
public enum MarketSort {
/** Alphabetical sort by ticker symbol. */
TICKER,
/** Descending sort by current sales price. */
PRICE,
/** Descending sort by latest change percentage. */
CHANGE,
/** Descending sort by amount of shares the player owns. */
OWNED;
}
Loading