From 9e557d6b3077bbf1d1f61416d123d3bc517e49b0 Mon Sep 17 00:00:00 2001 From: = Date: Fri, 15 May 2026 11:10:17 +0200 Subject: [PATCH] Feat: Name label and amount owned Stock name now updates when selecting a new stock. In addition, amount of shares owned is updated when buying and changing stocks --- .../dashboard/DashBoardController.java | 24 ++++++++++++------ .../view/widgets/dashboard/DashBoardView.java | 25 ++++++++++++++----- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardController.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardController.java index b2d94f7..6aa9f86 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardController.java @@ -1,8 +1,7 @@ package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.dashboard; 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.Stock; +import edu.ntnu.idi.idatt2003.g40.mappe.model.*; 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; @@ -33,8 +32,8 @@ public DashBoardController(final DashBoardView viewElement, super(viewElement, eventManager); } - private void handleStockSelection(Stock stock) { - getViewElement().setCurrentStock(stock); + private void handleStockSelection(final Stock stock, final float amountOwned) { + getViewElement().setCurrentStock(stock, amountOwned); getViewElement().updateGraph(); } @@ -43,24 +42,33 @@ private void populateStockList() { for (Stock s : stockList) { Button stockBtn = getViewElement().createStockButton(s.getSymbol()); - getViewElement().setOnStockAction(stockBtn, s, this::handleStockSelection); + + getViewElement().setOnStockAction(stockBtn, s, (stock)-> { + BigDecimal amountOfSharesOwnedThisStock = player.getPortfolio().getShares().stream() + .filter(share -> share.getStock().equals(s)) + .map(Share::getQuantity) + .reduce(BigDecimal.ZERO, BigDecimal::add); + handleStockSelection(stock, amountOfSharesOwnedThisStock.floatValue()); + }); } } - @Override protected void initInteractions() { populateStockList(); - getViewElement().setCurrentStock(stockList.getFirst()); + getViewElement().setCurrentStock(stockList.getFirst(), 0); getViewElement().updateGraph(); getViewElement().setOnAction(DashBoardActions.BUY_SHARES, () -> { if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())) { BigDecimal amountToBuy = new BigDecimal(getViewElement().getQuantityInputField().getText()); - exchange.buy( + Transaction purchase = exchange.buy( getViewElement().getCurrentStock().getSymbol(), amountToBuy, player ); + if (purchase.isCommited()) { + getViewElement().addOwnedShares(purchase.getShare().getQuantity().floatValue()); + } } }); diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardView.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardView.java index 1564f91..165f44b 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardView.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/widgets/dashboard/DashBoardView.java @@ -26,7 +26,10 @@ public class DashBoardView extends ViewElement { private XYChart.Series dataSeries; private VBox sidebar; private Stock selectedStock; + private float ownedStocks = 0; private Label selectedStockLabel; + private Label stockFullNameLabel; + private Label ownedQuantityLabel; private NumberAxis xAxis; private NumberAxis yAxis; private Label selectedStockPriceLabel; @@ -116,8 +119,9 @@ protected void initLayout() { VBox stockIdentity = new VBox(5); selectedStockLabel = new Label("APL"); selectedStockLabel.getStyleClass().add("stock-symbol-title"); - Label stockFullNameLabel = new Label("Apple corporation"); - Label ownedQuantityLabel = new Label("Owned: 0"); + stockFullNameLabel = new Label("Apple corporation"); + ownedQuantityLabel = new Label("Owned: 0"); + stockIdentity.getChildren().addAll(selectedStockLabel, stockFullNameLabel, ownedQuantityLabel); // Price Stats @@ -213,9 +217,17 @@ protected void initStyling() { p5QtyBtn.getStyleClass().add("qtyBtn"); } - public void setCurrentStock(final Stock stock) { + public void setCurrentStock(final Stock stock, final float amountOwned) { selectedStock = stock; selectedStockLabel.setText(stock.getSymbol()); + stockFullNameLabel.setText(stock.getCompany()); + ownedStocks = amountOwned; + ownedQuantityLabel.setText("Owned: " + ownedStocks); + } + + public void addOwnedShares(final float val) { + ownedStocks += val; + ownedQuantityLabel.setText("Owned: " + ownedStocks); } public Button createStockButton(final String label) { @@ -262,8 +274,7 @@ public void updateGraph() { private void updateStockPrice(final float value, final float changeSinceLast) { String changeSinceLastString; - float oldVal = value - changeSinceLast; - float changePercent = (value/oldVal - 1) * 100; + float changePercent = (value / (value - changeSinceLast) - 1) * 100; if (changeSinceLast > 0) { changeSinceLastString = "(+ " + Math.round(changePercent*100f)/100f + "%)"; } else if (changeSinceLast < 0) { @@ -271,7 +282,7 @@ private void updateStockPrice(final float value, } else { changeSinceLastString = ""; } - + selectedStockPriceLabel.setText(Float.toString(Math.round(value*100f)/100f) + "NOK " + changeSinceLastString); } @@ -283,6 +294,8 @@ private void setHighPriceLabel(float value) { highPriceLabel.setText("High: " + Math.round(value*100f)/100f + " NOK"); } + //public void update + public Stock getCurrentStock() { return selectedStock; }