diff --git a/src/main/java/edu/ntnu/idi/idatt/model/market/Stock.java b/src/main/java/edu/ntnu/idi/idatt/model/market/Stock.java index 923df9f..c7c65c4 100644 --- a/src/main/java/edu/ntnu/idi/idatt/model/market/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt/model/market/Stock.java @@ -1,6 +1,7 @@ package edu.ntnu.idi.idatt.model.market; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -90,6 +91,16 @@ public BigDecimal getLatestPriceChange() { return prices.get(size - 1).subtract(prices.get(size - 2)); } + // TODO: JAVADOCS, JUNIT + public BigDecimal getLatestPriceChangePercent() { + if (prices.isEmpty() || prices.size() == 1) { + return BigDecimal.ZERO; + } + + return (getLatestPriceChange().divide(prices.get(prices.size() - 2), 2, RoundingMode.HALF_UP)) + .multiply(new BigDecimal("100")); + } + /** * Getter for current sale price * diff --git a/src/main/java/edu/ntnu/idi/idatt/model/portfolio/Portfolio.java b/src/main/java/edu/ntnu/idi/idatt/model/portfolio/Portfolio.java index 0b9e940..070653d 100644 --- a/src/main/java/edu/ntnu/idi/idatt/model/portfolio/Portfolio.java +++ b/src/main/java/edu/ntnu/idi/idatt/model/portfolio/Portfolio.java @@ -61,6 +61,25 @@ public List getShares(String symbol) { return shares.stream().filter(s -> s.getStock().getSymbol().equals(symbol)).toList(); } + // TODO: JAVADOCS, JUNIT + public BigDecimal getOwnedAmount(String symbol) { + return getShares(symbol).stream().map(s -> s.getQuantity()) + .reduce(BigDecimal.ZERO, BigDecimal::add); + } + + public BigDecimal getProfitFromStock(String symbol) { + return getShares(symbol).stream().map(s -> s.getProfit()) + .reduce(BigDecimal.ZERO, BigDecimal::add); + } + + public double getChangeFromStock(String symbol) { + BigDecimal profitTotal = getProfitFromStock(symbol); + BigDecimal costTotal = getShares(symbol).stream().map(s -> s.getPurchasePrice()) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + return (profitTotal.subtract(costTotal).doubleValue()) / 100; + } + /** * Method for checking if the portfolio contains a specific share * diff --git a/src/main/java/edu/ntnu/idi/idatt/model/portfolio/Share.java b/src/main/java/edu/ntnu/idi/idatt/model/portfolio/Share.java index 2bbaf0f..eeaa73f 100644 --- a/src/main/java/edu/ntnu/idi/idatt/model/portfolio/Share.java +++ b/src/main/java/edu/ntnu/idi/idatt/model/portfolio/Share.java @@ -3,6 +3,7 @@ import java.math.BigDecimal; import edu.ntnu.idi.idatt.model.market.Stock; +import edu.ntnu.idi.idatt.service.transaction.SaleCalculator; /** * Share class @@ -50,4 +51,9 @@ public BigDecimal getPurchasePrice() { return purchasePrice; } + // TODO: JAVADOCS, JUNIT + public BigDecimal getProfit() { + return new SaleCalculator(this).calculateGross().subtract(purchasePrice); + } + } diff --git a/src/main/java/edu/ntnu/idi/idatt/view/SceneFactory.java b/src/main/java/edu/ntnu/idi/idatt/view/SceneFactory.java index c68f6b2..6f290f8 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/SceneFactory.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/SceneFactory.java @@ -1,8 +1,15 @@ package edu.ntnu.idi.idatt.view; +import edu.ntnu.idi.idatt.model.market.Stock; import edu.ntnu.idi.idatt.view.entry.StartController; import edu.ntnu.idi.idatt.view.entry.StartModel; import edu.ntnu.idi.idatt.view.entry.StartView; +import edu.ntnu.idi.idatt.view.primary.exchange.ExchangeController; +import edu.ntnu.idi.idatt.view.primary.exchange.ExchangeModel; +import edu.ntnu.idi.idatt.view.primary.exchange.ExchangeView; +import edu.ntnu.idi.idatt.view.primary.stock.StockController; +import edu.ntnu.idi.idatt.view.primary.stock.StockModel; +import edu.ntnu.idi.idatt.view.primary.stock.StockView; import javafx.scene.Parent; public class SceneFactory { @@ -20,4 +27,29 @@ public static Parent createStartView() { } + public static Parent createExchangeView() { + + ExchangeModel model = new ExchangeModel(); + ExchangeView view = new ExchangeView(); + ExchangeController controller = new ExchangeController(model); + + view.setModel(model); + view.setController(controller); + + return view.getInstance(); + + } + + public static Parent createStockView(Stock stock) { + + StockModel model = new StockModel(); + StockView view = new StockView(); + StockController controller = new StockController(model, stock); + + view.setModel(model); + view.setController(controller); + + return view.getInstance(); + } + } diff --git a/src/main/java/edu/ntnu/idi/idatt/view/components/AbstractViewUI.java b/src/main/java/edu/ntnu/idi/idatt/view/components/AbstractViewUI.java index 1933e3b..5d9fd48 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/components/AbstractViewUI.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/components/AbstractViewUI.java @@ -59,7 +59,7 @@ public void createUIComponents() { navigation = new VBox(); navigation.setMaxHeight(Double.MAX_VALUE); navigation.getStyleClass().add("dark"); - navigation.setMaxWidth(150); + navigation.setMaxWidth(200); header = new HBox(); header.getStyleClass().add("light"); diff --git a/src/main/java/edu/ntnu/idi/idatt/view/components/elements/StockComponent.java b/src/main/java/edu/ntnu/idi/idatt/view/components/elements/StockComponent.java new file mode 100644 index 0000000..b87d333 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt/view/components/elements/StockComponent.java @@ -0,0 +1,78 @@ +package edu.ntnu.idi.idatt.view.components.elements; + +import java.math.BigDecimal; +import java.util.List; +import java.util.function.Consumer; + +import edu.ntnu.idi.idatt.model.market.Stock; +import edu.ntnu.idi.idatt.view.components.ui.UICompositor; +import edu.ntnu.idi.idatt.view.util.CssUtils; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.control.Label; +import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; + +public class StockComponent extends HBox { + + private final Stock stock; + + public StockComponent(Stock stock) { + this.stock = stock; + this.setMaxSize(Double.MAX_VALUE, 100); + this.setPadding(new Insets(40)); + this.setAlignment(Pos.BASELINE_CENTER); + + this.setStyle("-fx-background-color: #404950;"); + + Label title = new Label(this.getTitle()); + + Label latestText = new Label("Latest"); + Label latestValue = new Label(stock.getSalesPrice().toString() + "USD"); + + Label changeText = new Label("Change"); + Label changeValue = new Label(stock.getLatestPriceChange().toString() + "USD"); + Label changeValuePercent = new Label( + String.valueOf(stock.getLatestPriceChangePercent()) + "%"); + + List