-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from Team-40-IDATT2003/enhancement/95-impleme…
…nt-market-page markedet
- Loading branch information
Showing
8 changed files
with
739 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/market/MarketActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
95 changes: 95 additions & 0 deletions
95
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/market/MarketController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/market/MarketSort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Oops, something went wrong.