Skip to content

Commit

Permalink
adds a lot of styling, such as the bank and stock app, the login and …
Browse files Browse the repository at this point in the history
…homepage and the weather/clock functionality.
  • Loading branch information
peretr committed May 13, 2026
1 parent 90760bf commit 554297b
Show file tree
Hide file tree
Showing 25 changed files with 1,018 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.BankApp;
import javafx.application.Platform;

/**
* Controller for the {@link BankApp}.
Expand All @@ -19,16 +20,17 @@ public record BankAppController(BankApp bankApp, Player player) implements AppCo

@Override
public void nextTick() {
bankApp.updateStatus(
player.getNetWorth().doubleValue(),
player.getMoney().doubleValue(),
player.getPortfolio().getNetWorth().doubleValue(),
0
);
System.out.println("[DEBUG] BankAppController.nextTick() - Updating Status");
Platform.runLater(() -> {
bankApp.updateStatus(
player.getNetWorth().doubleValue(),
player.getMoney().doubleValue(),
player.getPortfolio().getNetWorth().doubleValue(),
player.getStartingMoney().doubleValue()
);

bankApp.getPortfolioList().getItems().setAll(
player.getPortfolio().getShares()
);
bankApp.getPortfolioList().getItems().setAll(
player.getPortfolio().getShares()
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public StockAppController(

stockApp.getSearchField().textProperty().addListener(
(obs, old, newValue) -> {
if (stockApp.getCurrentStock() != null) {
if (stockApp.getCurrentStock() != null && !newValue.isEmpty()) {
stockApp.openSearchPage();
}
stockApp.getStockList().getItems().setAll(
Expand All @@ -50,14 +50,12 @@ public StockAppController(

stockApp.getQuantitySpinner().valueProperty().addListener(
(obs, old, newValue) -> {
System.out.println("[DEBUG] Quantity changed: " + old + " -> " + newValue);
if (stockApp.getCurrentStock() != null) {
updateReceipt();
}
});

stockApp.getConfirmButton().setOnMouseClicked(event -> {
System.out.println("[DEBUG] Confirm button clicked");
stockApp.getConfirmButton().setOnAction(event -> {
handleTransaction();
});
}
Expand All @@ -68,14 +66,11 @@ public StockAppController(
private void handleTransaction() {
Stock currentStock = stockApp.getCurrentStock();
if (currentStock == null) {
System.out.println("[DEBUG] Transaction failed: No stock selected");
return;
}

int quantityValue = stockApp.getQuantitySpinner().getValue();
System.out.println("[DEBUG] Quantity spinner value: " + quantityValue);
if (quantityValue == 0) {
System.out.println("[DEBUG] Transaction skipped: Quantity is 0");
return;
}

Expand All @@ -95,8 +90,6 @@ private void handleTransaction() {
* @param quantity the amount to buy
*/
private void handleBuy(final Stock stock, final BigDecimal quantity) {
System.out.println("[DEBUG] Attempting to buy " + quantity + " of "
+ stock.getSymbol());
try {
Transaction transaction = marketController.getExchange().buy(
stock.getSymbol(), quantity, player
Expand All @@ -105,13 +98,8 @@ private void handleBuy(final Stock stock, final BigDecimal quantity) {
if (transaction != null) {
transaction.commit(player);
player.getPortfolio().addShare(transaction.getShare());
System.out.println("[DEBUG] Buy successful");
} else {
System.out.println("[DEBUG] Buy failed: transaction is null");
}
} catch (Exception exception) {
System.out.println("[DEBUG] Buy failed with exception: "
+ exception.getMessage());
exception.printStackTrace();
}
}
Expand All @@ -123,16 +111,11 @@ private void handleBuy(final Stock stock, final BigDecimal quantity) {
* @param quantity the amount to sell
*/
private void handleSell(final Stock stock, final BigDecimal quantity) {
System.out.println("[DEBUG] Attempting to sell " + quantity + " of "
+ stock.getSymbol());
player.getPortfolio().getShares().stream()
.filter(s -> s.getStock().getSymbol().equals(stock.getSymbol()))
.findFirst()
.ifPresentOrElse(existingShare -> {
.ifPresent(existingShare -> {
if (existingShare.getQuantity().compareTo(quantity) < 0) {
System.out.println("[DEBUG] Sell failed: Not enough shares owned. "
+ "Have: " + existingShare.getQuantity() + ", Need: "
+ quantity);
return;
}

Expand All @@ -142,12 +125,8 @@ private void handleSell(final Stock stock, final BigDecimal quantity) {
if (transaction != null) {
transaction.commit(player);
player.getPortfolio().removeShare(transaction.getShare());
System.out.println("[DEBUG] Sell successful");
} else {
System.out.println("[DEBUG] Sell failed: transaction is null");
}
}, () -> System.out.println("[DEBUG] Sell failed: No shares of "
+ stock.getSymbol() + " owned"));
});
}

/**
Expand All @@ -171,12 +150,10 @@ private void updateReceipt() {
*/
@Override
public void nextTick() {
System.out.println("[DEBUG] StockAppController.nextTick() triggered");
marketController.updateMarket();
Platform.runLater(() -> stockApp.getStockList().refresh());
Stock currentStockInView = stockApp.getCurrentStock();
if (currentStockInView != null) {
System.out.println("[DEBUG] Updating UI for stock: "
+ currentStockInView.getSymbol());
stockApp.updatePriceLabel(currentStockInView);
updateReceipt();
// Graph is updated within marketController.updateMarket() if visible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,28 @@ public void addAppController(final AppController appController) {
* Starts the game simulation timer.
*/
public void startGame() {
timer = new Timer();
timer = new Timer(true); // Set as daemon thread to allow JVM to exit

final int delay = 0;
final int period = 1000;

timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("[DEBUG] Game Tick Started");
for (AppController controller : appControllers) {
controller.nextTick();
}
System.out.println("[DEBUG] Game Tick Finished");
}
}, delay, period);
}

/**
* Stops the game simulation timer.
*/
public void stopGame() {
if (timer != null) {
timer.cancel();
timer.purge();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public List<Stock> getMarket(final String searchTerm) {
* Updates the market by updating all stock prices and graphs.
*/
public void updateMarket() {
System.out.println("[DEBUG] MarketController.updateMarket() "
+ "- Updating prices for "
+ exchange.getAllStocks().size() + " stocks");
for (Stock stock : exchange.getAllStocks()) {
stock.updatePrice();
stock.updateStockGraph();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public boolean show(final App type) {
.findFirst()
.ifPresent(popup -> {
popup.getRoot().setVisible(true);
popup.getRoot().autosize();
bringToFront(popup);
});
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public DesktopViewController(
gameController.addAppController(new AppStoreController(appStoreApp));

HustlersApp hustlersApp = new HustlersApp();

StockApp stockApp = new StockApp();
StockApp stockApp = new StockApp(player);
MarketController marketController = new MarketController();
gameController.addAppController(
new StockAppController(stockApp, marketController, player)
Expand All @@ -84,6 +84,12 @@ public DesktopViewController(
popups.add(bankApp);

this.popupController = new PopupController(popups);

bankApp.setOnShareSelected(share -> {
popupController.show(App.STOCK);
stockApp.openStockPage(share.getStock());
});

this.desktopView = new DesktopView(this);

this.desktopView.getRoot().getChildren().addAll(
Expand Down Expand Up @@ -126,7 +132,10 @@ public DesktopView getDesktopView() {
* @return the app button.
*/
public Button createAppButton(final App type) {
Button button = new Button(type.toString());
String buttonText = type.toString().substring(0, 1).toUpperCase()
+ type.toString().substring(1).toLowerCase();
Button button = new Button(buttonText);
button.getStyleClass().add("app-button");
button.setMinSize(0, 0);

final double sizeMultiplier = 0.8;
Expand All @@ -147,7 +156,6 @@ public Button createAppButton(final App type) {

// Print when clicked and open popup
button.setOnAction(event -> {
System.out.println("button pressed: " + type);
openPopup(type);
});

Expand All @@ -170,7 +178,6 @@ public Button createAppButton(final App type) {
* Handles the settings button action.
*/
public void handleSettings() {
System.out.println("Settings button pressed");
// Placeholder for settings logic
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ private void handleStart() {
}

String resolvedName = resolveUsername(username);
Difficulty difficulty = Difficulty.fromString(startView.getSelectedMode());
String selectedMode = startView.getSelectedMode();
Difficulty difficulty = Difficulty.EASY;
if (selectedMode != null) {
if (selectedMode.startsWith("Medium")) {
difficulty = Difficulty.MEDIUM;
} else if (selectedMode.startsWith("Hard")) {
difficulty = Difficulty.HARD;
}
}

application.initGame(resolvedName, difficulty);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public void switchToDesktopView() {
stage.setScene(scene);
}

@Override
public void stop() {
if (gameController != null) {
gameController.stopGame();
}
}

public static void main(String[] args) {
launch(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,10 @@ public Transaction buy(
final BigDecimal quantity,
final Player player
) {
System.out.println("[DEBUG] Exchange" + " Buying " + quantity + " of "
+ symbol + " for player " + player.getName());
Stock stock = getStock(symbol);
BigDecimal purchasePrice = stock.getSalesPrice();

Share share = new Share(stock, quantity, purchasePrice);
System.out.println("[DEBUG] Purchase complete: " + quantity
+ " shares of " + symbol + " at " + purchasePrice);
return new Purchase(share, week);
}

Expand All @@ -132,14 +128,9 @@ public Transaction sell(
final BigDecimal quantity,
final Player player
) {
System.out.println("[DEBUG] Exchange " + " - Selling " + quantity
+ " shares of " + share.getStock().getSymbol()
+ " for player " + player.getName());
Share shareToSell = new Share(
share.getStock(), quantity, share.getPurchasePrice()
);
System.out.println("[DEBUG] Sale complete: Prepared " + quantity
+ " shares for transaction");
return new Sale(shareToSell, week);
}

Expand Down
12 changes: 4 additions & 8 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public Player(final String name, final BigDecimal startingMoney) {
this.transactionArchive = new TransactionArchive();
}

public BigDecimal getStartingMoney() {
return startingMoney;
}

public String getName() {
return name;
}
Expand All @@ -55,10 +59,7 @@ public BigDecimal getMoney() {
* @param amount the amount to add
*/
public void addMoney(final BigDecimal amount) {
BigDecimal oldMoney = money;
money = money.add(amount);
System.out.println("[DEBUG] Player " + name + " money added: "
+ oldMoney + " -> " + money + " (+" + amount + ")");
}

/**
Expand All @@ -69,14 +70,9 @@ public void addMoney(final BigDecimal amount) {
*/
public void withdrawMoney(final BigDecimal amount) {
if (money.compareTo(amount) < 0) {
System.out.println("[DEBUG] Player " + name + " withdrawal FAILED: "
+ "Insufficient funds (Needs: " + amount + ", Has: " + money + ")");
throw new InsufficientFunds("Not enough money to withdraw " + amount);
}
BigDecimal oldMoney = money;
money = money.subtract(amount);
System.out.println("[DEBUG] Player " + name + " money withdrawn: "
+ oldMoney + " -> " + money + " (-" + amount + ")");
}

/**
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Portfolio.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public boolean removeShare(final Share newShare) {
.findFirst();

if (match.isEmpty()) {
System.out.println("[DEBUG] Portfolio - Did not find share: "
+ newShare);
return false;
}

Expand All @@ -93,24 +91,18 @@ public boolean removeShare(final Share newShare) {

if (cmp < 0) {
// Trying to sell more than owned
System.out.println("[DEBUG] Portfolio - Cannot sell "
+ newShare.getQuantity()
+ " of " + newShare.getStock().getSymbol()
+ ", only " + existing.getQuantity() + " owned");
return false;
} else if (cmp == 0) {
// Selling entire position — remove from list
System.out.println("[DEBUG] Portfolio - Fully closed position: "
+ existing);
return shares.remove(existing);
} else {
// Partial sale — update quantity in place
existing.setQuantity(remainingQuantity);
System.out.println("[DEBUG] Portfolio - Partial sale: "
+ newShare.getQuantity()
+ " sold, " + remainingQuantity + " remaining of "
+ existing.getStock().getSymbol());
return true;
// Partial sale — replace with new instance to ensure UI update
shares.remove(existing);
return shares.add(new Share(
existing.getStock(),
remainingQuantity,
existing.getPurchasePrice()
));
}
}

Expand Down
Loading

0 comments on commit 554297b

Please sign in to comment.