Skip to content

Commit

Permalink
Player adding null and negative checks
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed Mar 6, 2026
1 parent 6b33b97 commit f7e1db6
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/millions/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ public Player(String name, BigDecimal startingMoney) {
this.money = startingMoney;
this.portfolio = new Portfolio();
this.transactionArchive = new TransactionArchive();

if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Player name cannot be null or blank");
}

if (startingMoney == null || startingMoney.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Starting money cannot be null or negative");
}
}

public void addMoney(BigDecimal amount) {
if (amount == null || amount.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Amount cannot be null or negative");
}
this.money = this.money.add(amount);
}

public void withdrawMoney(BigDecimal amount) {
if (amount == null || amount.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Amount cannot be null or negative");
}
this.money = this.money.subtract(amount);
}

Expand Down

0 comments on commit f7e1db6

Please sign in to comment.