-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,87 @@ | ||
| package Model; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Portfolio class of the player. | ||
| */ | ||
| public class Portfolio { | ||
|
|
||
| private final List<Share> shares; | ||
| private final List<Share> shares; | ||
|
|
||
| public Portfolio() { | ||
| this.shares = new ArrayList<>(); | ||
| } | ||
| public Portfolio() { | ||
| this.shares = new ArrayList<>(); | ||
| } | ||
|
|
||
| public boolean addShare(Share share) { | ||
| if (share == null) { | ||
| throw new IllegalArgumentException("Share cannot be null"); | ||
| } | ||
| return shares.add(share); | ||
| /** | ||
| * Method to add share. | ||
| * | ||
| * @param share the share being added | ||
| * @return returns the added share | ||
| */ | ||
| public boolean addShare(Share share) { | ||
| if (share == null) { | ||
| throw new IllegalArgumentException("Share cannot be null"); | ||
| } | ||
| return shares.add(share); | ||
| } | ||
|
|
||
| public boolean removeShare(Share share) { | ||
| if (share == null) { | ||
| throw new IllegalArgumentException("Share cannot be null"); | ||
| } | ||
| return shares.remove(share); | ||
| /** | ||
| * Method to remove share. | ||
| * | ||
| * @param share the share being removed | ||
| * @return returns the removed share | ||
| */ | ||
| public boolean removeShare(Share share) { | ||
| if (share == null) { | ||
| throw new IllegalArgumentException("Share cannot be null"); | ||
| } | ||
| return shares.remove(share); | ||
| } | ||
|
|
||
| public List<Share> getShares() { | ||
| return new ArrayList<>(shares); | ||
| } | ||
| public List<Share> getShares() { | ||
| return new ArrayList<>(shares); | ||
| } | ||
|
|
||
| public List<Share> getShares(String symbol) { | ||
| if (symbol == null || symbol.isBlank()) { | ||
| throw new IllegalArgumentException("Symbol cannot be null or blank"); | ||
| } | ||
| return shares.stream() | ||
| .filter(share -> share.getStock().getSymbol().equals(symbol)) | ||
| .toList(); | ||
| /** | ||
| * Method to get all shares. | ||
| * | ||
| * @param symbol the symbol for each stock | ||
| * @return the shares of stocks | ||
| */ | ||
| public List<Share> getShares(String symbol) { | ||
| if (symbol == null || symbol.isBlank()) { | ||
| throw new IllegalArgumentException("Symbol cannot be null or blank"); | ||
| } | ||
| return shares.stream() | ||
| .filter(share -> share.getStock().getSymbol().equals(symbol)) | ||
| .toList(); | ||
| } | ||
|
|
||
| public boolean contains(Share share) { | ||
| if (share == null) { | ||
| throw new IllegalArgumentException("Share cannot be null"); | ||
| } | ||
| return shares.contains(share); | ||
| /** | ||
| * Method to check if contains share. | ||
| * | ||
| * @param share the share | ||
| * @return the share if not null | ||
| */ | ||
| public boolean contains(Share share) { | ||
| if (share == null) { | ||
| throw new IllegalArgumentException("Share cannot be null"); | ||
| } | ||
| return shares.contains(share); | ||
| } | ||
|
|
||
| public BigDecimal getNetWorth() { | ||
| return shares.stream() | ||
| .map(share -> new SaleCalculator(share).calculateTotal()) | ||
| .reduce(BigDecimal.ZERO, BigDecimal::add); | ||
| } | ||
| /** | ||
| * Method to get the new worth. | ||
| * | ||
| * @return returns the net worth | ||
| */ | ||
| public BigDecimal getNetWorth() { | ||
| return shares.stream() | ||
| .map(share -> new SaleCalculator(share).calculateTotal()) | ||
| .reduce(BigDecimal.ZERO, BigDecimal::add); | ||
| } | ||
|
|
||
| } |