Skip to content

Commit

Permalink
chore: Restore method lines for checking if divisor is null (Empty po…
Browse files Browse the repository at this point in the history
…rtfolio).
  • Loading branch information
PawelSapula committed May 25, 2026
1 parent 5c86b15 commit a5166a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public BigDecimal getChangeFromStock() {
BigDecimal costTotal = getShares().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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ void addShare_shouldAddToPortfolio() {

}

void addDefaultShare() { // Since test over works, we will use this to initialize
// the rest of the tests under.
portfolio.addShare(share);
}

@Test
void getShares() {

Expand All @@ -54,11 +59,6 @@ void getShares() {
portfolio.getShares());
}

void addDefaultShare() { // Since test over works, we will use this to initialize
// the rest of the tests under.
portfolio.addShare(share);
}

@Test
void getSharesBySymbol_shouldReturnShares() {
addDefaultShare();
Expand Down Expand Up @@ -217,11 +217,18 @@ void getChangeFromStock_shouldReturnTotalFromPortfolioChange() {

BigDecimal expected = profit
.divide(totalCost, 2, RoundingMode.HALF_UP)
.multiply(BigDecimal.valueOf(100));
.multiply(new BigDecimal("100"));

assertEquals(
expected,
portfolio.getChangeFromStock());
}

@Test
void getChangeFromStock_emptyPortfolio() {

assertEquals(BigDecimal.ZERO, portfolio.getChangeFromStock());

}

}

0 comments on commit a5166a7

Please sign in to comment.