Skip to content

Commit

Permalink
refactor(Portfolio): playerNetValue -> getNetWorth. Fix logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsa committed Mar 25, 2026
1 parent f1247de commit 4c6750c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/main/java/edu/ntnu/idi/idatt/marked/Portfolio.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.stream.Collectors;

import edu.ntnu.idi.idatt.calculator.SaleCalculator;

/**
* Portfolio class
*
Expand Down Expand Up @@ -70,15 +72,14 @@ public boolean contains(Share share) {
}

/**
* Method for getting the net value of the player.
* Method for getting the net value of the portfolio..
*
* @return - Net value of the players shares.
* @return - Net value of the portfolio.
*/
public BigDecimal playerNetValue(){
BigDecimal netValue = shares.stream()
.map(Share-> Share.getPurchasePrice().multiply(Share.getQuantity()))
.reduce(BigDecimal.ZERO, BigDecimal::add);
return netValue;
public BigDecimal getNetWorth() {
return shares.stream()
.map(s -> new SaleCalculator(s).calculateTotal())
.reduce(BigDecimal.ZERO, BigDecimal::add);
}

}
7 changes: 5 additions & 2 deletions src/test/java/edu/ntnu/idi/idatt/marked/PortfolioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import edu.ntnu.idi.idatt.calculator.SaleCalculator;

public class PortfolioTest {

private Stock stock;
Expand Down Expand Up @@ -82,12 +84,13 @@ void NTremoveShare() {
* Positive test for finding net value of the players shares.
*/
@Test
void PTplayerNetValue(){
void PTgetNetWorth() {
Share share1 = new Share(stock, new BigDecimal("1"), new BigDecimal("135.8"));
Share share2 = new Share(stock, new BigDecimal("2"), new BigDecimal("254"));
portfolio.addShare(share1);
portfolio.addShare(share2);
assertEquals(new BigDecimal("643.8"), portfolio.playerNetValue());
assertEquals(new SaleCalculator(share1).calculateTotal().add(new SaleCalculator(share2).calculateTotal()),
portfolio.getNetWorth());
}

}

0 comments on commit 4c6750c

Please sign in to comment.