From bd8d34a8d1468d6b6588fa6e9da5c451122bbd8f Mon Sep 17 00:00:00 2001 From: Elisabeth Berg Date: Sun, 24 May 2026 19:57:30 +0200 Subject: [PATCH] Update Player class with exceptions --- src/main/java/Model/Player.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/Model/Player.java b/src/main/java/Model/Player.java index e7a1e31..cf3d8a3 100644 --- a/src/main/java/Model/Player.java +++ b/src/main/java/Model/Player.java @@ -9,6 +9,16 @@ public class Player { private TransactionArchive transactionArchive; public Player(String name, BigDecimal startingMoney) { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("Player name cannot be null or blank"); + } + if (startingMoney == null) { + throw new IllegalArgumentException("Starting money cannot be null"); + } + if (startingMoney.compareTo(BigDecimal.ZERO) < 0) { + throw new IllegalArgumentException("Starting money cannot be negative"); + } + this.name = name; this.startingMoney = startingMoney; this.money = startingMoney; @@ -25,10 +35,22 @@ public BigDecimal getMoney() { } public void addMoney(BigDecimal amount) { + if (amount == null) { + throw new IllegalArgumentException("Amount cannot be null"); + } + if (amount.compareTo(BigDecimal.ZERO) < 0) { + throw new IllegalArgumentException("Amount to add cannot be negative"); + } this.money = this.money.add(amount); } public void withdrawMoney(BigDecimal amount) { + if (amount == null) { + throw new IllegalArgumentException("Amount cannot be null"); + } + if (amount.compareTo(BigDecimal.ZERO) < 0) { + throw new IllegalArgumentException("Amount to withdraw cannot be negative"); + } this.money = this.money.subtract(amount); }