Skip to content

Commit

Permalink
implement basic structure of stock app with search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
einaskoi committed Apr 17, 2026
1 parent 1f0861d commit 8ebbfee
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppController;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popups.StockPopup;

public class StockAppController {
private MarketController marketController;

public StockAppController(StockPopup stockPopup) {
marketController = new MarketController();

stockPopup.getStockList().getItems().addAll(marketController.getMarket());

stockPopup.getSearchField().textProperty().addListener((obs, old, newValue) -> {
stockPopup.getStockList().getItems().setAll(marketController.getMarket(newValue));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Controller for the app buttons.
* Handles app button creation, resizing, click events, and drag-and-drop.
*/
public class AppController {
public class DesktopViewController {
private final PopupController popupController;
private final Pane parent;

Expand All @@ -31,7 +31,7 @@ public class AppController {
* @param popupController the popup controller to manage popups.
* @param parent the parent pane to add popups to.
*/
public AppController(PopupController popupController, Pane parent) {
public DesktopViewController(PopupController popupController, Pane parent) {
this.popupController = popupController;
this.parent = parent;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller;

import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;
import edu.ntnu.idi.idatt2003.gruppe42.Model.StockFileHandler;

import java.nio.file.Path;
import java.util.List;

public class MarketController {

private List<Stock> market;

public MarketController() {
try {
Path path = Path.of(getClass().getClassLoader().getResource("stocks.csv").toURI());
market = StockFileHandler.readFromFile(path);
} catch (Exception e) {
System.out.println("File not found");
}
}

public List<Stock> getMarket() {
return market;
}

public List<Stock> getMarket(String searchTerm) {
return market.stream().filter(stock -> stock.getCompany().toLowerCase().contains(searchTerm.toLowerCase())).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ public PopupController() {
* @return true if added, false otherwise
*/
public boolean add(Popup popup) {
System.out.print(popup.toString());
if (popup == null) {
return false;
}else if (popups.stream().anyMatch(oldPopup -> oldPopup.getType().equals(popup.getType()))) {
} else if (popups.stream().anyMatch(oldPopup -> oldPopup.getType().equals(popup.getType()))) {
return false;
}
popup.getCloseButton().setOnAction(event -> remove(popup));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package edu.ntnu.idi.idatt2003.gruppe42.View;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.DesktopViewController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
Expand All @@ -19,15 +18,15 @@
public class DesktopView {

private final PopupController popupController;
private final AppController appController;
private final DesktopViewController appController;
private final BorderPane root;
private static final int COLS = 6;
private static final int ROWS = 4;

public DesktopView() {
this.popupController = new PopupController();
this.root = new BorderPane();
this.appController = new AppController(popupController, root);
this.appController = new DesktopViewController(popupController, root);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.StockController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppController.StockAppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;
import javafx.geometry.Pos;
Expand All @@ -12,7 +13,9 @@
*/
public class StockPopup extends Popup {

private StockController stockController;
private StockAppController stockAppController;
private ListView<Stock> stockList;
private TextField searchField;

/**
* Constructs a new Stock popup.
Expand All @@ -24,30 +27,34 @@ public class StockPopup extends Popup {
*/
public StockPopup(int width, int height, int x, int y) {
super(width, height, x, y, App.STOCK);
stockController = new StockController();
System.out.println("Market size: " + stockController.getMarket().size());

TextField searchField = new TextField();
searchField = new TextField();
stockList = new ListView<>();

ListView<Stock> resultList = new ListView<>();
resultList.getItems().addAll(stockController.getMarket());

resultList.setCellFactory(lv -> new ListCell<Stock>() {
stockList.setCellFactory(lv -> new ListCell<Stock>() {
@Override
protected void updateItem(Stock stock, boolean empty) {
super.updateItem(stock, empty);
if (empty || stock == null) {
setGraphic(null);
} else {
Label nameLabel = new Label(stock.getCompany());

HBox row = new HBox(10, nameLabel);
HBox row = new HBox(10, new Label(stock.getCompany()));
row.setAlignment(Pos.CENTER_LEFT);
setGraphic(row);
}
}
});

content.getChildren().addAll(searchField, resultList);
new StockAppController(this);

content.getChildren().addAll(searchField, stockList);
}

public TextField getSearchField() {
return searchField;
}

public ListView<Stock> getStockList() {
return stockList;
}
}
10 changes: 5 additions & 5 deletions src/main/resources/stocks.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ BAN,Banana Inc.,191.27
APL,Apple Inc.,276.43
TIR,Tire Inc.,404.68
OIL,Oil Inc.,204.62
SPC,Space Inc. (Class A),311.20
WND,Turbine Inc. (Class C),311.62
SPC,Space Org.,311.20
WND,Turbine Inc.,311.62
NUC,Nuclear Inc.,343.35
CAR,Car Inc.,426.52
HYD,Hydro,501.05
HYD,Hydro Inc.,501.05
TIP,Q-tip Inc.,128.75
TED,Teddy Bear Inc.,1014.43
UNI,NTNU,311.14
COM,Communication Inc.,155.28
UNI,NTNU Org.,311.14
COM,Communication Org.,155.28
MON,Monsters Inc.,329.54
AIR,Plane Inc.,240.70
ATB,Bus Inc.,539.52

0 comments on commit 8ebbfee

Please sign in to comment.