Skip to content

refactor the view to controller relationships to allow player to be d… #87

Merged
merged 1 commit into from
May 11, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers;

public interface AppController {
void nextTick();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers;

import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.AppStoreApp;

public class AppStoreController implements AppController {
public AppStoreController(AppStoreApp appStoreApp) {
}

@Override
public void nextTick() {
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers;

public class BankAppController {
import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.BankApp;

public class BankAppController implements AppController{
private Player player;
private BankApp bankApp;

public BankAppController(BankApp bankApp, Player player) {
this.player = player;
this.bankApp = bankApp;
}
@Override
public void nextTick() {
System.out.println("[DEBUG] BankAppController.nextTick() - Updating Player Status");
bankApp.updateStatus(
player.getNetWorth().doubleValue(),
player.getMoney().doubleValue(),
player.getPortfolio().getNetWorth().doubleValue(),
0 // TODO: calculate growth
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp;

public class StockAppController {
public class StockAppController implements AppController {
private MarketController marketController;
private StockApp stockPopup;

public StockAppController(StockApp stockPopup) {
marketController = new MarketController(stockPopup);
public StockAppController(StockApp stockPopup, MarketController marketController) {
this.stockPopup = stockPopup;
this.marketController = marketController;

stockPopup.getStockList().getItems().addAll(marketController.getMarket());

Expand All @@ -16,4 +18,14 @@ public StockAppController(StockApp stockPopup) {
});

}

@Override
public void nextTick(){
System.out.println("[DEBUG] StockAppController.nextTick() - Updating Market");
marketController.updateMarket();
if (stockPopup.getCurrentStock() != null) {
System.out.println("[DEBUG] Updating Price Label for: " + stockPopup.getCurrentStock().getCompany());
stockPopup.updatePriceLabel(stockPopup.getCurrentStock());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class GameController {

private Player player;
private List<AppController> appControllers = new ArrayList<>();
private Timer timer;

public GameController(Player player) {
this.player = player;
}

public void addAppController(AppController appController) {
appControllers.add(appController);
}

public void startGame() {
timer = new Timer();

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");
}
}, 0, 1000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public class MarketController {
private int stockResolution;
private StockApp stockApp;

public MarketController(StockApp stockApp) {
this.stockApp = stockApp;
public MarketController() {
stockResolution = 50;

try {
Expand All @@ -43,26 +42,19 @@ public List<Stock> getMarket(String searchTerm) {
}

public void updateMarket() {
System.out.println("[DEBUG] MarketController.updateMarket() - Updating prices for " + market.size() + " stocks");
for (Stock stock : market) {
stock.updatePrice();
if (stock.getStockGraph().getVisibility()) {
stock.updateStockGraph();
stockApp.updatePriceLabel(stock);
}
}
}

public void startMarket() {
Timer timer = new Timer();
for (int i = 0; i < stockResolution; i++) {
updateMarket();
}
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Platform.runLater(() -> updateMarket());
}
}, 0, 1000);
}

public static void printMarket(List<Stock> stocks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@ public class PopupController {
/**
* Constructs a new popup controller.
*/
public PopupController() {
popups = new ArrayList<>();
popups.add(new AppStoreApp(400, 300, 100, 100));
popups.add(new HustlersApp(400, 300, 140, 140));
popups.add(new StockApp(500, 400, 200, 200));
popups.add(new MailApp(400, 300, 160, 160));
popups.add(new NewsApp(400, 300, 180, 180));
popups.add(new BankApp(400, 300, 120, 120));
public PopupController(List<Popup> popups) {
this.popups = popups;
}

//TODO: javadoc
Expand Down Expand Up @@ -64,4 +58,8 @@ public void bringToFront(Popup popup) {
public List<Popup> getPopups() {
return popups;
}

public Popup getPopup(App type) {
return popups.stream().filter(popup -> popup.getType().equals(type)).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppStoreController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.BankAppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.StockAppController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.GameController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.MarketController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.PopupController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.App;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.AppStoreApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.BankApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.HustlersApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.MailApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.NewsApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popup;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp;
import edu.ntnu.idi.idatt2003.gruppe42.View.Popup;
import edu.ntnu.idi.idatt2003.gruppe42.View.Views.DesktopView;
import java.util.ArrayList;
import java.util.List;
import javafx.beans.binding.Bindings;
import javafx.scene.control.Button;
import javafx.scene.input.ClipboardContent;
Expand All @@ -19,23 +28,55 @@
import javafx.scene.layout.StackPane;

/**
* Controller for the app buttons.
* Controller for the desktop.
* Handles app button creation, resizing, click events, and drag-and-drop.
*/
public class DesktopViewController {
private final PopupController popupController;
private final Pane parent;
private final DesktopView desktopView;

/**
* Constructs a new app controller.
* Constructs a new desktop view controller.
*
* @param popupController the popup controller to manage popups.
* @param parent the parent pane to add popups to.
* @param player the player model.
* @param gameController the game controller to register app controllers.
*/
public DesktopViewController(PopupController popupController, Pane parent) {
this.popupController = popupController;
this.parent = parent;
parent.getChildren().setAll(popupController.getPopups().stream().map(Popup::getRoot).toArray(Pane[]::new));
public DesktopViewController(Player player, GameController gameController) {
List<Popup> popups = new ArrayList<>();

AppStoreApp appStoreApp = new AppStoreApp(400, 300, 100, 100);
gameController.addAppController(new AppStoreController(appStoreApp));

HustlersApp hustlersApp = new HustlersApp(400, 300, 140, 140);

StockApp stockApp = new StockApp(400, 300, 120, 120);
MarketController marketController = new MarketController();
gameController.addAppController(new StockAppController(stockApp, marketController));

MailApp mailApp = new MailApp(400, 300, 160, 160);

NewsApp newsApp = new NewsApp(400, 300, 180, 180);

BankApp bankApp = new BankApp();
gameController.addAppController(new BankAppController(bankApp, player));

popups.add(appStoreApp);
popups.add(hustlersApp);
popups.add(stockApp);
popups.add(mailApp);
popups.add(newsApp);
popups.add(bankApp);

this.popupController = new PopupController(popups);
this.desktopView = new DesktopView(this);

this.desktopView.getRoot().getChildren().addAll(
popupController.getPopups().stream().map(Popup::getRoot).toArray(Pane[]::new)
);
}

public DesktopView getDesktopView() {
return desktopView;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import edu.ntnu.idi.idatt2003.gruppe42.Model.Difficulty;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;
import edu.ntnu.idi.idatt2003.gruppe42.View.Views.StartView;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import javafx.scene.control.Alert;
Expand Down Expand Up @@ -49,7 +48,6 @@ private void handleStart() {
Difficulty difficulty = Difficulty.fromString(startView.getSelectedMode());

application.initGame(resolvedName, difficulty);
application.switchToDesktopView();
}

private String resolveUsername(String input) {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Millions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.GameController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.DesktopViewController;
import edu.ntnu.idi.idatt2003.gruppe42.Controller.ViewControllers.StartViewController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Difficulty;
import edu.ntnu.idi.idatt2003.gruppe42.Model.GameFactory;
Expand All @@ -16,6 +18,7 @@ public class Millions extends Application {
private Stage stage;
private DesktopView desktopView;
private Player player;
private GameController gameController;

@Override
public void start(Stage stage) throws Exception {
Expand All @@ -35,6 +38,9 @@ public void start(Stage stage) throws Exception {

public void initGame(String username, Difficulty difficulty) {
player = GameFactory.createPlayer(username, difficulty);
gameController = new GameController(player);
gameController.startGame();
switchToDesktopView();
}

public void switchToStartView() {
Expand All @@ -44,7 +50,8 @@ public void switchToStartView() {
}

public void switchToDesktopView() {
desktopView = new DesktopView();
DesktopViewController desktopViewController = new DesktopViewController(player, gameController);
desktopView = desktopViewController.getDesktopView();
scene.setRoot(desktopView.getRoot());
stage.setScene(scene);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ public List<Stock> findStocks(String searchTerm) {
* @return a {@code Purchase} transaction representing the purchase
*/
public Transaction buy(String symbol, BigDecimal quantity, Player player) {

System.out.println("[DEBUG] Exchange " + name + " - Buying " + quantity + " of " + symbol + " for player " + player.getName());
player.withdrawMoney(quantity);
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 @@ -117,8 +118,11 @@ public Transaction buy(String symbol, BigDecimal quantity, Player player) {
* @return a sale transaction representing the sale
*/
public Transaction sell(Share share, Player player) {
System.out.println("[DEBUG] Exchange " + name + " - Selling shares of " + share.getStock().getSymbol() + " for player " + player.getName());
SaleCalculator saleCalculator = new SaleCalculator(share);
player.addMoney(saleCalculator.calculateTotal());
BigDecimal totalValue = saleCalculator.calculateTotal();
player.addMoney(totalValue);
System.out.println("[DEBUG] Sale complete: Received " + totalValue);
return new Sale(share, week);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public BigDecimal getMoney() {
* @param amount the amount to add to the player's account
*/
public void addMoney(BigDecimal amount) {
BigDecimal oldMoney = money;
money = money.add(amount);
System.out.println("[DEBUG] Player " + name + " money added: " + oldMoney + " -> " + money + " (+" + amount + ")");
}

/**
Expand All @@ -65,9 +67,12 @@ public void addMoney(BigDecimal amount) {
*/
public void withdrawMoney(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
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ public BigDecimal getLatestPriceChange() {
}

public void updatePrice() {
BigDecimal oldPrice = getSalesPrice();
BigDecimal newPrice = getSalesPrice().subtract(new BigDecimal("1"));
newPrice = newPrice.max(BigDecimal.ZERO);
prices.add(newPrice);
System.out.println("[DEBUG] Stock " + symbol + " updated: " + oldPrice + " -> " + newPrice);
}
public StockGraph getStockGraph() {
return stockGraph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public AppStoreApp(int width, int height, int x, int y) {
super(width, height, x, y, App.APPSTORE);
// Add AppStore specific content here
}

}
Loading