From e694c85bc56293deafd2609fb42a33fe2c21f13b Mon Sep 17 00:00:00 2001 From: EspenTinius Date: Fri, 13 Feb 2026 18:26:53 +0100 Subject: [PATCH 1/2] code portfolio class the code for the portfoli class --- .../idi/idatt2003/g40/mappe/Portfolio.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java index e69de29..bee0941 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java @@ -0,0 +1,41 @@ +package edu.ntnu.idi.idatt2003.g40.mappe; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class Portfolio { + + private final List shares = new ArrayList<>(); + + public Portfolio() { + // Intentionally empty + } + + public boolean addShare(Share share) { + Objects.requireNonNull(share, "share cannot be null"); + return shares.add(share); + } + + public boolean removeShare(Share share) { + Objects.requireNonNull(share, "share cannot be null"); + return shares.remove(share); + } + + + public List getShares() { + return List.copyOf(shares); + } + + public List getShares(String symbol) { + Objects.requireNonNull(symbol, "symbol cannot be null"); + return shares.stream() + .filter(s -> symbol.equalsIgnoreCase(s.getStock().getSymbol())) + .toList(); + } + + public boolean contains(Share share) { + Objects.requireNonNull(share, "share cannot be null"); + return shares.contains(share); + } +} From 5e509c9706e4136548d23e4b9f3ea0b43075e73c Mon Sep 17 00:00:00 2001 From: EspenTinius Date: Fri, 13 Feb 2026 18:30:07 +0100 Subject: [PATCH 2/2] doc portfolio class added det documentation for the portfolio class --- .../idi/idatt2003/g40/mappe/Portfolio.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java index bee0941..f27e44c 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Portfolio.java @@ -4,29 +4,61 @@ import java.util.List; import java.util.Objects; +/** + * Represents a player's portfolio of shares. + *

+ * The portfolio stores shares and provides operations for adding, removing, + * retrieving and checking ownership of shares. + *

+ */ public class Portfolio { private final List shares = new ArrayList<>(); + /** + * 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); } + /** + * 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); } - + /** + * Returns an immutable snapshot of all shares in the portfolio. + * + * @return a list of shares + */ public List 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 getShares(String symbol) { Objects.requireNonNull(symbol, "symbol cannot be null"); return shares.stream() @@ -34,6 +66,12 @@ public List getShares(String symbol) { .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(Share share) { Objects.requireNonNull(share, "share cannot be null"); return shares.contains(share);