Skip to content
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
42 changes: 28 additions & 14 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<Stock> 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}
* */
Expand All @@ -60,6 +55,14 @@ public void start(final Stage stage) throws Exception {
EventManager eventManager = new EventManager();
ViewManager viewManager = new ViewManager(stage, eventManager);

List<Stock> 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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -65,7 +71,6 @@ public Exchange(final String name, final List<Stock> 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) {
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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();
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,14 +19,15 @@
* @see EventManager
*
*/
public abstract class ViewController<T1 extends ViewElement<?>>
public abstract class ViewController<T1 extends ViewElement<?, ?>>
implements EventPublisher {

/**
* The {@link ViewElement} object this controller is associated with.
*
*/
private final T1 viewElement;

/**
* The {@link EventManager} object to send events to.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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<ViewData> eventData = new EventData<>(EventType.SCENE_CHANGE, viewData);
invoke(eventData, eventManager);
}

@Override
public final <T> void invoke(final EventData<T> data,
final EventManager eventManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ViewData {
* Name of the scene.
*
*/
private final String sceneName;
private final ViewEnum sceneName;

/**
* Constructor.
Expand All @@ -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());
Expand All @@ -40,7 +40,7 @@ public ViewData(final String sceneName) throws IllegalArgumentException {
* @return scene name.
*
*/
public String getSceneName() {
public ViewEnum getSceneName() {
return sceneName;
}
}
Loading