diff --git a/src/main/java/edu/ntnu/idi/idatt/model/player/Player.java b/src/main/java/edu/ntnu/idi/idatt/model/player/Player.java index 6f981a7..d0a21e2 100644 --- a/src/main/java/edu/ntnu/idi/idatt/model/player/Player.java +++ b/src/main/java/edu/ntnu/idi/idatt/model/player/Player.java @@ -89,5 +89,8 @@ public String getStatus() { return "Novice"; } + public BigDecimal getStartingMoney(){ + return startingMoney; + } } 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 78a591b..c255340 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 @@ -73,18 +73,33 @@ public BigDecimal getOwnedAmount(String symbol) { .reduce(BigDecimal.ZERO, BigDecimal::add); } + public BigDecimal getOwnedAmount() { + return getShares().stream().map(s -> s.getQuantity()) + .reduce(BigDecimal.ZERO, BigDecimal::add); + } + public BigDecimal getProfitFromStock(String symbol) { - return getShares(symbol).stream().map(s -> { - BigDecimal total = new SaleCalculator(s).calculateTotal(); - BigDecimal buyPrice = s.getQuantity().multiply(s.getPurchasePrice()); + return getShares(symbol).stream().map(s -> s.getProfit()).reduce(BigDecimal.ZERO, BigDecimal::add); + } - return total.subtract(buyPrice); - }).reduce(BigDecimal.ZERO, BigDecimal::add); + public BigDecimal getProfitFromStock() { + return getShares().stream().map(s -> s.getProfit()).reduce(BigDecimal.ZERO, BigDecimal::add); } public BigDecimal getChangeFromStock(String symbol) { BigDecimal profitTotal = getProfitFromStock(symbol); - BigDecimal costTotal = getShares(symbol).stream().map(s -> s.getPurchasePrice().multiply(s.getQuantity())) + BigDecimal costTotal = getShares(symbol).stream().map(s -> s.getTotalPurchasePrice()) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + if (costTotal.compareTo(BigDecimal.ZERO) <= 0) + return BigDecimal.ZERO; + + return profitTotal.divide(costTotal, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)); + } + + public BigDecimal getChangeFromStock() { + BigDecimal profitTotal = getProfitFromStock(); + BigDecimal costTotal = getShares().stream().map(s -> s.getTotalPurchasePrice()) .reduce(BigDecimal.ZERO, BigDecimal::add); if (costTotal.compareTo(BigDecimal.ZERO) <= 0) 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 a00a65f..186cb43 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 @@ -1,6 +1,7 @@ package edu.ntnu.idi.idatt.model.portfolio; import java.math.BigDecimal; +import java.math.RoundingMode; import edu.ntnu.idi.idatt.model.market.Stock; import edu.ntnu.idi.idatt.service.transaction.SaleCalculator; @@ -52,9 +53,16 @@ public BigDecimal getPurchasePrice() { } // TODO: JAVADOCS, JUNIT + public BigDecimal getTotalPurchasePrice() { + return purchasePrice.multiply(quantity); + } + public BigDecimal getProfit() { - BigDecimal totalCost = purchasePrice.multiply(quantity); - return new SaleCalculator(this).calculateGross().subtract(totalCost); + return new SaleCalculator(this).calculateGross().subtract(getTotalPurchasePrice()); + } + + public BigDecimal getProfitPercent() { + return getProfit().divide(getTotalPurchasePrice(), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)); } } diff --git a/src/main/java/edu/ntnu/idi/idatt/service/transaction/SaleCalculator.java b/src/main/java/edu/ntnu/idi/idatt/service/transaction/SaleCalculator.java index 102f1a5..47819d5 100644 --- a/src/main/java/edu/ntnu/idi/idatt/service/transaction/SaleCalculator.java +++ b/src/main/java/edu/ntnu/idi/idatt/service/transaction/SaleCalculator.java @@ -3,6 +3,7 @@ import edu.ntnu.idi.idatt.model.portfolio.Share; import java.math.BigDecimal; +import java.math.RoundingMode; /** * SaleCalculator class @@ -78,7 +79,7 @@ public BigDecimal calculateTotal() { // TODO: Javadocs, junit public BigDecimal calculateProfit() { - return calculateTotal().divide(purchasePrice.multiply(quantity)); + return calculateGross().subtract(purchasePrice.multiply(quantity)); } } diff --git a/src/main/java/edu/ntnu/idi/idatt/view/components/elements/PlayerPortfolioComponent.java b/src/main/java/edu/ntnu/idi/idatt/view/components/elements/PlayerPortfolioComponent.java index 6744dd4..5b15d78 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/components/elements/PlayerPortfolioComponent.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/components/elements/PlayerPortfolioComponent.java @@ -3,11 +3,15 @@ import edu.ntnu.idi.idatt.model.portfolio.Portfolio; import edu.ntnu.idi.idatt.session.UserSession; import edu.ntnu.idi.idatt.view.components.ui.UICompositor; +import edu.ntnu.idi.idatt.view.util.CssUtils; import javafx.geometry.Pos; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; +import java.math.BigDecimal; +import java.math.MathContext; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.Collections; @@ -16,13 +20,19 @@ public class PlayerPortfolioComponent extends HBox { public PlayerPortfolioComponent(Portfolio portfolio) { this.setMaxSize(Double.MAX_VALUE, 500); this.getStyleClass().add("light"); + BigDecimal startingMoney = UserSession.getInstance().getPlayer().getStartingMoney(); + BigDecimal netMoney = UserSession.getInstance().getPlayer().getNetWorth(); + BigDecimal netPercentage = netMoney.divide(startingMoney) + .multiply(new BigDecimal("100")) + .setScale(2, RoundingMode.HALF_UP) + .subtract(new BigDecimal("100")); Label userTitle = new Label(String.format("%s's Portfolio", UserSession.getInstance().getPlayer().getName())); Label netWorth = new Label(); netWorth.textProperty().bind( UserSession.getInstance().netWorthProperty().asString("Net Worth: %.2f $")); Label percentageChange = new Label( - "Percentage change: " + UserSession.getInstance().netWorthProperty().get()); + "Percentage change: " + netPercentage + "%"); Label playerStatus = new Label("Player status: " + UserSession.getInstance().getPlayer().getStatus()); Label totalShares = new Label( "Total shares owned: " + UserSession.getInstance().getPlayer().getPortfolio().getShares().size()); @@ -30,6 +40,8 @@ public PlayerPortfolioComponent(Portfolio portfolio) { Collections.addAll(labels, netWorth, percentageChange, playerStatus, totalShares); labels.forEach(e -> e.getStyleClass().add("portfolio-box-text")); userTitle.getStyleClass().add("portfolio-box-title"); + String color = CssUtils.generateValueColors(netPercentage); + CssUtils.apply(percentageChange, color); UICompositor playerPortfolioComponent = new UICompositor.Builder() .parent(new HBox()) diff --git a/src/main/java/edu/ntnu/idi/idatt/view/components/elements/ShareComponent.java b/src/main/java/edu/ntnu/idi/idatt/view/components/elements/ShareComponent.java index 7546158..486b994 100644 --- a/src/main/java/edu/ntnu/idi/idatt/view/components/elements/ShareComponent.java +++ b/src/main/java/edu/ntnu/idi/idatt/view/components/elements/ShareComponent.java @@ -8,44 +8,57 @@ import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; -import java.util.ArrayList; -import java.util.Collections; - +import java.util.List; +import java.util.function.Consumer; public class ShareComponent extends HBox { - public ShareComponent(Share share){ - this.setMaxSize(Double.MAX_VALUE, 300); - this.setPadding(new Insets(30)); - this.getStyleClass().add("rowBox"); - - Label name = new Label(share.getStock().getCompany()); - Label quantity = new Label("Owned shares: "+share.getQuantity().toString()); - Label ticker = new Label("("+share.getStock().getSymbol()+")"); - Label latestPrice = new Label("Latest price: "+share.getStock().getSalesPrice().toString()); - Button sellButton = new Button("Sell"); - - ArrayList