Implement all TODOs

This commit is contained in:
2026-06-13 16:46:39 +03:30
parent fa95f2ebeb
commit 96532c56da
15 changed files with 154 additions and 98 deletions
@@ -1,18 +1,13 @@
package dev.banking.model;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BankAccount {
private final int accountId;
private long balance;
/*
* Students may introduce additional fields
* such as:
* - Lock / ReentrantLock
* - ReadWriteLock
* - Object monitor
* - etc.
*/
private final Lock lock = new ReentrantLock();
public BankAccount(int accountId, long initialBalance) {
this.accountId = accountId;
@@ -23,56 +18,48 @@ public class BankAccount {
return accountId;
}
/*
* TODO:
* Return the current balance in a thread-safe way.
*
* Requirements:
* - Must be safe under concurrent reads/writes
* - Should not block unnecessarily if using read/write locks
*/
public long getBalance() {
throw new UnsupportedOperationException("TODO: implement thread-safe balance read");
lock.lock();
try {
return balance;
} finally {
lock.unlock();
}
}
/*
* TODO:
* Increase balance atomically.
*
* Requirements:
* - Must not lose updates under concurrency
*/
public void deposit(long amount) {
throw new UnsupportedOperationException("TODO: implement thread-safe deposit");
lock.lock();
try {
balance += amount;
} finally {
lock.unlock();
}
}
/*
* TODO:
* Decrease balance atomically.
*
* Requirements:
* - Must not cause race conditions
* - Negative balance handling is NOT required unless you decide
* to extend the system (optional)
*/
public void withdraw(long amount) {
throw new UnsupportedOperationException("TODO: implement thread-safe withdraw");
lock.lock();
try {
balance -= amount;
} finally {
lock.unlock();
}
}
/*
* TODO:
* Transfer money between two accounts atomically.
*
* IMPORTANT REQUIREMENTS:
* - Must be atomic (no partial transfer)
* - Must be deadlock-free
* - Must protect both source and target accounts
*
* HINT:
* - Consider global lock ordering using accountId
* - Or tryLock with retry strategy
*/
public void transfer(BankAccount target, long amount) {
throw new UnsupportedOperationException("TODO: implement atomic deadlock-free transfer");
BankAccount first = this.accountId < target.accountId ? this : target;
BankAccount second = this.accountId < target.accountId ? target : this;
first.lock.lock();
try {
second.lock.lock();
try {
this.withdraw(amount);
target.deposit(amount);
} finally {
second.lock.unlock();
}
} finally {
first.lock.unlock();
}
}
}
@@ -1,8 +1,5 @@
package dev.banking.model;
/**
* Represents a deposit operation.
*/
public final class DepositTransaction extends Transaction {
private final int accountId;
@@ -1,8 +1,5 @@
package dev.banking.model;
/**
* Base class for all transaction types.
*/
public abstract class Transaction {
private final int amount;
@@ -1,8 +1,5 @@
package dev.banking.model;
/**
* Represents a transfer operation between two accounts.
*/
public final class TransferTransaction
extends Transaction {
@@ -1,8 +1,5 @@
package dev.banking.model;
/**
* Represents a withdrawal operation.
*/
public final class WithdrawTransaction extends Transaction {
private final int accountId;
@@ -1,7 +1,6 @@
package dev.banking.monitor;
import dev.banking.model.BankAccount;
import java.util.Collection;
public class LiveMonitor {
@@ -9,15 +8,12 @@ public class LiveMonitor {
public void update(
Collection<BankAccount> accounts
) {
for (BankAccount account : accounts) {
System.out.printf(
"Account %d -> %d%n",
account.getAccountId(),
account.getBalance()
);
}
}
}
@@ -1,7 +1,6 @@
package dev.banking.processor;
import dev.banking.model.*;
import java.util.Map;
public class TransactionProcessor {
@@ -15,33 +14,19 @@ public class TransactionProcessor {
}
public void process(Transaction tx) {
if (tx instanceof DepositTransaction deposit) {
BankAccount account = accounts.get(deposit.getAccountId());
account.deposit(deposit.getAmount());
}
else if (tx instanceof WithdrawTransaction withdraw) {
BankAccount account = accounts.get(withdraw.getAccountId());
account.withdraw(withdraw.getAmount());
}
else if (tx instanceof TransferTransaction transfer) {
BankAccount source = accounts.get(transfer.getSourceAccountId());
BankAccount target = accounts.get(transfer.getTargetAccountId());
source.transfer(
target,
transfer.getAmount()
);
source.transfer(target, transfer.getAmount());
}
else {
throw new IllegalArgumentException(
"Unknown transaction type: " + tx.getClass()
@@ -2,22 +2,9 @@ package dev.banking.service;
import dev.banking.model.*;
import dev.banking.processor.TransactionProcessor;
import java.util.List;
import java.util.concurrent.ExecutorService;
/**
* Dispatches a list of transactions to a shared ExecutorService
* for concurrent (asynchronous) processing.
*
* Each transaction is submitted as an independent task and may
* be executed in parallel depending on thread availability.
*
* No ordering guarantees are provided between transactions.
*
* Lifecycle management of the ExecutorService (creation,
* shutdown, termination) is handled outside this class.
*/
public class BankingSystem {
private final ExecutorService executor;
@@ -34,13 +21,10 @@ public class BankingSystem {
public void processTransactions(
List<Transaction> transactions
) {
for (Transaction tx : transactions) {
executor.submit(() -> {
processor.process(tx);
});
}
}
}