update BanckAccount class

This commit is contained in:
2026-06-10 07:41:45 +03:30
parent f3c9bf8876
commit dd2de9aef0
@@ -35,6 +35,7 @@ public class BankAccount {
public void deposit(long amount) { public void deposit(long amount) {
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
if (amount < 0) throw new IllegalArgumentException("Amount cannot be negative");
balance += amount; balance += amount;
} }
finally { finally {
@@ -45,6 +46,8 @@ public class BankAccount {
public void withdraw(long amount) { public void withdraw(long amount) {
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
if (amount < 0) throw new IllegalArgumentException("Amount cannot be negative");
if (balance < amount) throw new IllegalArgumentException("insufficient balance");
balance -= amount; balance -= amount;
} }
finally { finally {
@@ -61,6 +64,8 @@ public class BankAccount {
try { try {
second.lock.writeLock().lock(); second.lock.writeLock().lock();
try { try {
if (amount < 0) throw new IllegalArgumentException("Amount cannot be negative");
if (amount > balance) throw new IllegalArgumentException("insufficient balance");
this.balance -= amount; this.balance -= amount;
target.balance += amount; target.balance += amount;
} finally { } finally {