Skip to content

Commit

Permalink
Feat: Updated portfolio
Browse files Browse the repository at this point in the history
Added method for getting networth
  • Loading branch information
tommyah committed Mar 18, 2026
1 parent 66ef91c commit 915d867
Showing 1 changed file with 84 additions and 61 deletions.
145 changes: 84 additions & 61 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,102 @@
package edu.ntnu.idi.idatt2003.g40.mappe;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Represents a player's portfolio of shares.
* <p>
* The portfolio stores shares and provides operations for adding, removing,
* retrieving and checking ownership of shares.
* </p>
*
* <p>The portfolio stores shares and provides operations for adding, removing,
* retrieving and checking ownership of shares.</p>
*/
public class Portfolio {
public final class Portfolio {

private final List<Share> shares = new ArrayList<>();
/**
* List of shares.
* */
private final List<Share> shares = new ArrayList<>();

/**
* Creates an empty portfolio.
*/
public Portfolio() {
// Intentionally empty
}
/**
* Creates an empty portfolio.
*/
public Portfolio() {
// Intentionally empty
}

/**
* Adds a share to the portfolio.
*
* @param share the share to add
* @return {@code true} if the share was added, {@code false} otherwise
*/
public boolean addShare(Share share) {
Objects.requireNonNull(share, "share cannot be null");
return shares.add(share);
}
/**
* Adds a share to the portfolio.
*
* @param share the share to add
* @return {@code true} if the share was added, {@code false} otherwise
*/
public boolean addShare(final Share share) {
Objects.requireNonNull(share, "share cannot be null");
return shares.add(share);
}

/**
* Removes a share from the portfolio.
*
* @param share the share to remove
* @return {@code true} if the share was removed, {@code false} if it was not present
*/
public boolean removeShare(Share share) {
Objects.requireNonNull(share, "share cannot be null");
return shares.remove(share);
}
/**
* Removes a share from the portfolio.
*
* @param share the share to remove
* @return {@code true} if the share was removed,
* {@code false} if it was not present.
*/
public boolean removeShare(final Share share) {
Objects.requireNonNull(share, "share cannot be null");
return shares.remove(share);
}

/**
* Returns an immutable snapshot of all shares in the portfolio.
*
* @return a list of shares
*/
public List<Share> getShares() {
return List.copyOf(shares);
}
/**
* Returns an immutable snapshot of all shares in the portfolio.
*
* @return a list of shares
*/
public List<Share> getShares() {
return List.copyOf(shares);
}

/**
* Returns an immutable snapshot of all shares whose stock symbol matches the given symbol.
*
* @param symbol the stock symbol to match
* @return a list of shares matching the symbol
*/
public List<Share> getShares(String symbol) {
Objects.requireNonNull(symbol, "symbol cannot be null");
return shares.stream()
.filter(s -> symbol.equalsIgnoreCase(s.getStock().getSymbol()))
.toList();
}
/**
* Returns an immutable snapshot of all shares whose
* stock symbol matches the given symbol.
*
* @param symbol the stock symbol to match
* @return a list of shares matching the symbol
*/
public List<Share> getShares(final String symbol) {
Objects.requireNonNull(symbol, "symbol cannot be null");
return shares.stream()
.filter(s -> symbol.equalsIgnoreCase(s.getStock().getSymbol()))
.toList();
}

/**
* Checks whether the given share exists in the portfolio.
*
* @param share the share to check
* @return {@code true} if the portfolio
* contains the share, otherwise {@code false}
*/
public boolean contains(final Share share) {
Objects.requireNonNull(share, "share cannot be null");
return shares.contains(share);
}

/**
* Returns the total net worth of the portfolios shares,
* using a sale calculator.
*
* @return the net worth.
* */
public BigDecimal getNetWorth() {
BigDecimal netWorth = new BigDecimal("0");

for (Share s : shares) {
SaleCalculator calculator = new SaleCalculator(s);

/**
* Checks whether the given share exists in the portfolio.
*
* @param share the share to check
* @return {@code true} if the portfolio contains the share, otherwise {@code false}
*/
public boolean contains(Share share) {
Objects.requireNonNull(share, "share cannot be null");
return shares.contains(share);
netWorth = netWorth.add(calculator.calculateTotal());
}
return netWorth;
}
}

0 comments on commit 915d867

Please sign in to comment.