Skip to content

Commit

Permalink
Update Purchase class with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed May 24, 2026
1 parent 3bb7de3 commit 428fde3
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/Model/Purchase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 428fde3

Please sign in to comment.