From 588cd2dc274055e57f64b37b9d08e6efd1c8e48d Mon Sep 17 00:00:00 2001 From: Elisabeth Berg Date: Sun, 24 May 2026 19:58:07 +0200 Subject: [PATCH] Update Sale class with exceptions --- src/main/java/Model/Sale.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/Model/Sale.java b/src/main/java/Model/Sale.java index 42fee68..7653df4 100644 --- a/src/main/java/Model/Sale.java +++ b/src/main/java/Model/Sale.java @@ -6,12 +6,21 @@ public Sale(Share share, int week) { super(share, week, new SaleCalculator(share)); } + @Override public void commit(Player player) { - BigDecimal price = getCalculator().calculateTotal(); - - if (isCommitted() || !player.getPortfolio().contains(this.getShare())) { - return; + if (player == null) { + throw new IllegalArgumentException("Player cannot be null"); + } + if (isCommitted()) { + throw new IllegalStateException("Sale has already been committed"); } + if (!player.getPortfolio().contains(this.getShare())) { + throw new IllegalStateException( + "Share not found in player's portfolio: " + this.getShare().getStock().getSymbol() + ); + } + + BigDecimal price = getCalculator().calculateTotal(); player.addMoney(price); player.getPortfolio().removeShare(this.getShare());