Skip to content

Commit

Permalink
Feat: Implemented search functionality
Browse files Browse the repository at this point in the history
Implemented functionality for searching for stocks in the dashboard view
  • Loading branch information
tommyah committed May 15, 2026
1 parent 7c421e1 commit 3f394dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javafx.scene.control.TextFormatter;

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

Expand Down Expand Up @@ -38,22 +39,29 @@ private void handleStockSelection(final Stock stock, final float amountOwned) {
getViewElement().updateGraph();
}

private void populateStockList() {
private void populateStockList(String filter) {
getViewElement().clearStockList();
String search = filter.toLowerCase();
for (Stock s : stockList) {
Button stockBtn = getViewElement().createStockButton(s.getSymbol());
boolean matches = s.getSymbol().toLowerCase().contains(search) ||
s.getCompany().toLowerCase().contains(search);


getViewElement().setOnStockAction(stockBtn, s, (stock)-> {
BigDecimal amountOfSharesOwned = player.getPortfolio().getTotalSharesBySymbol(s.getSymbol());
handleStockSelection(stock, amountOfSharesOwned.floatValue());
});
if (matches) {
Button stockBtn = getViewElement().createStockButton(s.getSymbol());

getViewElement().setOnStockAction(stockBtn, s, (Stock stock) -> {
BigDecimal amountOfSharesOwned = player.getPortfolio()
.getTotalSharesBySymbol(s.getSymbol());
handleStockSelection(stock, amountOfSharesOwned.floatValue());
});
}
}
}

@Override
protected void initInteractions() {
populateStockList();
populateStockList("");
getViewElement().setCurrentStock(stockList.getFirst(), 0);
getViewElement().updateGraph();
getViewElement().setOnAction(DashBoardActions.BUY_SHARES, () -> {
Expand Down Expand Up @@ -116,5 +124,9 @@ protected void initInteractions() {
}
return null;
}));

getViewElement().getSideBarSearchField().textProperty().addListener((observable, o, n) -> {
populateStockList(n);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void initLayout() {
yAxis.setTickMarkVisible(true);
yAxis.setMinorTickVisible(false);
yAxis.setAutoRanging(false);
yAxis.setLabel("Price ($)");
yAxis.setLabel("Price (NOK)");

chart = new LineChart<>(xAxis, yAxis);
chart.setCreateSymbols(false);
Expand Down Expand Up @@ -312,7 +312,9 @@ private void setHighPriceLabel(float value) {
highPriceLabel.setText("High: " + Math.round(value*100f)/100f + " NOK");
}

//public void update
public TextField getSideBarSearchField() {
return sideBarSearchField;
}

public Stock getCurrentStock() {
return selectedStock;
Expand Down

0 comments on commit 3f394dc

Please sign in to comment.