-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move common gameView functions to ViewUtils
- Loading branch information
martin
committed
May 23, 2026
1 parent
8d704be
commit 53ec0ce
Showing
2 changed files
with
83 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |