From 3bb7de3b67d2400406591a8f9d6944b86024461a Mon Sep 17 00:00:00 2001 From: Elisabeth Berg Date: Sun, 24 May 2026 19:57:42 +0200 Subject: [PATCH] Update Portfolio class with exceptions --- src/main/java/Model/Portfolio.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/Model/Portfolio.java b/src/main/java/Model/Portfolio.java index 05dd1de..949bea3 100644 --- a/src/main/java/Model/Portfolio.java +++ b/src/main/java/Model/Portfolio.java @@ -12,10 +12,16 @@ public Portfolio() { } 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); } @@ -24,6 +30,9 @@ public List getShares() { } public List getShares(String symbol) { + if (symbol == null || symbol.isBlank()) { + throw new IllegalArgumentException("Symbol cannot be null or blank"); + } List result = new ArrayList<>(); for (Share share : shares) { if (share.getStock().getSymbol().equals(symbol)) { @@ -34,6 +43,9 @@ public List getShares(String symbol) { } public boolean contains(Share share) { + if (share == null) { + throw new IllegalArgumentException("Share cannot be null"); + } return shares.contains(share); }