-
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.
adds purchase and sale functionality with proper handling
- Loading branch information
Showing
12 changed files
with
281 additions
and
90 deletions.
There are no files selected for viewing
91 changes: 84 additions & 7 deletions
91
...in/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/StockAppController.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 |
|---|---|---|
| @@ -1,31 +1,108 @@ | ||
| package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Share; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.Transaction; | ||
| import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp; | ||
| import java.math.BigDecimal; | ||
|
|
||
| public class StockAppController implements AppController { | ||
| private MarketController marketController; | ||
| private StockApp stockPopup; | ||
| private Stock currentStock; | ||
|
|
||
| public StockAppController(StockApp stockPopup, MarketController marketController) { | ||
| public StockAppController(StockApp stockPopup, MarketController marketController, Player player) { | ||
| this.stockPopup = stockPopup; | ||
| this.marketController = marketController; | ||
|
|
||
| stockPopup.getStockList().getItems().addAll(marketController.getMarket()); | ||
|
|
||
| stockPopup.getSearchField().textProperty().addListener((obs, old, newValue) -> { | ||
| stockPopup.getStockList().getItems().addAll(marketController.getExchange().getAllStocks()); | ||
|
|
||
| stockPopup.getSearchField().textProperty().addListener(( | ||
| obs, old, newValue) -> { | ||
| if (stockPopup.getCurrentStock() != null) { | ||
| stockPopup.openSearchPage(); | ||
| } | ||
| stockPopup.getStockList().getItems().setAll(marketController.getMarket(newValue)); | ||
| }); | ||
|
|
||
| stockPopup.getBuyButton().setOnMouseClicked(event -> { | ||
| currentStock = stockPopup.getCurrentStock(); | ||
| if (currentStock == null) { | ||
| return; | ||
| } | ||
|
|
||
| String quantityText = stockPopup.getQuantityField().getText(); | ||
| BigDecimal quantity; | ||
| try { | ||
| quantity = new BigDecimal(quantityText); | ||
| if (quantity.compareTo(BigDecimal.ZERO) <= 0) { | ||
| return; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| return; | ||
| } | ||
|
|
||
| Transaction transaction = marketController.getExchange().buy( | ||
| currentStock.getSymbol(), quantity, player | ||
| ); | ||
|
|
||
| if (transaction == null) { | ||
| return; | ||
| } | ||
|
|
||
| player.getTransactionArchive().add(transaction); | ||
| player.getPortfolio().addShare(transaction.getShare()); | ||
| }); | ||
|
|
||
| stockPopup.getSellButton().setOnMouseClicked(event -> { | ||
| currentStock = stockPopup.getCurrentStock(); | ||
| if (currentStock == null) { | ||
| return; | ||
| } | ||
|
|
||
| String quantityText = stockPopup.getQuantityField().getText(); | ||
| BigDecimal quantityToSell; | ||
| try { | ||
| quantityToSell = new BigDecimal(quantityText); | ||
| if (quantityToSell.compareTo(BigDecimal.ZERO) <= 0) { | ||
| return; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| return; | ||
| } | ||
|
|
||
| player.getPortfolio().getShares().stream() | ||
| .filter(s -> s.getStock().getSymbol().equals(currentStock.getSymbol())) | ||
| .findFirst() | ||
| .ifPresent(existingShare -> { | ||
| if (existingShare.getQuantity().compareTo(quantityToSell) < 0) { | ||
| return; | ||
| } | ||
|
|
||
| Transaction transaction = marketController.getExchange().sell(existingShare, quantityToSell, player); | ||
| player.getTransactionArchive().add(transaction); | ||
| player.getPortfolio().removeShare(transaction.getShare()); | ||
| }); | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Updates the app state on each simulation tick. | ||
| * Refreshes the market data and updates the price label for the currently viewed stock. | ||
| */ | ||
| @Override | ||
| public void nextTick(){ | ||
| public void nextTick() { | ||
| System.out.println("[DEBUG] StockAppController.nextTick() - Updating Market"); | ||
| marketController.updateMarket(); | ||
| if (stockPopup.getCurrentStock() != null) { | ||
| System.out.println("[DEBUG] Updating Price Label for: " + stockPopup.getCurrentStock().getCompany()); | ||
| stockPopup.updatePriceLabel(stockPopup.getCurrentStock()); | ||
| Stock currentStockInView = stockPopup.getCurrentStock(); | ||
| if (currentStockInView != null) { | ||
| System.out.println("[DEBUG] Updating Price Label for: " | ||
| + currentStockInView.getCompany()); | ||
| stockPopup.updatePriceLabel(currentStockInView); | ||
| } | ||
| } | ||
| } |
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
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
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
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
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
Oops, something went wrong.