Skip to content

Commit

Permalink
Merge pull request #91 from einaskoi/per/style
Browse files Browse the repository at this point in the history
the great styling
  • Loading branch information
einaskoi authored May 13, 2026
2 parents a0cf2be + 774cb9d commit 2430c27
Show file tree
Hide file tree
Showing 35 changed files with 1,333 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@

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}.
* Handles updating the player's financial status and portfolio view.
*/
public final class BankAppController implements AppController {
private final Player player;
private final BankApp bankApp;

public record BankAppController(BankApp bankApp, Player player) implements AppController {
/**
* Constructs a new BankAppController.
*
* @param bankApp the bank app view
* @param player the player model
*/
public BankAppController(final BankApp bankApp, final Player player) {
this.player = player;
this.bankApp = bankApp;
public BankAppController {
}

@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,14 +98,9 @@ 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 e) {
System.out.println("[DEBUG] Buy failed with exception: "
+ e.getMessage());
e.printStackTrace();
} catch (Exception exception) {
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 @@ -12,11 +12,8 @@
* Controller for managing the game state and timing.
*/
public final class GameController {
/** The player model. */
private final Player player;
/** List of application controllers. */
private final List<AppController> appControllers = new ArrayList<>();
/** Timer for game ticks. */
private Timer timer;

/**
Expand All @@ -41,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 @@ -4,10 +4,9 @@
import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;
import edu.ntnu.idi.idatt2003.gruppe42.Model.StockFileHandler;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp;
import javafx.application.Platform;

import java.nio.file.Path;
import java.util.List;
import java.util.Objects;

/**
* Controller for the stock market.
Expand All @@ -25,14 +24,14 @@ public MarketController() {
this.stockResolution = 50;

try {
Path path = Path.of(getClass().getClassLoader()
.getResource("stocks.csv").toURI());
Path path = Path.of(Objects.requireNonNull(getClass().getClassLoader()
.getResource("stocks.csv")).toURI());
exchange = new Exchange(StockFileHandler.readFromFile(path));

startMarket();
System.out.println("Market loaded");

} catch (Exception e) {
} catch (Exception exception) {
System.out.println("File not found");
}
}
Expand All @@ -55,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
@@ -0,0 +1,87 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.AppController;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/**
* Controller for managing game time and weather.
* Advances 1 hour per tick.
*/
public class TimeAndWeatherController implements AppController {
private final IntegerProperty hour = new SimpleIntegerProperty(0);
private final IntegerProperty dayIndex = new SimpleIntegerProperty(0); // 0 = Sunday, 1 = Monday, etc.
private final IntegerProperty temperature = new SimpleIntegerProperty(15);
private final StringProperty weather = new SimpleStringProperty("Sunny");
private final Random random = new Random();

private static final String[] DAYS = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

public TimeAndWeatherController() {
updateWeather();
}

@Override
public void nextTick() {
int nextHour = hour.get() + 1;
if (nextHour >= 24) {
hour.set(0);
dayIndex.set((dayIndex.get() + 1) % 7);
updateWeather();
} else {
hour.set(nextHour);
}
}

private void updateWeather() {
// Basic seasonal/random temperature logic
// Range from -10 to 30
int change = random.nextInt(11) - 5; // -5 to +5
int newTemp = temperature.get() + change;
if (newTemp < -10) newTemp = -10;
if (newTemp > 30) newTemp = 30;
temperature.set(newTemp);

if (newTemp <= 0) {
weather.set("Snow");
} else {
// If warm, could be sunny or rain
if (random.nextBoolean()) {
weather.set("Sunny");
} else {
weather.set("Rain");
}
}
}

public IntegerProperty hourProperty() {
return hour;
}

public IntegerProperty dayIndexProperty() {
return dayIndex;
}

public IntegerProperty temperatureProperty() {
return temperature;
}

public StringProperty weatherProperty() {
return weather;
}

public String getTimeString() {
return String.format("%02d:00", hour.get());
}

public String getDayOfWeekString() {
return DAYS[dayIndex.get()];
}
}
Loading

0 comments on commit 2430c27

Please sign in to comment.