Skip to content

Commit

Permalink
Fix: Fixed visual bug regarding precision error when calculating owne…
Browse files Browse the repository at this point in the history
…d shares
  • Loading branch information
tommyah committed May 19, 2026
1 parent a860e36 commit 41f1125
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ protected void initInteractions() {
getViewElement().setCurrentStock(stockList.getFirst(), 0);
getViewElement().updateGraph(selectedTimeRange);
getViewElement().setOnAction(DashBoardActions.BUY_SHARES, () -> {
if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())) {
if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())
&& Float.parseFloat(getViewElement().getQuantityInputField().getText()) > 0) {
BigDecimal amountToBuy = new BigDecimal(getViewElement().getQuantityInputField().getText());
Transaction purchase = exchange.buy(
getViewElement().getCurrentStock().getSymbol(),
Expand All @@ -143,7 +144,8 @@ protected void initInteractions() {
});

getViewElement().setOnAction(DashBoardActions.SELL_SHARES, () -> {
if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())) {
if (Validator.NOT_EMPTY.isValid(getViewElement().getQuantityInputField().getText())
&& Float.parseFloat(getViewElement().getQuantityInputField().getText()) > 0) {
List<Transaction> transactions = exchange.sell(
new BigDecimal(getViewElement().getQuantityInputField().getText()),
getViewElement().getCurrentStock().getSymbol(),
Expand Down Expand Up @@ -190,6 +192,12 @@ protected void initInteractions() {
return null;
}));

getViewElement().getQuantityInputField().focusedProperty().addListener((observable, wasFocused, isNowFocused) -> {
if (!isNowFocused && getViewElement().getQuantityInputField().getText().trim().isEmpty()) {
getViewElement().getQuantityInputField().setText("1.0");
}
});

getViewElement().getSideBarSearchField().textProperty().addListener((observable, o, n) -> {
selectedFilter = n;
populateStockList(selectedFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public void setCurrentStock(final Stock stock, final float amountOwned) {
selectedStockLabel.setText(stock.getSymbol());
stockFullNameLabel.setText(stock.getCompany());
ownedStocks = amountOwned;
ownedQuantityLabel.setText("Owned: " + ownedStocks);
ownedQuantityLabel.setText("Owned: " + formatShares(ownedStocks));
}

/**
Expand All @@ -392,7 +392,7 @@ public void setCurrentStock(final Stock stock, final float amountOwned) {
* */
public void addOwnedShares(final float val) {
ownedStocks += val;
ownedQuantityLabel.setText("Owned: " + ownedStocks);
ownedQuantityLabel.setText("Owned: " + formatShares(ownedStocks));
}

/**
Expand Down Expand Up @@ -553,4 +553,14 @@ public TextField getQuantityInputField() {
public ComboBox<DashBoardTimeRange> getTimeRangeSelector() {
return timeRangeSelector;
}

/**
* Helper method for formatting owned shares (avoid precision showing in UI).
*
* @param amount the amount of shares to show.
* */
private String formatShares(final float amount) {
String formatted = String.format(java.util.Locale.US, "%.3f", amount);
return formatted.replaceAll("0+$", "").replaceAll("\\.$", "");
}
}

0 comments on commit 41f1125

Please sign in to comment.