Skip to content

Commit

Permalink
Merge pull request #82 from einaskoi/einar/stockApp
Browse files Browse the repository at this point in the history
Einar/stock app
  • Loading branch information
peretr authored Apr 17, 2026
2 parents 806e51e + 8ebbfee commit bc7155b
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 7 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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;
Expand All @@ -18,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,12 +1,22 @@
package edu.ntnu.idi.idatt2003.gruppe42.View.Popups;

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;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;

/**
* A popup for the Stock app.
*/
public class StockPopup extends Popup {

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

/**
* Constructs a new Stock popup.
*
Expand All @@ -17,6 +27,34 @@ public class StockPopup extends Popup {
*/
public StockPopup(int width, int height, int x, int y) {
super(width, height, x, y, App.STOCK);
// Add Stock specific content here

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

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 {
HBox row = new HBox(10, new Label(stock.getCompany()));
row.setAlignment(Pos.CENTER_LEFT);
setGraphic(row);
}
}
});

new StockAppController(this);

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

public TextField getSearchField() {
return searchField;
}

public ListView<Stock> getStockList() {
return stockList;
}
}
19 changes: 19 additions & 0 deletions src/main/resources/stocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Companies by Market Cap
# Ticker,Name,Price

BAN,Banana Inc.,191.27
APL,Apple Inc.,276.43
TIR,Tire Inc.,404.68
OIL,Oil Inc.,204.62
SPC,Space Org.,311.20
WND,Turbine Inc.,311.62
NUC,Nuclear Inc.,343.35
CAR,Car Inc.,426.52
HYD,Hydro Inc.,501.05
TIP,Q-tip Inc.,128.75
TED,Teddy Bear Inc.,1014.43
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 bc7155b

Please sign in to comment.