Skip to content

Commit

Permalink
fix(Portfolio): Profit calculation methids
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsa committed May 14, 2026
1 parent ba24b8d commit 28fa33e
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/edu/ntnu/idi/idatt/model/portfolio/Portfolio.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,23 @@ public BigDecimal getOwnedAmount(String symbol) {
}

public BigDecimal getProfitFromStock(String symbol) {
return getShares(symbol).stream().map(s -> s.getProfit())
.reduce(BigDecimal.ZERO, BigDecimal::add);
return getShares(symbol).stream().map(s -> {
BigDecimal total = new SaleCalculator(s).calculateTotal();
BigDecimal buyPrice = s.getQuantity().multiply(s.getPurchasePrice());

return total.subtract(buyPrice);
}).reduce(BigDecimal.ZERO, BigDecimal::add);
}

public BigDecimal getChangeFromStock(String symbol) {
BigDecimal profitTotal = getProfitFromStock(symbol);
BigDecimal costTotal = getShares(symbol).stream().map(s -> s.getPurchasePrice())
BigDecimal costTotal = getShares(symbol).stream().map(s -> s.getPurchasePrice().multiply(s.getQuantity()))
.reduce(BigDecimal.ZERO, BigDecimal::add);

if (costTotal.compareTo(BigDecimal.ZERO) <= 0)
return BigDecimal.ZERO;

return profitTotal.divide(costTotal, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
return profitTotal.divide(costTotal, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
}

/**
Expand Down

0 comments on commit 28fa33e

Please sign in to comment.