Skip to content

Commit

Permalink
Update Player class with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed May 24, 2026
1 parent 7ea77dd commit bd8d34a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/Model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down

0 comments on commit bd8d34a

Please sign in to comment.