Skip to content

Commit

Permalink
feat: Create percentage calculator for net worth increase or decrease
Browse files Browse the repository at this point in the history
  • Loading branch information
danieskj committed May 15, 2026
1 parent 37229c9 commit 39ca987
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/model/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,8 @@ public String getStatus() {

return "Novice";
}
public BigDecimal getStartingMoney(){
return startingMoney;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,20 +20,28 @@ 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());
ArrayList<Label> labels = new ArrayList<>();
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())
Expand Down

0 comments on commit 39ca987

Please sign in to comment.