From 428fde3fd25bfdbc5ef619abb1c7396cb08f99ab Mon Sep 17 00:00:00 2001 From: Elisabeth Berg Date: Sun, 24 May 2026 19:57:55 +0200 Subject: [PATCH] Update Purchase class with exceptions --- src/main/java/Model/Purchase.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/Model/Purchase.java b/src/main/java/Model/Purchase.java index d0b14fe..33c41e5 100644 --- a/src/main/java/Model/Purchase.java +++ b/src/main/java/Model/Purchase.java @@ -8,10 +8,19 @@ public Purchase(Share share, int week) { @Override public void commit(Player player) { + if (player == null) { + throw new IllegalArgumentException("Player cannot be null"); + } + if (isCommitted()) { + throw new IllegalStateException("Purchase has already been committed"); + } + BigDecimal price = this.getCalculator().calculateTotal(); - if (isCommitted() || (player.getMoney().compareTo(price) < 0)) { - return; + if (player.getMoney().compareTo(price) < 0) { + throw new IllegalStateException( + "Insufficient funds: required " + price + ", available " + player.getMoney() + ); } player.withdrawMoney(price);