implement BankAccount.java

This commit is contained in:
2026-06-15 17:12:58 +03:30
parent 69620f3e55
commit ed4473a239
@@ -1,9 +1,13 @@
package dev.banking.model; package dev.banking.model;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BankAccount { public class BankAccount {
private final int accountId; private final int accountId;
private long balance; private long balance;
private Lock lock = new ReentrantLock();
/* /*
* Students may introduce additional fields * Students may introduce additional fields
@@ -23,44 +27,44 @@ public class BankAccount {
return accountId; return accountId;
} }
/* /* Return the current balance in a thread-safe way.
* TODO:
* Return the current balance in a thread-safe way.
* *
* Requirements: * Requirements:
* - Must be safe under concurrent reads/writes * - Must be safe under concurrent reads/writes
* - Should not block unnecessarily if using read/write locks * - Should not block unnecessarily if using read/write locks
*/ */
public long getBalance() { public long getBalance() {
throw new UnsupportedOperationException("TODO: implement thread-safe balance read");
lock.lock();
try{
return balance;
}
finally {
lock.unlock();
}
} }
/* /* Increase balance atomically.
* TODO:
* Increase balance atomically.
* *
* Requirements: * Requirements:
* - Must not lose updates under concurrency * - Must not lose updates under concurrency
*/ */
public void deposit(long amount) { public synchronized void deposit(long amount) {
throw new UnsupportedOperationException("TODO: implement thread-safe deposit"); balance += amount;
} }
/* /* Decrease balance atomically.
* TODO:
* Decrease balance atomically.
* *
* Requirements: * Requirements:
* - Must not cause race conditions * - Must not cause race conditions
* - Negative balance handling is NOT required unless you decide * - Negative balance handling is NOT required unless you decide
* to extend the system (optional) * to extend the system (optional)
*/ */
public void withdraw(long amount) { public synchronized void withdraw(long amount) {
throw new UnsupportedOperationException("TODO: implement thread-safe withdraw"); balance -= amount;
} }
/* /*
* TODO:
* Transfer money between two accounts atomically. * Transfer money between two accounts atomically.
* *
* IMPORTANT REQUIREMENTS: * IMPORTANT REQUIREMENTS:
@@ -73,6 +77,33 @@ public class BankAccount {
* - Or tryLock with retry strategy * - Or tryLock with retry strategy
*/ */
public void transfer(BankAccount target, long amount) { public void transfer(BankAccount target, long amount) {
throw new UnsupportedOperationException("TODO: implement atomic deadlock-free transfer");
BankAccount first, second;
if(target == this) return;
if (this.getAccountId() < target.getAccountId()) {
first = this;
second = target;
}
else {
first = target;
second = this;
}
first.lock.lock();
try {
second.lock.lock();
try {
this.withdraw(amount);
target.deposit(amount);
}
finally {
second.lock.unlock();
}
}
finally {
first.lock.unlock();
}
} }
} }