From 3d0ed35383f40217554abcf65f127847bd060e59 Mon Sep 17 00:00:00 2001 From: Per Eric Trapnes Date: Wed, 22 Apr 2026 15:07:14 +0200 Subject: [PATCH] refactored how we build popups and created a new page within the stockapp --- .../AppControllers/StockAppController.java | 1 + .../gruppe42/Controller/MarketController.java | 32 ++++++++++ .../gruppe42/Controller/PopupController.java | 51 ++++++++-------- .../DesktopViewController.java | 14 +---- .../idi/idatt2003/gruppe42/Model/Stock.java | 5 ++ .../gruppe42/View/Apps/StockApp.java | 61 ++++++++++++++----- .../idi/idatt2003/gruppe42/View/Popup.java | 55 +++++++++-------- 7 files changed, 139 insertions(+), 80 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/StockAppController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/StockAppController.java index 297a3c3..485ca0e 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/StockAppController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/StockAppController.java @@ -14,5 +14,6 @@ public StockAppController(StockApp stockPopup) { stockPopup.getSearchField().textProperty().addListener((obs, old, newValue) -> { stockPopup.getStockList().getItems().setAll(marketController.getMarket(newValue)); }); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/MarketController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/MarketController.java index 72b7d38..7f88a29 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/MarketController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/MarketController.java @@ -5,15 +5,22 @@ import java.nio.file.Path; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; public class MarketController { private List market; + private int stockResolution; public MarketController() { try { Path path = Path.of(getClass().getClassLoader().getResource("stocks.csv").toURI()); market = StockFileHandler.readFromFile(path); + + //startMarket(); + System.out.println("Market loaded"); + } catch (Exception e) { System.out.println("File not found"); } @@ -26,4 +33,29 @@ public List getMarket() { public List getMarket(String searchTerm) { return market.stream().filter(stock -> stock.getCompany().toLowerCase().contains(searchTerm.toLowerCase())).toList(); } + public void updateMarket(){ + for (Stock stock : market){ + stock.updatePrice(); + } + } + + public void startMarket(){ + Timer timer = new Timer(); + + for (int i = 0; i < stockResolution; i++){ + updateMarket(); + } + + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + updateMarket(); + } + }, 0, 1000); + } + public static void printMarket(List stocks){ + for (Stock stock : stocks){ + System.out.println(stock.getHistoricalPrices().toString()); + } + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java index 0083dc5..966719a 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/PopupController.java @@ -1,5 +1,12 @@ package edu.ntnu.idi.idatt2003.gruppe42.Controller; +import edu.ntnu.idi.idatt2003.gruppe42.Model.App; +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.AppStoreApp; +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.BankApp; +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.HustlersApp; +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.MailApp; +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.NewsApp; +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp; import edu.ntnu.idi.idatt2003.gruppe42.View.Popup; import javafx.scene.layout.Pane; import java.util.ArrayList; @@ -18,41 +25,29 @@ public class PopupController { */ public PopupController() { popups = new ArrayList<>(); + popups.add(new AppStoreApp(400, 300, 100, 100)); + popups.add(new HustlersApp(400, 300, 140, 140)); + popups.add(new StockApp(400, 300, 200, 200)); + popups.add(new MailApp(400, 300, 160, 160)); + popups.add(new NewsApp(400, 300, 180, 180)); + popups.add(new BankApp(400, 300, 120, 120)); } + //TODO: javadoc /** * Adds a popup to the controller and sets up its events. * - * @param popup the popup to add + * @param type of popup to add * @return true if added, false otherwise */ - public boolean add(Popup popup) { - System.out.print(popup.toString()); - if (popup == null) { + public boolean show(App type) { + if (type == null) { return false; - } else if (popups.stream().anyMatch(oldPopup -> oldPopup.getType().equals(popup.getType()))) { - return false; - } - popup.getCloseButton().setOnAction(event -> remove(popup)); - popup.getRoot().setOnMousePressed(event -> bringToFront(popup)); - popup.getContent().setOnMousePressed(event -> bringToFront(popup)); - return popups.add(popup); - } - - /** - * Removes a popup from its parent and the controller. - * - * @param popup the popup to remove - * @return true if removed, false otherwise - */ - public boolean remove(Popup popup) { - if (popup == null || !popups.contains(popup)) { - return false; - } - if (popup.getRoot().getParent() instanceof Pane parent) { - parent.getChildren().remove(popup.getRoot()); } - return popups.remove(popup); + popups.stream().filter( + popup -> popup.getType().equals(type)).findFirst().ifPresent( + popup -> {popup.getRoot().setVisible(true); bringToFront(popup);}); + return true; } /** @@ -65,4 +60,8 @@ public void bringToFront(Popup popup) { popup.getRoot().toFront(); } } + + public List getPopups() { + return popups; + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/DesktopViewController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/DesktopViewController.java index f15927f..4ca10e5 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/DesktopViewController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/DesktopViewController.java @@ -35,6 +35,7 @@ public class DesktopViewController { public DesktopViewController(PopupController popupController, Pane parent) { this.popupController = popupController; this.parent = parent; + parent.getChildren().setAll(popupController.getPopups().stream().map(Popup::getRoot).toArray(Pane[]::new)); } /** @@ -88,18 +89,7 @@ public Button createAppButton(App type) { * @param type the app type. */ private void openPopup(App type) { - Popup popup = switch (type) { - case APPSTORE -> new AppStoreApp(400, 300, 100, 100); - case HUSTLERS -> new HustlersApp(400, 300, 140, 140); - case STOCK -> new StockApp(400, 300, 200, 200); - case MAIL -> new MailApp(400, 300, 160, 160); - case NEWS -> new NewsApp(400, 300, 180, 180); - case BANK -> new BankApp(400, 300, 120, 120); - }; - - if (popupController.add(popup)) { - parent.getChildren().add(popup.getRoot()); - } + popupController.show(type); } /** diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java index ffbc303..701b064 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java @@ -67,4 +67,9 @@ public BigDecimal getLatestPriceChange() { return prices.get(prices.size() - 1) .subtract(prices.get(prices.size() - 2)); } + + public void updatePrice() { + BigDecimal newPrice = getSalesPrice(); + prices.add(newPrice); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/StockApp.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/StockApp.java index 16c4312..d612187 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/StockApp.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/StockApp.java @@ -7,6 +7,7 @@ import javafx.geometry.Pos; import javafx.scene.control.*; import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; /** * A popup for the Stock app. @@ -27,26 +28,30 @@ public class StockApp extends Popup { */ public StockApp(int width, int height, int x, int y) { super(width, height, x, y, App.STOCK); - searchField = new TextField(); stockList = new ListView<>(); - stockList.setCellFactory(lv -> new ListCell() { - @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); - } - } - }); + stockList.setCellFactory( + lv -> + new ListCell() { + @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.setOnMouseClicked(event -> { + System.out.println(stock.getCompany() + "is pressed!"); + openStockPage(stock); + }); + row.setAlignment(Pos.CENTER_LEFT); + setGraphic(row); + } + } + }); new StockAppController(this); - content.getChildren().addAll(searchField, stockList); } @@ -57,4 +62,30 @@ public TextField getSearchField() { public ListView getStockList() { return stockList; } + + public void openStockPage(Stock stock){ + content.setAlignment(Pos.TOP_LEFT); + + Button closeButton = new Button("Close"); + setCloseButtonAction(closeButton); + + HBox header = new HBox(); + Label companyLabel = new Label(stock.getCompany()); + Label priceLabel = new Label(stock.getSalesPrice().toString()); + Button buyButton = new Button("Buy"); + Button sellButton = new Button("Sell"); + header.getChildren().addAll(companyLabel, priceLabel, buyButton, sellButton); + + + content.getChildren().setAll(searchField, closeButton, header); + + } + + public void setCloseButtonAction(Button closeButton){ + closeButton.setOnAction(e -> openSearchPage()); + } + + public void openSearchPage(){ + content.getChildren().setAll(searchField, stockList); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popup.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popup.java index 8d0f99f..f56e059 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popup.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Popup.java @@ -4,6 +4,7 @@ import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; +import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; @@ -40,6 +41,7 @@ protected Popup(int width, int height, int x, int y, App type) { this.height = height; this.type = type; + root = new BorderPane(); scrollPane = new ScrollPane(); scrollPane.setStyle("-fx-hbar-policy: never; -fx-vbar-policy: AS_NEEDED; -fx-background-insets: 0; -fx-padding: 0;"); @@ -51,6 +53,7 @@ protected Popup(int width, int height, int x, int y, App type) { closeButton = new Button("Exit"); header.getChildren().addAll(titleLabel, closeButton); header.setStyle("-fx-background-color: lightgray;"); + setCloseButtonAction(); content.setPrefSize(width, height); scrollPane.setMinSize(width, height); @@ -62,7 +65,30 @@ protected Popup(int width, int height, int x, int y, App type) { root.setTop(header); root.setCenter(scrollPane); + setOnPressedAction(); makeDraggable(); + root.setVisible(false); + } + + private void setCloseButtonAction() { + closeButton.setOnAction(e -> root.setVisible(false)); + } + + private void setOnPressedAction() { + root.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> root.toFront()); + } + + private void makeDraggable() { + header.setOnMousePressed(e -> { + dx = e.getSceneX() - root.getLayoutX(); + dy = e.getSceneY() - root.getLayoutY(); + }); + + header.setOnMouseDragged(e -> { + root.setLayoutX(e.getSceneX() - dx); + root.setLayoutY(e.getSceneY() - dy); + keepInBounds(); + }); } /** @@ -100,37 +126,12 @@ public BorderPane getRoot() { return root; } - /** - * Returns the content node of the popup. - */ - public VBox getContent() { - return content; - } - - /** - * Returns the close button of the popup. - */ - public Button getCloseButton() { - return closeButton; - } /** * Returns the type of the popup. */ - public App getType() {return type; - } - - private void makeDraggable() { - header.setOnMousePressed(e -> { - dx = e.getSceneX() - root.getLayoutX(); - dy = e.getSceneY() - root.getLayoutY(); - }); - - header.setOnMouseDragged(e -> { - root.setLayoutX(e.getSceneX() - dx); - root.setLayoutY(e.getSceneY() - dy); - keepInBounds(); - }); + public App getType() { + return type; } /**