Skip to content

Commit

Permalink
Merge pull request #86 from einaskoi/per/StockGraph
Browse files Browse the repository at this point in the history
add live updated graph and price label, such that the user can see cu…
  • Loading branch information
einaskoi authored Apr 22, 2026
2 parents 6f60555 + 3939218 commit 4ae6f81
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,13 +14,17 @@ public class MarketController {

private List<Stock> 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) {
Expand All @@ -31,31 +37,37 @@ public List<Stock> getMarket() {
}

public List<Stock> 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<Stock> stocks){
for (Stock stock : stocks){

public static void printMarket(List<Stock> stocks) {
for (Stock stock : stocks) {
System.out.println(stock.getHistoricalPrices().toString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +15,7 @@
public class Stock {
private final String symbol;
private final String company;
private StockGraph stockGraph;
private List<BigDecimal> prices = new ArrayList<>();

/**
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +19,8 @@ public class StockApp extends Popup {
private StockAppController stockAppController;
private ListView<Stock> stockList;
private TextField searchField;
private Label priceLabel;
private Stock currentStock;

/**
* Constructs a new Stock popup.
Expand All @@ -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);
Expand All @@ -63,29 +67,40 @@ public ListView<Stock> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@ public class StockGraph extends VBox {
private final LineChart<Number, Number> 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;
}
Expand All @@ -40,21 +50,25 @@ public boolean display(Stock stock, int resolution) {

List<BigDecimal> history = stock.getHistoricalPrices();

if (history.size() < resolution) {
if (history.size() < stockResolution) {
return false;
}

List<BigDecimal> latestHistory = history.subList(Math.max(history.size() - resolution, 0), history.size());
List<BigDecimal> 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)));
}

lineChart.getData().add(series);
return true;
}

public void clear() {
lineChart.getData().clear();
public boolean getVisibility() {
return isVisible;
}

public void setVisibility(boolean visible) {
isVisible = visible;
}
}

0 comments on commit 4ae6f81

Please sign in to comment.