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 485ca0e..6a254e9 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 @@ -7,7 +7,7 @@ public class StockAppController { private MarketController marketController; public StockAppController(StockApp stockPopup) { - marketController = new MarketController(); + marketController = new MarketController(stockPopup); stockPopup.getStockList().getItems().addAll(marketController.getMarket()); 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 7f88a29..cd0dad0 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 @@ -2,6 +2,8 @@ import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; import edu.ntnu.idi.idatt2003.gruppe42.Model.StockFileHandler; +import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp; +import javafx.application.Platform; import java.nio.file.Path; import java.util.List; @@ -12,13 +14,17 @@ public class MarketController { private List market; private int stockResolution; + private StockApp stockApp; + + public MarketController(StockApp stockApp) { + this.stockApp = stockApp; + stockResolution = 50; - public MarketController() { try { Path path = Path.of(getClass().getClassLoader().getResource("stocks.csv").toURI()); market = StockFileHandler.readFromFile(path); - //startMarket(); + startMarket(); System.out.println("Market loaded"); } catch (Exception e) { @@ -31,31 +37,37 @@ public List getMarket() { } public List getMarket(String searchTerm) { - return market.stream().filter(stock -> stock.getCompany().toLowerCase().contains(searchTerm.toLowerCase())).toList(); + return market.stream() + .filter(stock -> stock.getCompany().toLowerCase().contains(searchTerm.toLowerCase())) + .toList(); } - public void updateMarket(){ - for (Stock stock : market){ + + public void updateMarket() { + for (Stock stock : market) { stock.updatePrice(); + if (stock.getStockGraph().getVisibility()) { + stock.updateStockGraph(); + stockApp.updatePriceLabel(stock); + } } } - public void startMarket(){ + public void startMarket() { Timer timer = new Timer(); - - for (int i = 0; i < stockResolution; i++){ + for (int i = 0; i < stockResolution; i++) { updateMarket(); } - timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { - updateMarket(); + Platform.runLater(() -> updateMarket()); } }, 0, 1000); } - public static void printMarket(List stocks){ - for (Stock stock : stocks){ + + public static void printMarket(List stocks) { + for (Stock stock : stocks) { System.out.println(stock.getHistoricalPrices().toString()); } } -} +} \ No newline at end of file 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 966719a..9dc317d 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 @@ -27,7 +27,7 @@ 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 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)); 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 701b064..4fb150e 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 @@ -1,5 +1,6 @@ package edu.ntnu.idi.idatt2003.gruppe42.Model; +import edu.ntnu.idi.idatt2003.gruppe42.View.Views.Components.StockGraph; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -14,6 +15,7 @@ public class Stock { private final String symbol; private final String company; + private StockGraph stockGraph; private List prices = new ArrayList<>(); /** @@ -25,6 +27,7 @@ public class Stock { public Stock(String symbol, String company, BigDecimal salesPrice) { this.symbol = symbol; this.company = company; + this.stockGraph = new StockGraph(); prices.add(salesPrice); } @@ -69,7 +72,14 @@ public BigDecimal getLatestPriceChange() { } public void updatePrice() { - BigDecimal newPrice = getSalesPrice(); + BigDecimal newPrice = getSalesPrice().subtract(new BigDecimal("1")); + newPrice = newPrice.max(BigDecimal.ZERO); prices.add(newPrice); } + public StockGraph getStockGraph() { + return stockGraph; + } + public void updateStockGraph() { + stockGraph.update(this); + } } 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 d612187..af4fa36 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 @@ -4,6 +4,8 @@ import edu.ntnu.idi.idatt2003.gruppe42.Model.App; import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock; import edu.ntnu.idi.idatt2003.gruppe42.View.Popup; +import edu.ntnu.idi.idatt2003.gruppe42.View.Views.Components.StockGraph; +import javafx.application.Platform; import javafx.geometry.Pos; import javafx.scene.control.*; import javafx.scene.layout.HBox; @@ -17,6 +19,8 @@ public class StockApp extends Popup { private StockAppController stockAppController; private ListView stockList; private TextField searchField; + private Label priceLabel; + private Stock currentStock; /** * Constructs a new Stock popup. @@ -42,7 +46,7 @@ protected void updateItem(Stock stock, boolean empty) { } else { HBox row = new HBox(10, new Label(stock.getCompany())); row.setOnMouseClicked(event -> { - System.out.println(stock.getCompany() + "is pressed!"); + System.out.println(stock.getCompany() + " is pressed!"); openStockPage(stock); }); row.setAlignment(Pos.CENTER_LEFT); @@ -63,29 +67,40 @@ public ListView getStockList() { return stockList; } - public void openStockPage(Stock stock){ + public void openStockPage(Stock stock) { + currentStock = stock; content.setAlignment(Pos.TOP_LEFT); Button closeButton = new Button("Close"); - setCloseButtonAction(closeButton); + setCloseButtonAction(closeButton, stock); HBox header = new HBox(); Label companyLabel = new Label(stock.getCompany()); - Label priceLabel = new Label(stock.getSalesPrice().toString()); + + priceLabel = new Label(stock.getSalesPrice().toString()); Button buyButton = new Button("Buy"); Button sellButton = new Button("Sell"); header.getChildren().addAll(companyLabel, priceLabel, buyButton, sellButton); + stock.getStockGraph().setVisibility(true); + Platform.runLater(() -> stock.updateStockGraph()); + StockGraph graph = stock.getStockGraph(); + content.getChildren().setAll(searchField, closeButton, header, graph); + } - content.getChildren().setAll(searchField, closeButton, header); - + public void updatePriceLabel(Stock stock) { + if (priceLabel != null && stock == currentStock) { + priceLabel.setText(stock.getSalesPrice().toString()); + } } - public void setCloseButtonAction(Button closeButton){ + public void setCloseButtonAction(Button closeButton, Stock stock) { + stock.getStockGraph().setVisibility(false); closeButton.setOnAction(e -> openSearchPage()); } - public void openSearchPage(){ + 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/Views/Components/StockGraph.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/View/Views/Components/StockGraph.java index df9fea7..4d804f0 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 @@ -14,21 +14,31 @@ public class StockGraph extends VBox { private final LineChart lineChart; private final NumberAxis xAxis; private final NumberAxis yAxis; + private boolean isVisible = false; + private final int stockResolution = 50; - public StockGraph(double width, double height) { + + public StockGraph() { xAxis = new NumberAxis(); xAxis.setLabel("Time"); + xAxis.setAutoRanging(true); + xAxis.setTickLabelsVisible(false); + xAxis.setTickMarkVisible(false); yAxis = new NumberAxis(); yAxis.setLabel("Price"); + yAxis.setAutoRanging(true); lineChart = new LineChart<>(xAxis, yAxis); - lineChart.setPrefSize(width, height); + lineChart.setPrefSize(100, 100); + lineChart.setCreateSymbols(false); + lineChart.setLegendVisible(false); + lineChart.setAnimated(false); getChildren().add(lineChart); } - public boolean display(Stock stock, int resolution) { + public boolean update(Stock stock) { if (stock == null) { return false; } @@ -40,13 +50,13 @@ public boolean display(Stock stock, int resolution) { List history = stock.getHistoricalPrices(); - if (history.size() < resolution) { + if (history.size() < stockResolution) { return false; } - List latestHistory = history.subList(Math.max(history.size() - resolution, 0), history.size()); + List latestHistory = history.subList(Math.max(history.size() - stockResolution, 0), history.size()); - for (int i = 0; i < resolution; i++) { + for (int i = 0; i < stockResolution; i++) { series.getData().add(new XYChart.Data<>(i, latestHistory.get(i))); } @@ -54,7 +64,11 @@ public boolean display(Stock stock, int resolution) { return true; } - public void clear() { - lineChart.getData().clear(); + public boolean getVisibility() { + return isVisible; + } + + public void setVisibility(boolean visible) { + isVisible = visible; } }