Skip to content

Backend refactoring #48

Merged
merged 15 commits into from
May 13, 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
11 changes: 11 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/model/Exchange.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package edu.ntnu.idi.idatt.model;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;

import edu.ntnu.idi.idatt.model.market.Stock;
Expand All @@ -9,6 +11,7 @@
import edu.ntnu.idi.idatt.model.transaction.Purchase;
import edu.ntnu.idi.idatt.model.transaction.Sale;
import edu.ntnu.idi.idatt.model.transaction.Transaction;
import edu.ntnu.idi.idatt.session.UserSession;

/**
* Exchange class
Expand Down Expand Up @@ -194,6 +197,14 @@ public Transaction sell(Share share, Player player) {
* @see Stock
*/
public void advance() {
this.week += 1;
Random random = new Random();
stockMap.values()
.forEach(s -> s.addNewSalesPrice(s.getSalesPrice()
.multiply(BigDecimal.valueOf(random.nextDouble(0.8, 1.4)).setScale(2, RoundingMode.HALF_UP))));
UserSession.getInstance().netWorthProperty().set(
UserSession.getInstance().getPlayer().getNetWorth().doubleValue()); // TODO: Remove hook from here, just for
// testing
}

}
10 changes: 8 additions & 2 deletions src/main/java/edu/ntnu/idi/idatt/model/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ public TransactionArchive getTransactionArchive() {
public void addMoney(BigDecimal amount) {
this.money = this.money.add(amount);
UserSession.getInstance().moneyProperty().set(this.money.doubleValue()); // Workaround instead of PropertyChange due
// to JSON saving.
// to JSON saving
UserSession.getInstance().netWorthProperty().set(this.getNetWorth().doubleValue()); // Portfolio doesnt change
// without monetary change
// (either buy or sale) so thats
// the decision to hook net
// worth here and under.
}

public void withdrawMoney(BigDecimal amount) {
this.money = this.money.subtract(amount);
UserSession.getInstance().moneyProperty().set(this.money.doubleValue()); // Hook
UserSession.getInstance().moneyProperty().set(this.money.doubleValue()); // Hooks
UserSession.getInstance().netWorthProperty().set(this.getNetWorth().doubleValue());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public List<Transaction> getTransactions(int week) {
return transactions.stream().filter(transaction -> transaction.getWeek() == week).toList();
}

// TODO: java, junit
public List<Transaction> getTransactions() {
return transactions;
}

/**
* Getter for purchases done
*
Expand All @@ -56,6 +61,13 @@ public List<Purchase> getPurchases(int week) {
.toList();
}

// TODO: java, junit
public List<Purchase> getPurchases() {
return getTransactions().stream().filter(t -> t instanceof Purchase)
.map(t -> (Purchase) t)
.toList();
}

/**
* Getter for sales done
*
Expand All @@ -68,6 +80,13 @@ public List<Sale> getSales(int week) {
.toList();
}

// TODO: java, junit
public List<Sale> getSales() {
return getTransactions().stream().filter(t -> t instanceof Sale)
.map(t -> (Sale) t)
.toList();
}

/**
* Part 2
*
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/edu/ntnu/idi/idatt/session/UserSession.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package edu.ntnu.idi.idatt.session;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import edu.ntnu.idi.idatt.model.Exchange;
import edu.ntnu.idi.idatt.model.player.Player;
import javafx.beans.property.SimpleDoubleProperty;
Expand All @@ -25,15 +22,14 @@ public static UserSession getInstance() {

private Player player;
private Exchange exchange;
private final SimpleDoubleProperty moneyProperty = new SimpleDoubleProperty();

public Player getPlayer() {
return player;
}

public void setPlayer(Player player) {
this.player = player;
moneyProperty.set(player.getMoney().doubleValue());
moneyProperty.set(player.getMoney().doubleValue()); // Startup hook
}

public Exchange getExchange() {
Expand All @@ -44,14 +40,21 @@ public void setExchange(Exchange exchange) {
this.exchange = exchange;
}

public SessionBundle getSession() {
return new SessionBundle(player, exchange);
}
private final SimpleDoubleProperty moneyProperty = new SimpleDoubleProperty();
private final SimpleDoubleProperty netWorthProperty = new SimpleDoubleProperty();

public SimpleDoubleProperty moneyProperty() {
return moneyProperty;
}

public SimpleDoubleProperty netWorthProperty() {
return netWorthProperty;
}

public SessionBundle getSession() {
return new SessionBundle(player, exchange);
}

public class SessionBundle {

private Player player;
Expand Down
46 changes: 45 additions & 1 deletion src/main/java/edu/ntnu/idi/idatt/view/SceneFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package edu.ntnu.idi.idatt.view;

import java.util.ArrayDeque;
import java.util.Deque;

import edu.ntnu.idi.idatt.model.market.Stock;
import edu.ntnu.idi.idatt.session.UserSession;
import edu.ntnu.idi.idatt.view.entry.StartController;
import edu.ntnu.idi.idatt.view.entry.StartModel;
import edu.ntnu.idi.idatt.view.entry.StartView;
Expand All @@ -17,6 +21,39 @@

public class SceneFactory {

@FunctionalInterface
public interface MVCInitializer {
Parent execute();
}

private static Deque<MVCInitializer> navigation = new ArrayDeque<>();
private static boolean navigatingBack = false;

public static void goBack() {
if (navigation.size() > 1) {
navigation.pop();
navigatingBack = true;
SceneManager.switchTo(navigation.peek().execute());
navigatingBack = false;
}
}

public static void reloadCurrent() {
navigatingBack = true;
SceneManager.switchTo(navigation.peek().execute());
navigatingBack = false;
}

private static void mark(MVCInitializer initializer) {
if (!navigatingBack) {
navigation.push(initializer);
}
}

public static boolean isFinal() {
return navigation.size() == 1;
}

public static Parent createStartView() {

StartModel model = new StartModel();
Expand All @@ -32,6 +69,8 @@ public static Parent createStartView() {

public static Parent createExchangeView() {

mark(() -> createExchangeView());

ExchangeModel model = new ExchangeModel();
ExchangeView view = new ExchangeView();
ExchangeController controller = new ExchangeController(model);
Expand All @@ -43,7 +82,10 @@ public static Parent createExchangeView() {

}

public static Parent createStockView(Stock stock) {
public static Parent createStockView(String symbol) {

mark(() -> createStockView(symbol));
Stock stock = UserSession.getInstance().getExchange().getStock(symbol);

StockModel model = new StockModel();
StockView view = new StockView();
Expand All @@ -57,6 +99,8 @@ public static Parent createStockView(Stock stock) {

public static Parent createTransactionView() {

mark(() -> createTransactionView());

TransactionModel model = new TransactionModel();
TransactionView view = new TransactionView();
TransactionController controller = new TransactionController(model);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.ntnu.idi.idatt.view.components.elements;

import java.math.BigDecimal;
import java.util.List;
import java.util.function.Consumer;

Expand All @@ -25,13 +24,13 @@ public StockComponent(Stock stock) {

this.setStyle("-fx-background-color: #404950;");

Label title = new Label(this.getTitle());
Label title = new Label(String.format("%s (%s)", stock.getCompany(), stock.getSymbol()));

Label latestText = new Label("Latest");
Label latestValue = new Label(stock.getSalesPrice().toString() + "USD");
Label latestValue = new Label(String.format("%.2f USD", stock.getSalesPrice()));

Label changeText = new Label("Change");
Label changeValue = new Label(stock.getLatestPriceChange().toString() + "USD");
Label changeValue = new Label(String.format("%.2f USD", stock.getLatestPriceChange()));
Label changeValuePercent = new Label(
String.valueOf(stock.getLatestPriceChangePercent()) + "%");

Expand Down Expand Up @@ -63,16 +62,8 @@ public StockComponent(Stock stock) {
this.getChildren().addAll(stockComponent.makeUI());
}

public String getTitle() {
return stock.getCompany() + " (" + stock.getSymbol() + ")";
}

public String getSymbol() {
return stock.getSymbol();
}

public void onStockClick(Consumer<String> handler) {
this.setOnMouseClicked((e) -> handler.accept(this.getSymbol()));
this.setOnMouseClicked((e) -> handler.accept(stock.getSymbol()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import edu.ntnu.idi.idatt.model.transaction.Purchase;
import edu.ntnu.idi.idatt.model.transaction.Sale;
import edu.ntnu.idi.idatt.model.transaction.Transaction;
import edu.ntnu.idi.idatt.service.transaction.PurchaseCalculator;
import edu.ntnu.idi.idatt.service.transaction.SaleCalculator;
import edu.ntnu.idi.idatt.service.transaction.TransactionCalculator;
import edu.ntnu.idi.idatt.view.components.ui.UICompositor;
import edu.ntnu.idi.idatt.view.util.CssUtils;
import javafx.geometry.Insets;
Expand All @@ -29,31 +29,27 @@ public TransactionComponent(Transaction transaction) {
Label amountOfShares;
Label saleProfit;

TransactionCalculator calculator = transaction.getCalculator();
totalValue = new Label(String.format("Total: %.2f USD", calculator.calculateTotal()));
taxComissionValue = new Label(String.format(
"Tax & Comission: %.2f USD", calculator.calculateTax().add(calculator.calculateCommision())));
grossValue = new Label(String.format("Gross: %.2f USD", calculator.calculateGross()));
amountOfShares = new Label(
String.format("Shares: %.2f [%s]", transaction.getShare().getQuantity(), stock.getSymbol()));

if (transaction instanceof Purchase) {
title = new Label("PURCHASE");
CssUtils.apply(title, CssUtils.GREEN);

PurchaseCalculator calculator = (PurchaseCalculator) transaction.getCalculator();
totalValue = new Label("Total: " + calculator.calculateTotal() + " USD");
taxComissionValue = new Label(
"Tax & Comission: " + calculator.calculateTax().add(calculator.calculateCommision()) + " USD");
grossValue = new Label("Gross: " + calculator.calculateGross() + " USD");
amountOfShares = new Label("Shares: " + transaction.getShare().getQuantity() + " [" + stock.getSymbol() + "]");
saleProfit = new Label();
CssUtils.apply(title, CssUtils.GREEN);

} else if (transaction instanceof Sale) {
title = new Label("SALE");
CssUtils.apply(title, CssUtils.RED);

SaleCalculator calculator = (SaleCalculator) transaction.getCalculator();
totalValue = new Label("Total: " + calculator.calculateTotal() + " USD");
taxComissionValue = new Label(
"Tax & Comission: " + calculator.calculateTax().add(calculator.calculateCommision()) + " USD");
grossValue = new Label("Gross: " + calculator.calculateGross() + " USD");
amountOfShares = new Label("Shares: " + transaction.getShare().getQuantity() + " [" + stock.getSymbol() + "]");
saleProfit = new Label("Profit: " + calculator.calculateProfit() + " USD");
SaleCalculator sCalc = (SaleCalculator) transaction.getCalculator();
saleProfit = new Label(String.format("Profit: %.2f USD", sCalc.calculateProfit()));

CssUtils.apply(saleProfit, CssUtils.generateValueColors(calculator.calculateProfit().doubleValue()));
CssUtils.apply(saleProfit, CssUtils.generateValueColors(sCalc.calculateProfit().doubleValue()));
}

else {
Expand Down
Loading
Loading