Skip to content

Commit

Permalink
Feat: Updated Top bar and Stats page
Browse files Browse the repository at this point in the history
Stats page and top bar now has graphs that change over weeks and value changes
  • Loading branch information
tommyah committed May 13, 2026
1 parent de55313 commit 3e3e6ee
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 74 deletions.
11 changes: 10 additions & 1 deletion src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ntnu.idi.idatt2003.g40.mappe;

import edu.ntnu.idi.idatt2003.g40.mappe.engine.Exchange;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Player;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock;
import edu.ntnu.idi.idatt2003.g40.mappe.service.FileConverter;
import edu.ntnu.idi.idatt2003.g40.mappe.service.FileParser;
Expand All @@ -16,11 +17,13 @@
import edu.ntnu.idi.idatt2003.g40.mappe.view.settings.SettingsController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.settings.SettingsView;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.financialsummary.SummaryController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.financialsummary.SummaryView;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.stats.StatsController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.stats.StatsView;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.topbar.TopBarController;
import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.topbar.TopBarView;
Expand Down Expand Up @@ -62,6 +65,7 @@ public void start(final Stage stage) throws Exception {
stocksInFile = converter1.getStocksFromStrings(parser1.readFile());

Exchange exchange = new Exchange("Exchange", stocksInFile);
Player player = new Player("Player 1", new BigDecimal("10000"));

// Main menu
MainMenuView mainMenuView = new MainMenuView();
Expand All @@ -82,14 +86,19 @@ public void start(final Stage stage) throws Exception {

// Summary section of the top bar
SummaryView summaryView = new SummaryView();
new SummaryController(summaryView, eventManager, exchange);
new SummaryController(summaryView, eventManager, exchange, player);

// Top bar
TopBarView topBarView = new TopBarView(summaryView);
new TopBarController(topBarView, eventManager);

// Stats page
StatsView statsView = new StatsView();
new StatsController(statsView,
eventManager,
player,
exchange,
stocksInFile);

// In-game
InGameView inGameView = new InGameView(topBarView, statsView.getRootPane());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view;

import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData;
import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator;
import java.util.EnumMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.financialsummary;

import edu.ntnu.idi.idatt2003.g40.mappe.engine.Exchange;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Player;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewController;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class SummaryController extends ViewController<SummaryView> {

private Exchange exchange;
private Player player;
private List<Float> playerNetWorthHistory;

/**
* {@inheritDoc}.
*/
public SummaryController(final SummaryView viewElement,
final EventManager eventManager,
final Exchange exchange)
final Exchange exchange,
final Player player)
throws IllegalArgumentException {
this.exchange = exchange;
this.player = player;
this.playerNetWorthHistory = new ArrayList<>();
super(viewElement, eventManager);
getViewElement().setBalance(player.getStartingMoney().floatValue(), player.getStartingMoney().floatValue());
}

@Override
protected void initInteractions() {
playerNetWorthHistory.add(player.getNetWorth().floatValue());
getViewElement().setOnAction(SummaryActions.NEXT_WEEK, () -> {
exchange.nextWeek();
exchange.advance();
});

exchange.weekProperty().addListener((observable, o, n) -> {
getViewElement().setWeek((Integer) n);
playerNetWorthHistory.add(player.getNetWorth().floatValue());
getViewElement().updateChart(playerNetWorthHistory);
getViewElement().setBalance(player.getMoney().floatValue(), player.getNetWorth().floatValue());
});

player.getNetWorthAsFloatProperty().addListener((observable, o, n) -> {
getViewElement().setBalance(player.getMoney().floatValue(), player.getNetWorth().floatValue());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;

import java.math.BigDecimal;
import java.util.List;

public class SummaryView extends ViewElement<HBox, SummaryActions> {
private Label balanceLabel;
private Label weekLabel;
private Label titleLabel;
private LineChart<Number, Number> chart;
private XYChart.Series<Number, Number> dataSeries;
private Button nextBtn;
private NumberAxis xAxis;
private NumberAxis yAxis;

public SummaryView() {
super(new HBox(), SummaryActions.class);
Expand All @@ -38,20 +43,20 @@ protected void initLayout() {

VBox balanceInfo = new VBox();

titleLabel = new Label("balance/investments");
balanceLabel = new Label("10000$/0$");
titleLabel = new Label("Networth");
balanceLabel = new Label("0$");

Region spacerL = new Region();
VBox.setVgrow(spacerL, Priority.ALWAYS);

balanceInfo.getChildren().addAll(titleLabel, spacerL, balanceLabel);

NumberAxis xAxis = new NumberAxis(1, 22, 1);
xAxis = new NumberAxis(1, 10, 1);
xAxis.setMinorTickVisible(false);
xAxis.setTickMarkVisible(true);
xAxis.setTickLabelsVisible(true);

NumberAxis yAxis = new NumberAxis();
yAxis = new NumberAxis();
yAxis.setTickLabelsVisible(false);
yAxis.setTickMarkVisible(false);
yAxis.setMinorTickVisible(false);
Expand Down Expand Up @@ -103,15 +108,35 @@ protected void initStyling() {
nextBtn.getStyleClass().add("next-button");
}

public void setBalance(final String text) {
balanceLabel.setText(text);
public void setBalance(final float money, final float netWorth) {
balanceLabel.setText(Math.round(money*100f)/100f + "$ / " + Math.round(netWorth*100f)/100f + "$");
}

public void setWeek(int week) {
weekLabel.setText("week: " + week);
}

public void updateChart(double x, double y) {
dataSeries.getData().add(new XYChart.Data<>(x, y));
public void updateChart(final List<Float> playerNetWorthHistory) {
dataSeries.getData().clear();
xAxis.setLowerBound(Math.max(1, playerNetWorthHistory.size() - 10));
xAxis.setUpperBound(Math.max(10, playerNetWorthHistory.size()));

double min = playerNetWorthHistory.stream()
.min(Float::compare)
.orElse(0.0f);

double max = playerNetWorthHistory.stream()
.max(Float::compare)
.orElse(100.0f);

double padding = (max - min) * 0.05;

yAxis.setLowerBound(Math.max(0, min - padding));
yAxis.setUpperBound(max + padding);

yAxis.setTickUnit(Math.round((max - min) / 5));
for (int i = 0; i < playerNetWorthHistory.size(); i++) {
dataSeries.getData().add(new XYChart.Data<>(i + 1, playerNetWorthHistory.get(i)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public enum StatsActions {
BUY_SHARES,
SELL_SHARES;
SELL_SHARES,
SELECT_STOCK;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,68 @@

import edu.ntnu.idi.idatt2003.g40.mappe.engine.Exchange;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Player;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Share;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Transaction;
import edu.ntnu.idi.idatt2003.g40.mappe.service.PurchaseCalculator;
import edu.ntnu.idi.idatt2003.g40.mappe.service.TransactionCalculator;
import edu.ntnu.idi.idatt2003.g40.mappe.service.TransactionFactory;
import edu.ntnu.idi.idatt2003.g40.mappe.service.TransactionType;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewController;
import javafx.scene.control.Button;

import java.math.BigDecimal;
import java.util.List;

public class StatsController extends ViewController<StatsView> {

private Player player;
private Exchange exchange;
private List<Stock> stockList;

/**
* {@inheritDoc}
*/
public StatsController(final StatsView viewElement,
final EventManager eventManager,
final Player player,
final Exchange exchange)
final Exchange exchange,
final List<Stock> stockList)
throws IllegalArgumentException {
this.player = player;
this.stockList = stockList;
this.exchange = exchange;
super(viewElement, eventManager);
}

private void handleStockSelection(Stock stock) {
getViewElement().setCurrentStock(stock);
getViewElement().updateGraph();
}

private void populateStockList() {
getViewElement().clearStockList();
for (Stock s : stockList) {
Button stockBtn = getViewElement().createStockButton(s.getSymbol());

getViewElement().setOnStockAction(stockBtn, s, selectedStock -> {
handleStockSelection(selectedStock);
});
}
}


@Override
protected void initInteractions() {
populateStockList();
getViewElement().setCurrentStock(stockList.getFirst());
getViewElement().updateGraph();
getViewElement().setOnAction(StatsActions.BUY_SHARES, () -> {
BigDecimal amountToBuy = new BigDecimal("1.0");
Transaction transaction = exchange.buy(
exchange.buy(
getViewElement().getCurrentStock().getSymbol(),
amountToBuy,
player
);
});

exchange.weekProperty().addListener((observable,o,n) -> {
getViewElement().updateGraph();
});
}
}
Loading

0 comments on commit 3e3e6ee

Please sign in to comment.