diff --git a/src/main/java/millions/view/GameView.java b/src/main/java/millions/view/GameView.java index acfecfd..a893b04 100644 --- a/src/main/java/millions/view/GameView.java +++ b/src/main/java/millions/view/GameView.java @@ -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)); @@ -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)); } } }); @@ -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)); @@ -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; @@ -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; @@ -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) { @@ -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. diff --git a/src/main/java/millions/view/ViewUtils.java b/src/main/java/millions/view/ViewUtils.java new file mode 100644 index 0000000..1c8893c --- /dev/null +++ b/src/main/java/millions/view/ViewUtils.java @@ -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 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; + } +}