diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java index 3053f88..841fbb4 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java @@ -1,5 +1,6 @@ package edu.ntnu.idi.idatt2003.g40.mappe; +import edu.ntnu.idi.idatt2003.g40.mappe.engine.Exchange; import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock; import edu.ntnu.idi.idatt2003.g40.mappe.service.FileConverter; import edu.ntnu.idi.idatt2003.g40.mappe.service.FileParser; @@ -17,6 +18,12 @@ import java.io.IOException; import java.util.List; import java.util.Objects; + +import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.financialsummary.SummaryController; +import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.financialsummary.SummaryView; +import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.stats.StatsView; +import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.topbar.TopBarController; +import edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.topbar.TopBarView; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; @@ -32,18 +39,6 @@ * */ public class Main extends Application { - static void main() { - FileParser parser1 = new FileParser("src/main/resources/dummydata.txt"); - FileConverter converter1 = new FileConverter(); - try { - List stocksInFile = converter1.getStocksFromStrings(parser1.readFile()); - parser1.writeStocksToFile(converter1.stocksToStrings(stocksInFile)); - stocksInFile.forEach(s -> System.out.println(s.getSymbol())); - } catch (IOException e) { - System.err.println(e.getMessage()); - } - } - /** * {@inheritDoc} * */ @@ -60,6 +55,14 @@ public void start(final Stage stage) throws Exception { EventManager eventManager = new EventManager(); ViewManager viewManager = new ViewManager(stage, eventManager); + List stocksInFile; + FileParser parser1 = new FileParser("src/main/resources/dummydata.txt"); + + FileConverter converter1 = new FileConverter(); + stocksInFile = converter1.getStocksFromStrings(parser1.readFile()); + + Exchange exchange = new Exchange("Exchange", stocksInFile); + // Main menu MainMenuView mainMenuView = new MainMenuView(); new MainMenuController(mainMenuView, eventManager); @@ -77,10 +80,21 @@ public void start(final Stage stage) throws Exception { SettingsView settingsView = new SettingsView(); new SettingsController(settingsView, eventManager); + // Summary section of the top bar + SummaryView summaryView = new SummaryView(); + new SummaryController(summaryView, eventManager, exchange); + + // Top bar + TopBarView topBarView = new TopBarView(summaryView); + new TopBarController(topBarView, eventManager); + + // Stats page + StatsView statsView = new StatsView(); + // In-game - InGameView inGameView = new InGameView(); + InGameView inGameView = new InGameView(topBarView, statsView.getRootPane()); - // Registrer alle views og start på hovedmenyen + // Register all views viewManager.addView(mainMenuView); viewManager.addView(playGameView); viewManager.addView(settingsView); diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java index 8eec48d..84f9611 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/engine/Exchange.java @@ -9,7 +9,13 @@ import edu.ntnu.idi.idatt2003.g40.mappe.service.PurchaseCalculator; import edu.ntnu.idi.idatt2003.g40.mappe.service.SaleCalculator; import edu.ntnu.idi.idatt2003.g40.mappe.service.TransactionCalculator; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventPublisher; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventType; import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator; +import javafx.beans.property.IntegerProperty; +import javafx.beans.property.SimpleIntegerProperty; import java.math.BigDecimal; import java.util.ArrayList; @@ -40,7 +46,7 @@ public final class Exchange { /** * Current week (set to 1 in constructor). * */ - private int week; + private final IntegerProperty week = new SimpleIntegerProperty(1); /** * Map of {@link Stock} objects. Key is stock symbol. Value is stock. @@ -65,7 +71,6 @@ public Exchange(final String name, final List stocks) throws IllegalArgum throw new IllegalArgumentException("Invalid exchange parameters!"); } this.name = name; - this.week = 1; this.stockMap = new HashMap<>(); this.random = new Random(); for (Stock stock : stocks) { @@ -88,9 +93,25 @@ public String getName() { * @return week. * */ public int getWeek() { + return week.get(); + } + + /** + * Getter method for the {@link IntegerProperty} object of week. + * + * @return week. + * */ + public IntegerProperty weekProperty() { return week; } + /** + * Advances the week. + * */ + public void nextWeek() { + week.set(week.get() + 1); + } + /** * Method for checking whether exchange has a stock. * @@ -159,8 +180,10 @@ public Transaction buy(final String symbol, Stock stock = stockMap.get(symbol); Share share = new Share(stock, quantity, stock.getSalesPrice()); TransactionCalculator calculator = new PurchaseCalculator(share); - player.withdrawMoney(calculator.calculateTotal()); - return new Purchase(share, week, calculator); + Purchase purchase = new Purchase(share, week.get(), calculator); + player.handleTransaction(purchase); + purchase.commit(player); + return purchase; } /** @@ -179,15 +202,17 @@ public Transaction sell(final Share share, final Player player) throw new IllegalArgumentException("Invalid sell!"); } TransactionCalculator calculator = new SaleCalculator(share); - player.addMoney(calculator.calculateTotal()); - return new Sale(share, week, calculator); + Sale sale = new Sale(share, week.get(), calculator); + player.handleTransaction(sale); + sale.commit(player); + return sale; } /** * Method for advancing time, increasing the amount of weeks. * */ public void advance() { - week++; + nextWeek(); for (Stock stock : stockMap.values()) { BigDecimal currentPrice = stock.getSalesPrice(); diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java index 971313e..4fc551d 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java @@ -149,4 +149,21 @@ public BigDecimal getNetWorth() { public PlayerStatus getStatus() { return PlayerStatusController.getStatus(this); } + + /** + * Method for handling a transaction for the player. + * + * @param transaction the transaction to handle. + * */ + public void handleTransaction(final Transaction transaction) { + transactionArchive.add(transaction); + + if (transaction instanceof Purchase purchase) { + withdrawMoney(purchase.getCalculator().calculateTotal()); + portfolio.addShare(purchase.getShare()); + } else if (transaction instanceof Sale sale) { + addMoney(sale.getCalculator().calculateTotal()); + portfolio.removeShare(sale.getShare()); + } + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewController.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewController.java index 61fb506..fed6cfa 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewController.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewController.java @@ -3,6 +3,7 @@ import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData; import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventPublisher; +import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventType; /** * Handles logic and event publishing for {@link ViewElement} objects. @@ -18,7 +19,7 @@ * @see EventManager * */ -public abstract class ViewController> +public abstract class ViewController> implements EventPublisher { /** @@ -26,6 +27,7 @@ public abstract class ViewController> * */ private final T1 viewElement; + /** * The {@link EventManager} object to send events to. * @@ -53,16 +55,6 @@ protected ViewController(final T1 viewElement, } } - /** - * Getter method for the event manager. - * - * @return the {@link EventManager} object. - * - */ - protected EventManager getEventManager() { - return eventManager; - } - /** * Getter method for the current view element. * @@ -79,6 +71,17 @@ public T1 getViewElement() { */ protected abstract void initInteractions(); + /** + * Basic method for invoking an event to change to a specific view. + * + * @param viewName the view to set to. + * */ + protected void changeScene(final ViewEnum viewName) { + ViewData viewData = new ViewData(viewName); + EventData eventData = new EventData<>(EventType.SCENE_CHANGE, viewData); + invoke(eventData, eventManager); + } + @Override public final void invoke(final EventData data, final EventManager eventManager) { diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewData.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewData.java index 33fca3c..5f66e88 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewData.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewData.java @@ -17,7 +17,7 @@ public class ViewData { * Name of the scene. * */ - private final String sceneName; + private final ViewEnum sceneName; /** * Constructor. @@ -26,8 +26,8 @@ public class ViewData { * * @throws IllegalArgumentException if sceneName is empty or null. */ - public ViewData(final String sceneName) throws IllegalArgumentException { - if (Validator.NOT_EMPTY.isValid(sceneName)) { + public ViewData(final ViewEnum sceneName) throws IllegalArgumentException { + if (Validator.NOT_EMPTY.isValid(sceneName.toString())) { this.sceneName = sceneName; } else { throw new IllegalArgumentException(Validator.NOT_EMPTY.getErrorMessage()); @@ -40,7 +40,7 @@ public ViewData(final String sceneName) throws IllegalArgumentException { * @return scene name. * */ - public String getSceneName() { + public ViewEnum getSceneName() { return sceneName; } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java index aa0f941..0463471 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.java @@ -1,7 +1,8 @@ package edu.ntnu.idi.idatt2003.g40.mappe.view; import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator; -import java.util.ArrayList; +import java.util.EnumMap; +import java.util.Map; import javafx.scene.control.Button; import javafx.scene.layout.Pane; @@ -12,15 +13,16 @@ *

Extends {@link Pane}

* * @param the type of pane this view element represents. + * @param an enum representing all interactable actions found in the view. * */ -public abstract class ViewElement { +public abstract class ViewElement> { /** * List of buttons in this view. * */ - private final ArrayList