Skip to content

Commit

Permalink
refactor: move common gameView functions to ViewUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed May 23, 2026
1 parent 8d704be commit 53ec0ce
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 63 deletions.
69 changes: 6 additions & 63 deletions src/main/java/millions/view/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,7 @@ private Tab createStocksTab() {
change -> change.getControlNewText().matches("-?\\d*") ? change : null));
quantitySlider.setPrefWidth(200);

ownedSharesBox.setPrefWidth(260);
ownedSharesBox.setCellFactory(
comboBox ->
new ListCell<>() {
@Override
protected void updateItem(Share share, boolean empty) {
super.updateItem(share, empty);
if (empty || share == null) {
setText(null);
setStyle("");
} else {
setText(formatOwnedShare(share));
setStyle(getProfitStyle(share));
}
}
});
ownedSharesBox.setButtonCell(
new ListCell<>() {
@Override
protected void updateItem(Share share, boolean empty) {
super.updateItem(share, empty);
if (empty || share == null) {
setText("Select lot");
setStyle("");
} else {
setText(formatOwnedShare(share));
setStyle(getProfitStyle(share));
}
}
});
ViewUtils.configureOwnedSharesBox(ownedSharesBox);

HBox sellBox = new HBox(8, sellButton, new VBox(4, new Label("Owned Shares"), ownedSharesBox));

Expand Down Expand Up @@ -178,7 +149,7 @@ protected void updateItem(Stock stock, boolean empty) {
if (empty || stock == null) {
setText(null);
} else {
setText(formatStock(stock));
setText(ViewUtils.formatStock(stock));
}
}
});
Expand Down Expand Up @@ -258,7 +229,7 @@ private void refreshQuantityControls(Stock stock) {
}

int maxBuyable = controller.getMaxBuyableQuantity(stock.getSymbol());
int current = clampQuantity(getQuantityValue(), Math.max(1, maxBuyable));
int current = ViewUtils.clampQuantity(getQuantityValue(), Math.max(1, maxBuyable));

updatingQuantityControls = true;
quantityField.setText(String.valueOf(current));
Expand Down Expand Up @@ -350,7 +321,7 @@ private void syncQuantityFromField(String newValue) {
}

updatingQuantityControls = true;
int clamped = clampQuantity(parseQuantity(newValue), getCurrentMaxBuyable());
int clamped = ViewUtils.clampQuantity(parseQuantity(newValue), getCurrentMaxBuyable());
quantityField.setText(String.valueOf(clamped));
quantitySlider.setValue(clamped);
updatingQuantityControls = false;
Expand All @@ -362,7 +333,7 @@ private void syncQuantityFromSlider(int newValue) {
}

updatingQuantityControls = true;
int clamped = clampQuantity(newValue, getCurrentMaxBuyable());
int clamped = ViewUtils.clampQuantity(newValue, getCurrentMaxBuyable());
quantityField.setText(String.valueOf(clamped));
quantitySlider.setValue(clamped);
updatingQuantityControls = false;
Expand All @@ -377,17 +348,6 @@ private int parseQuantity(String text) {
}
}

private int clampQuantity(int quantity, int maxBuyable) {
int upperBound = Math.max(1, maxBuyable);
if (quantity < 1) {
return 1;
}
if (quantity > upperBound) {
return upperBound;
}
return quantity;
}

private int getCurrentMaxBuyable() {
Stock selected = stocksList.getSelectionModel().getSelectedItem();
if (selected == null) {
Expand All @@ -396,25 +356,8 @@ private int getCurrentMaxBuyable() {
return Math.max(1, controller.getMaxBuyableQuantity(selected.getSymbol()));
}

private String formatOwnedShare(Share share) {
BigDecimal profit = getShareProfit(share);
String sign = profit.compareTo(BigDecimal.ZERO) >= 0 ? "+" : "";
return share.getQuantity() + "|" + share.getPurchasePrice() + "|" + sign + profit;
}

private String getProfitStyle(Share share) {
return getShareProfit(share).compareTo(BigDecimal.ZERO) >= 0
? "-fx-text-fill: green;"
: "-fx-text-fill: red;";
}

private BigDecimal getShareProfit(Share share) {
BigDecimal currentPrice = share.getStock().getSalesPrice();
return currentPrice.subtract(share.getPurchasePrice()).multiply(share.getQuantity());
}

private String formatStock(Stock stock) {
return stock.getSymbol() + " - " + stock.getCompany() + " (" + stock.getSalesPrice() + ")";
return ViewUtils.formatStock(stock);
}

// Listener callbacks update the shared header and the stocks tab.
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/millions/view/ViewUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package millions.view;

import java.math.BigDecimal;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import millions.model.Share;
import millions.model.Stock;

/** Small utility helpers for view formatting and simple calculations. */
public final class ViewUtils {

private ViewUtils() {}

public static void configureOwnedSharesBox(ComboBox<Share> comboBox) {
comboBox.setCellFactory(
box ->
new ListCell<>() {
@Override
protected void updateItem(Share share, boolean empty) {
super.updateItem(share, empty);
if (empty || share == null) {
setText(null);
setStyle("");
} else {
setText(formatOwnedShare(share));
setStyle(getProfitStyle(share));
}
}
});
comboBox.setButtonCell(
new ListCell<>() {
@Override
protected void updateItem(Share share, boolean empty) {
super.updateItem(share, empty);
if (empty || share == null) {
setText("Select lot");
setStyle("");
} else {
setText(formatOwnedShare(share));
setStyle(getProfitStyle(share));
}
}
});
}

public static String formatStock(Stock stock) {
return stock.getSymbol() + " - " + stock.getCompany() + " (" + stock.getSalesPrice() + ")";
}

public static String formatOwnedShare(Share share) {
BigDecimal profit = getShareProfit(share);
String sign = profit.compareTo(BigDecimal.ZERO) >= 0 ? "+" : "";
return share.getQuantity() + "|" + share.getPurchasePrice() + "|" + sign + profit;
}

public static String getProfitStyle(Share share) {
return getShareProfit(share).compareTo(BigDecimal.ZERO) >= 0
? "-fx-text-fill: green;"
: "-fx-text-fill: red;";
}

public static BigDecimal getShareProfit(Share share) {
BigDecimal currentPrice = share.getStock().getSalesPrice();
return currentPrice.subtract(share.getPurchasePrice()).multiply(share.getQuantity());
}

public static int clampQuantity(int quantity, int maxBuyable) {
int upperBound = Math.max(1, maxBuyable);
if (quantity < 1) {
return 1;
}
if (quantity > upperBound) {
return upperBound;
}
return quantity;
}
}

0 comments on commit 53ec0ce

Please sign in to comment.