Skip to content

Commit

Permalink
Feat: Improved trade actions sections
Browse files Browse the repository at this point in the history
Added quantity buttons and buy / sell button in top section of dashboard view. Functionality not added yet.
  • Loading branch information
tommyah committed May 15, 2026
1 parent 6322b37 commit 91aff4f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ public Transaction sell(final Share share, final Player player)
* Method for advancing time, increasing the amount of weeks.
* */
public void advance() {
week.set(week.get() + 1);
for (Stock stock : stockMap.values()) {
BigDecimal currentPrice = stock.getSalesPrice();

Expand All @@ -213,6 +212,7 @@ public void advance() {
BigDecimal newPrice = currentPrice.multiply(factor);
stock.addNewSalesPrice(newPrice);
}
week.set(week.get() + 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ private void populateStockList() {
for (Stock s : stockList) {
Button stockBtn = getViewElement().createStockButton(s.getSymbol());

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.function.Consumer;

import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.financialsummary.SummaryView;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.chart.LineChart;
Expand All @@ -18,26 +19,24 @@
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.layout.*;

public class DashBoardView extends ViewElement<HBox, DashBoardActions> {
private LineChart<Number, Number> chart;
private XYChart.Series<Number, Number> dataSeries;
private VBox sidebar;
private ArrayList<Button> stockButtons;
private Stock selectedStock;
private Label selectedStockLabel;
private HBox tradeActions;
private NumberAxis xAxis;
private NumberAxis yAxis;
private Label selectedStockPriceLabel;
private TextField shareQuantityInputField;
private Button m5QtyBtn;
private Button m1QtyBtn;
private Button p1QtyBtn;
private Button p5QtyBtn;

public DashBoardView() {
stockButtons = new ArrayList<>();
super(new HBox(), DashBoardActions.class);
}

Expand Down Expand Up @@ -71,9 +70,6 @@ protected void initLayout() {
selectedStockLabel = new Label("");
selectedStockPriceLabel = new Label("");

HBox topSection = new HBox();
topSection.getChildren().addAll(selectedStockLabel, selectedStockPriceLabel);

xAxis = new NumberAxis(1, 22, 1);
xAxis.setMinorTickVisible(false);
xAxis.setTickMarkVisible(true);
Expand Down Expand Up @@ -103,12 +99,6 @@ protected void initLayout() {
chart.minHeightProperty().set(50);
chart.getData().add(dataSeries);

tradeActions = new HBox();
tradeActions.setAlignment(Pos.CENTER);
tradeActions.setMaxWidth(Integer.MAX_VALUE);
tradeActions.setPrefWidth(Integer.MAX_VALUE);
tradeActions.minWidth(100);

Button buyBtn = new Button("buy");
buyBtn.getStyleClass().add("buy-button");
registerButton(DashBoardActions.BUY_SHARES, buyBtn);
Expand All @@ -117,31 +107,108 @@ protected void initLayout() {
sellBtn.getStyleClass().add("sell-button");
registerButton(DashBoardActions.SELL_SHARES, sellBtn);

HBox header = new HBox(20);
header.setAlignment(Pos.CENTER_LEFT);

// Stock Identity (The gray pill)
VBox stockIdentity = new VBox(5);
selectedStockLabel = new Label("APL");
selectedStockLabel.getStyleClass().add("stock-symbol-title");
Label stockFullNameLabel = new Label("Apple corporation");
Label ownedQuantityLabel = new Label("Owned: 0");
stockIdentity.getChildren().addAll(selectedStockLabel, stockFullNameLabel, ownedQuantityLabel);

// Price Stats
VBox priceStats = new VBox(5);
selectedStockPriceLabel = new Label("0.0 NOK (+0.0%)");
selectedStockPriceLabel.getStyleClass().add("price-large");

HBox lowHighBox = new HBox(10);
Label lowPriceLabel = new Label("Low: 0.0 NOK");
Label highPriceLabel = new Label("High: 0.0 NOK");
lowHighBox.getChildren().addAll(lowPriceLabel, highPriceLabel);
priceStats.getChildren().addAll(selectedStockPriceLabel, lowHighBox);

m5QtyBtn = new Button("-5");
m1QtyBtn = new Button("-1");
shareQuantityInputField = new TextField("1.0");
shareQuantityInputField.setAlignment(Pos.CENTER);
p1QtyBtn = new Button("+1");
p5QtyBtn = new Button("+5");

buyBtn = new Button("buy");
buyBtn.getStyleClass().add("buy-button");
sellBtn = new Button("sell");
sellBtn.getStyleClass().add("sell-button");

GridPane grid = new GridPane();
grid.setHgap(0);
grid.setVgap(0);
grid.setAlignment(Pos.CENTER);

int amountOfCols = 6;
float colWidth = 100f/amountOfCols;

for (int i = 0; i<amountOfCols; i++) {
if(i == 0 || i == amountOfCols - 1) {
colWidth = 20;
} else {
colWidth = 15;
}
ColumnConstraints curCol = makeCol(colWidth);
grid.getColumnConstraints().add(curCol);
}

shareQuantityInputField.setPromptText("Quantity amount");
HBox leftQty = new HBox(0, m5QtyBtn, m1QtyBtn);
leftQty.setAlignment(Pos.CENTER_RIGHT);

HBox rightQty = new HBox(0, p1QtyBtn, p5QtyBtn);
rightQty.setAlignment(Pos.CENTER_LEFT);

HBox qtySection = new HBox(0, leftQty, shareQuantityInputField, rightQty);
qtySection.setAlignment(Pos.CENTER);

HBox tradeBtns = new HBox(20, buyBtn, sellBtn);
HBox.setHgrow(buyBtn, Priority.ALWAYS);
HBox.setHgrow(sellBtn, Priority.ALWAYS);
tradeBtns.setAlignment(Pos.CENTER);

grid.add(qtySection, 0, 0, 6,1);
grid.add(tradeBtns, 0, 1, 6, 1);

header.getChildren().addAll(stockIdentity, priceStats, grid);

Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
tradeActions.getChildren().addAll(buyBtn, spacer, shareQuantityInputField, sellBtn);

VBox.setVgrow(chart, Priority.ALWAYS);

mainContent.getChildren().addAll(topSection, chart, tradeActions);
mainContent.getChildren().addAll(header, chart);
getRootPane().getChildren().addAll(scrollPane, mainContent);

registerButton(DashBoardActions.BUY_SHARES, buyBtn);
registerButton(DashBoardActions.SELL_SHARES, sellBtn);
}

private ColumnConstraints makeCol(final float w) {
ColumnConstraints col = new ColumnConstraints();
col.setHalignment(HPos.CENTER);
col.setPercentWidth(w);
col.setFillWidth(true);
return col;
}

@Override
protected void initStyling() {
sidebar.getStyleClass().add("market-sidebar");
tradeActions.getStyleClass().add("trade-actions");
selectedStockLabel.getStyleClass().add("selected-stock-pill");
selectedStockPriceLabel.getStyleClass().add("selected-stock-pill");
shareQuantityInputField.getStyleClass().add("qtyTextField");
chart.getStyleClass().add("chart");
m5QtyBtn.getStyleClass().add("qtyBtn");
m1QtyBtn.getStyleClass().add("qtyBtn");
p1QtyBtn.getStyleClass().add("qtyBtn");
p5QtyBtn.getStyleClass().add("qtyBtn");
}

public void setCurrentStock(final Stock stock) {
Expand All @@ -151,21 +218,19 @@ public void setCurrentStock(final Stock stock) {

public Button createStockButton(final String label) {
Button button = new Button(label);
stockButtons.add(button);
sidebar.getChildren().add(button);
button.getStyleClass().add("stock-button");
return button;
}

public void clearStockList() {
stockButtons.clear();
sidebar.getChildren().clear();
}

public void updateGraph() {
dataSeries.getData().clear();
List<BigDecimal> prices = selectedStock.getHistoricalPrices();
updateStockPrice(prices.getLast().floatValue());
updateStockPrice(prices.getLast().floatValue(), selectedStock.getLatestPriceChange().floatValue());
xAxis.setLowerBound(Math.max(1, prices.size() - 22));
xAxis.setUpperBound(Math.max(22, prices.size()));

Expand All @@ -190,8 +255,14 @@ public void updateGraph() {
}
}

private void updateStockPrice(final float value) {
selectedStockPriceLabel.setText(Float.toString(Math.round(value*100f)/100f));
private void updateStockPrice(final float value, final float changeSinceLast) {
String changeSinceLastString;
if (changeSinceLast >= 0) {
changeSinceLastString = "+ " + Math.round(changeSinceLast*100f)/100f;
} else {
changeSinceLastString = "- " + Math.abs(changeSinceLast*100f)/100f;
}
selectedStockPriceLabel.setText(Float.toString(Math.round(value*100f)/100f) + "NOK (" + changeSinceLastString + ")");
}

public Stock getCurrentStock() {
Expand Down
59 changes: 50 additions & 9 deletions src/main/resources/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
-fx-font-size: 24;
}

.stock-button : hover {
.stock-button:hover {
-fx-cursor: hand;
-fx-scale-x: 1.05;
-fx-scale-y: 1.05;
Expand All @@ -271,10 +271,14 @@
-fx-text-fill: black;
-fx-font-style: italic;
-fx-font-weight: bold;
-fx-min-height: 60;
-fx-pref-height: 80;
-fx-min-width: 150;
-fx-max-width: 250;

-fx-min-height: 30;
-fx-pref-height: 45;
-fx-max-height: 60;

-fx-min-width: 60;
-fx-pref-width: 80;
-fx-max-width: 200;
}

.sell-button {
Expand All @@ -284,14 +288,51 @@
-fx-text-fill: black;
-fx-font-style: italic;
-fx-font-weight: bold;
-fx-min-height: 60;
-fx-pref-height: 80;
-fx-min-width: 150;
-fx-max-width: 250;

-fx-min-height: 30;
-fx-pref-height: 45;
-fx-max-height: 60;

-fx-min-width: 60;
-fx-pref-width: 80;
-fx-max-width: 200;
}

.buy-button:hover, .sell-button:hover {
-fx-cursor: hand;
-fx-scale-x: 1.05;
-fx-scale-y: 1.05;
}
.qtyBtn {
-fx-background-color: rgba(140, 140, 140, 0.6);

-fx-background-radius: 50;

-fx-text-fill: white;
-fx-font-weight: bold;
-fx-font-size: 14px;

-fx-min-width: 60;
-fx-pref-width: 75;
-fx-max-width: 90;

-fx-min-height: 20;
-fx-pref-height: 30;
-fx-max-height: 40;
}

.qtyBtn:hover {
-fx-cursor: hand;
-fx-scale-x: 1.05;
-fx-scale-y: 1.05;
}

.qtyTextField {
-fx-min-width: 50;
-fx-pref-width: 75;
-fx-max-width: 100;

-fx-min-height: 30;
-fx-pref-height: 45;
-fx-max-height: 60;
}

0 comments on commit 91aff4f

Please sign in to comment.