diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/AppController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/AppController.java new file mode 100644 index 0000000..1d052ad --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/AppController.java @@ -0,0 +1,5 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers; + +public interface AppController { + void nextTick(); +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/AppStoreController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/AppStoreController.java new file mode 100644 index 0000000..5f9ffdd --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/AppStoreController.java @@ -0,0 +1,12 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers; + +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.AppStoreApp; + +public class AppStoreController implements AppController { + public AppStoreController(AppStoreApp appStoreApp) { + } + + @Override + public void nextTick() { + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/BankAppController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/BankAppController.java index dc37c8d..979d60d 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/BankAppController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/AppControllers/BankAppController.java @@ -1,4 +1,24 @@ package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers; -public class BankAppController { +import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.BankApp; + +public class BankAppController implements AppController{ + private Player player; + private BankApp bankApp; + + public BankAppController(BankApp bankApp, Player player) { + this.player = player; + this.bankApp = bankApp; + } + @Override + public void nextTick() { + System.out.println("[DEBUG] BankAppController.nextTick() - Updating Player Status"); + bankApp.updateStatus( + player.getNetWorth().doubleValue(), + player.getMoney().doubleValue(), + player.getPortfolio().getNetWorth().doubleValue(), + 0 // TODO: calculate growth + ); + } } 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 6a254e9..101b38e 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 @@ -3,11 +3,13 @@ import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController; import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp; -public class StockAppController { +public class StockAppController implements AppController { private MarketController marketController; + private StockApp stockPopup; - public StockAppController(StockApp stockPopup) { - marketController = new MarketController(stockPopup); + public StockAppController(StockApp stockPopup, MarketController marketController) { + this.stockPopup = stockPopup; + this.marketController = marketController; stockPopup.getStockList().getItems().addAll(marketController.getMarket()); @@ -16,4 +18,14 @@ public StockAppController(StockApp stockPopup) { }); } + + @Override + public void nextTick(){ + System.out.println("[DEBUG] StockAppController.nextTick() - Updating Market"); + marketController.updateMarket(); + if (stockPopup.getCurrentStock() != null) { + System.out.println("[DEBUG] Updating Price Label for: " + stockPopup.getCurrentStock().getCompany()); + stockPopup.updatePriceLabel(stockPopup.getCurrentStock()); + } + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/GameController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/GameController.java new file mode 100644 index 0000000..e8e5401 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/GameController.java @@ -0,0 +1,40 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Controller; + +import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppController; +import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +public class GameController { + + private Player player; + private List appControllers = new ArrayList<>(); + private Timer timer; + + public GameController(Player player) { + this.player = player; + } + + public void addAppController(AppController appController) { + appControllers.add(appController); + } + + public void startGame() { + timer = new Timer(); + + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + System.out.println("[DEBUG] Game Tick Started"); + for (AppController controller : appControllers) { + controller.nextTick(); + } + System.out.println("[DEBUG] Game Tick Finished"); + } + }, 0, 1000); + } +} 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 cd0dad0..f7d0711 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 @@ -16,8 +16,7 @@ public class MarketController { private int stockResolution; private StockApp stockApp; - public MarketController(StockApp stockApp) { - this.stockApp = stockApp; + public MarketController() { stockResolution = 50; try { @@ -43,26 +42,19 @@ public List getMarket(String searchTerm) { } public void updateMarket() { + System.out.println("[DEBUG] MarketController.updateMarket() - Updating prices for " + market.size() + " stocks"); for (Stock stock : market) { stock.updatePrice(); if (stock.getStockGraph().getVisibility()) { stock.updateStockGraph(); - stockApp.updatePriceLabel(stock); } } } public void startMarket() { - Timer timer = new Timer(); for (int i = 0; i < stockResolution; i++) { updateMarket(); } - timer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - Platform.runLater(() -> updateMarket()); - } - }, 0, 1000); } public static void printMarket(List stocks) { 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 9dc317d..442ec6f 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 @@ -23,14 +23,8 @@ public class PopupController { /** * Constructs a new popup controller. */ - 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(500, 400, 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)); + public PopupController(List popups) { + this.popups = popups; } //TODO: javadoc @@ -64,4 +58,8 @@ public void bringToFront(Popup popup) { public List getPopups() { return popups; } + + public Popup getPopup(App type) { + return popups.stream().filter(popup -> popup.getType().equals(type)).findFirst().orElse(null); + } } 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 4ca10e5..6f64d63 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 @@ -1,14 +1,23 @@ package edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppStoreController; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.BankAppController; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.StockAppController; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.GameController; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController; import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupController; import edu.ntnu.idi.idatt2003.gruppe42.Model.App; +import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; 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.Popup; import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp; +import edu.ntnu.idi.idatt2003.gruppe42.View.Popup; +import edu.ntnu.idi.idatt2003.gruppe42.View.Views.DesktopView; +import java.util.ArrayList; +import java.util.List; import javafx.beans.binding.Bindings; import javafx.scene.control.Button; import javafx.scene.input.ClipboardContent; @@ -19,23 +28,55 @@ import javafx.scene.layout.StackPane; /** - * Controller for the app buttons. + * Controller for the desktop. * Handles app button creation, resizing, click events, and drag-and-drop. */ public class DesktopViewController { private final PopupController popupController; - private final Pane parent; + private final DesktopView desktopView; /** - * Constructs a new app controller. + * Constructs a new desktop view controller. * - * @param popupController the popup controller to manage popups. - * @param parent the parent pane to add popups to. + * @param player the player model. + * @param gameController the game controller to register app controllers. */ - public DesktopViewController(PopupController popupController, Pane parent) { - this.popupController = popupController; - this.parent = parent; - parent.getChildren().setAll(popupController.getPopups().stream().map(Popup::getRoot).toArray(Pane[]::new)); + public DesktopViewController(Player player, GameController gameController) { + List popups = new ArrayList<>(); + + AppStoreApp appStoreApp = new AppStoreApp(400, 300, 100, 100); + gameController.addAppController(new AppStoreController(appStoreApp)); + + HustlersApp hustlersApp = new HustlersApp(400, 300, 140, 140); + + StockApp stockApp = new StockApp(400, 300, 120, 120); + MarketController marketController = new MarketController(); + gameController.addAppController(new StockAppController(stockApp, marketController)); + + MailApp mailApp = new MailApp(400, 300, 160, 160); + + NewsApp newsApp = new NewsApp(400, 300, 180, 180); + + BankApp bankApp = new BankApp(); + gameController.addAppController(new BankAppController(bankApp, player)); + + popups.add(appStoreApp); + popups.add(hustlersApp); + popups.add(stockApp); + popups.add(mailApp); + popups.add(newsApp); + popups.add(bankApp); + + this.popupController = new PopupController(popups); + this.desktopView = new DesktopView(this); + + this.desktopView.getRoot().getChildren().addAll( + popupController.getPopups().stream().map(Popup::getRoot).toArray(Pane[]::new) + ); + } + + public DesktopView getDesktopView() { + return desktopView; } /** diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java index 917b575..9a3eb7d 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Controller/ViewControllers/StartViewController.java @@ -4,7 +4,6 @@ import edu.ntnu.idi.idatt2003.gruppe42.Model.Difficulty; import edu.ntnu.idi.idatt2003.gruppe42.Model.Player; import edu.ntnu.idi.idatt2003.gruppe42.View.Views.StartView; -import java.math.BigDecimal; import java.util.List; import java.util.Random; import javafx.scene.control.Alert; @@ -49,7 +48,6 @@ private void handleStart() { Difficulty difficulty = Difficulty.fromString(startView.getSelectedMode()); application.initGame(resolvedName, difficulty); - application.switchToDesktopView(); } private String resolveUsername(String input) { diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java index 226704e..a9afdb3 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java @@ -1,5 +1,7 @@ package edu.ntnu.idi.idatt2003.gruppe42; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.GameController; +import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.DesktopViewController; import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.StartViewController; import edu.ntnu.idi.idatt2003.gruppe42.Model.Difficulty; import edu.ntnu.idi.idatt2003.gruppe42.Model.GameFactory; @@ -16,6 +18,7 @@ public class Millions extends Application { private Stage stage; private DesktopView desktopView; private Player player; + private GameController gameController; @Override public void start(Stage stage) throws Exception { @@ -35,6 +38,9 @@ public void start(Stage stage) throws Exception { public void initGame(String username, Difficulty difficulty) { player = GameFactory.createPlayer(username, difficulty); + gameController = new GameController(player); + gameController.startGame(); + switchToDesktopView(); } public void switchToStartView() { @@ -44,7 +50,8 @@ public void switchToStartView() { } public void switchToDesktopView() { - desktopView = new DesktopView(); + DesktopViewController desktopViewController = new DesktopViewController(player, gameController); + desktopView = desktopViewController.getDesktopView(); scene.setRoot(desktopView.getRoot()); stage.setScene(scene); } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java index 71b6ebe..0f9ba70 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java @@ -97,12 +97,13 @@ public List findStocks(String searchTerm) { * @return a {@code Purchase} transaction representing the purchase */ public Transaction buy(String symbol, BigDecimal quantity, Player player) { - + System.out.println("[DEBUG] Exchange " + name + " - Buying " + quantity + " of " + symbol + " for player " + player.getName()); player.withdrawMoney(quantity); Stock stock = getStock(symbol); BigDecimal purchasePrice = stock.getSalesPrice(); Share share = new Share(stock, quantity, purchasePrice); + System.out.println("[DEBUG] Purchase complete: " + quantity + " shares of " + symbol + " at " + purchasePrice); return new Purchase(share, week); } @@ -117,8 +118,11 @@ public Transaction buy(String symbol, BigDecimal quantity, Player player) { * @return a sale transaction representing the sale */ public Transaction sell(Share share, Player player) { + System.out.println("[DEBUG] Exchange " + name + " - Selling shares of " + share.getStock().getSymbol() + " for player " + player.getName()); SaleCalculator saleCalculator = new SaleCalculator(share); - player.addMoney(saleCalculator.calculateTotal()); + BigDecimal totalValue = saleCalculator.calculateTotal(); + player.addMoney(totalValue); + System.out.println("[DEBUG] Sale complete: Received " + totalValue); return new Sale(share, week); } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java index cb4f284..3dade14 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java @@ -55,7 +55,9 @@ public BigDecimal getMoney() { * @param amount the amount to add to the player's account */ public void addMoney(BigDecimal amount) { + BigDecimal oldMoney = money; money = money.add(amount); + System.out.println("[DEBUG] Player " + name + " money added: " + oldMoney + " -> " + money + " (+" + amount + ")"); } /** @@ -65,9 +67,12 @@ public void addMoney(BigDecimal amount) { */ public void withdrawMoney(BigDecimal amount) { if (money.compareTo(amount) < 0) { + System.out.println("[DEBUG] Player " + name + " withdrawal FAILED: Insufficient funds (Needs: " + amount + ", Has: " + money + ")"); throw new InsufficientFunds("Not enough money to withdraw " + amount); } + BigDecimal oldMoney = money; money = money.subtract(amount); + System.out.println("[DEBUG] Player " + name + " money withdrawn: " + oldMoney + " -> " + money + " (-" + amount + ")"); } /** 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 4fb150e..a0b5abd 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 @@ -72,9 +72,11 @@ public BigDecimal getLatestPriceChange() { } public void updatePrice() { + BigDecimal oldPrice = getSalesPrice(); BigDecimal newPrice = getSalesPrice().subtract(new BigDecimal("1")); newPrice = newPrice.max(BigDecimal.ZERO); prices.add(newPrice); + System.out.println("[DEBUG] Stock " + symbol + " updated: " + oldPrice + " -> " + newPrice); } public StockGraph getStockGraph() { return stockGraph; diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/AppStoreApp.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/AppStoreApp.java index 6fa285d..c37bcd8 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/AppStoreApp.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/AppStoreApp.java @@ -20,4 +20,5 @@ public AppStoreApp(int width, int height, int x, int y) { super(width, height, x, y, App.APPSTORE); // Add AppStore specific content here } + } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/BankApp.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/BankApp.java index 4652c9d..3c69933 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/BankApp.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/BankApp.java @@ -1,23 +1,48 @@ package edu.ntnu.idi.idatt2003.gruppe42.View.Apps; - + import edu.ntnu.idi.idatt2003.gruppe42.Model.App; import edu.ntnu.idi.idatt2003.gruppe42.View.Popup; +import javafx.application.Platform; +import javafx.scene.control.Label; +import javafx.scene.layout.GridPane; /** * A popup for the Bank app. */ public class BankApp extends Popup { + private Label netWorthLabel = new Label("Net Worth: "); + private Label unInvestedMoneyLabel = new Label("Cash: "); + private Label investedMoneyLabel = new Label("Stock: "); + private Label growthPercentageLabel = new Label("Growth: "); /** * Constructs a new Bank popup. - * - * @param width width of the popup - * @param height height of the popup - * @param x x-position of the popup - * @param y y-position of the popup */ - public BankApp(int width, int height, int x, int y) { - super(width, height, x, y, App.BANK); - // Add Bank specific content here + public BankApp(){ + super(400, 300, 120, 120, App.BANK); + updateContent(); + } + + private void updateContent(){ + GridPane statusPane = new GridPane(); + + statusPane.add(netWorthLabel, 0, 0); + statusPane.add(unInvestedMoneyLabel, 0, 1); + statusPane.add(investedMoneyLabel, 1, 1); + statusPane.add(growthPercentageLabel, 1, 0); + + content.getChildren().setAll(statusPane); + } + + public void updateStatus(double netWorth, double unInvestedMoney, double investedMoney, double growthPercentage) { + Platform.runLater(() -> { + netWorthLabel.setText("Net Worth: " + netWorth); + unInvestedMoneyLabel.setText("Cash: " + unInvestedMoney); + investedMoneyLabel.setText("Stock: " + investedMoney); + growthPercentageLabel.setText("Growth: " + growthPercentage + "%"); + updateContent(); + }); } } + + diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/HustlersApp.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/HustlersApp.java index dc7d7b4..7467c1d 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/HustlersApp.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/HustlersApp.java @@ -20,4 +20,5 @@ public HustlersApp(int width, int height, int x, int y) { super(width, height, x, y, App.HUSTLERS); // Add Hustlers specific content here } + } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/MailApp.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/MailApp.java index dde2bc6..3b31ac3 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/MailApp.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Apps/MailApp.java @@ -20,4 +20,5 @@ public MailApp(int width, int height, int x, int y) { super(width, height, x, y, App.MAIL); // Add Mail specific content here } + } 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 af4fa36..deb75fd 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 @@ -1,6 +1,5 @@ package edu.ntnu.idi.idatt2003.gruppe42.View.Apps; -import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.StockAppController; import edu.ntnu.idi.idatt2003.gruppe42.Model.App; import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; import edu.ntnu.idi.idatt2003.gruppe42.View.Popup; @@ -15,8 +14,6 @@ * A popup for the Stock app. */ public class StockApp extends Popup { - - private StockAppController stockAppController; private ListView stockList; private TextField searchField; private Label priceLabel; @@ -55,10 +52,13 @@ protected void updateItem(Stock stock, boolean empty) { } }); - new StockAppController(this); content.getChildren().addAll(searchField, stockList); } + public Stock getCurrentStock() { + return currentStock; + } + public TextField getSearchField() { return searchField; } @@ -90,7 +90,7 @@ public void openStockPage(Stock stock) { public void updatePriceLabel(Stock stock) { if (priceLabel != null && stock == currentStock) { - priceLabel.setText(stock.getSalesPrice().toString()); + Platform.runLater(() -> priceLabel.setText(stock.getSalesPrice().toString())); } } @@ -103,4 +103,5 @@ public void openSearchPage() { currentStock = null; content.getChildren().setAll(searchField, stockList); } + } \ No newline at end of file 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 f56e059..657e831 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 @@ -1,5 +1,5 @@ package edu.ntnu.idi.idatt2003.gruppe42.View; - + import edu.ntnu.idi.idatt2003.gruppe42.Model.App; import javafx.scene.control.Button; import javafx.scene.control.Label; @@ -126,7 +126,6 @@ public BorderPane getRoot() { return root; } - /** * Returns the type of the popup. */ diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/Components/StockGraph.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/Components/StockGraph.java index 4d804f0..31f18cb 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/Components/StockGraph.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/Components/StockGraph.java @@ -1,6 +1,7 @@ package edu.ntnu.idi.idatt2003.gruppe42.View.Views.Components; import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; +import javafx.application.Platform; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; @@ -43,11 +44,6 @@ public boolean update(Stock stock) { return false; } - lineChart.getData().clear(); - - XYChart.Series series = new XYChart.Series<>(); - series.setName(stock.getCompany()); - List history = stock.getHistoricalPrices(); if (history.size() < stockResolution) { @@ -56,11 +52,18 @@ public boolean update(Stock stock) { List latestHistory = history.subList(Math.max(history.size() - stockResolution, 0), history.size()); - for (int i = 0; i < stockResolution; i++) { - series.getData().add(new XYChart.Data<>(i, latestHistory.get(i))); - } + Platform.runLater(() -> { + lineChart.getData().clear(); + + XYChart.Series series = new XYChart.Series<>(); + series.setName(stock.getCompany()); + + for (int i = 0; i < stockResolution; i++) { + series.getData().add(new XYChart.Data<>(i, latestHistory.get(i))); + } - lineChart.getData().add(series); + lineChart.getData().add(series); + }); return true; } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/DesktopView.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/DesktopView.java index 9094dc2..010c120 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/DesktopView.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/DesktopView.java @@ -1,7 +1,6 @@ package edu.ntnu.idi.idatt2003.gruppe42.View.Views; import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.DesktopViewController; -import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupController; import edu.ntnu.idi.idatt2003.gruppe42.Model.App; import javafx.geometry.Pos; import javafx.scene.layout.BorderPane; @@ -16,17 +15,14 @@ * Displays a 6x4 grid of apps. */ public class DesktopView { - - private final PopupController popupController; - private final DesktopViewController appController; + private final DesktopViewController desktopViewController; private final BorderPane root; private static final int COLS = 6; private static final int ROWS = 4; - public DesktopView() { - this.popupController = new PopupController(); + public DesktopView(DesktopViewController desktopViewController) { this.root = new BorderPane(); - this.appController = new DesktopViewController(popupController, root); + this.desktopViewController = desktopViewController; } /** @@ -72,7 +68,7 @@ private GridPane createGrid() { // Add initial buttons to the first row based on the Apps enum if (row == 0 && col < App.values().length) { - cell.getChildren().add(appController.createAppButton(App.values()[col])); + cell.getChildren().add(desktopViewController.createAppButton(App.values()[col])); } } } @@ -87,7 +83,7 @@ private GridPane createGrid() { private StackPane createCell() { StackPane cell = new StackPane(); cell.setStyle("-fx-border-color: black; -fx-border-width: 1;"); - appController.configureCellAsDropTarget(cell); + desktopViewController.configureCellAsDropTarget(cell); return cell; }