From f7e1db69f88ad4de9f43c0bd337d00e124966281 Mon Sep 17 00:00:00 2001 From: martin Date: Fri, 6 Mar 2026 13:14:55 +0100 Subject: [PATCH] Player adding null and negative checks --- src/main/java/millions/Player.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/millions/Player.java b/src/main/java/millions/Player.java index 747c8d0..6c01c2c 100644 --- a/src/main/java/millions/Player.java +++ b/src/main/java/millions/Player.java @@ -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); }