Skip to content

Commit

Permalink
Feat: Name label and amount owned
Browse files Browse the repository at this point in the history
Stock name now updates when selecting a new stock. In addition, amount of shares owned is updated when buying and changing stocks
  • Loading branch information
tommyah committed May 15, 2026
1 parent 258fbaf commit 9e557d6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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());
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public class DashBoardView extends ViewElement<HBox, DashBoardActions> {
private XYChart.Series<Number, Number> 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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -262,16 +274,15 @@ 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) {
changeSinceLastString = "(- " + Math.round(Math.abs(changePercent)*100f)/100f + "%)";
} else {
changeSinceLastString = "";
}

selectedStockPriceLabel.setText(Float.toString(Math.round(value*100f)/100f) + "NOK " + changeSinceLastString);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 9e557d6

Please sign in to comment.