Account Management (Open & Close Accounts):
SavingsAccount: Enforces a minimum balance threshold (minBalance) and calculates monthly interest (calculateInterest).CheckingAccount: Provides overdraft protection (overdraftLimit).Deposit & Withdrawal:
Atomic Fund Transfers:
Transaction History & Statements:
Transaction record (Deposit, Withdrawal, Transfer) with timestamps and status.from, to).Real-time Fraud Monitoring:
Thread Safety & Race-Free: Concurrent deposits, withdrawals, and transfers on the same account must not corrupt balances or cause lost updates.
Deadlock Prevention: High-concurrency bi-directional transfers (e.g., Account A →\rightarrow→ B and Account B →\rightarrow→ A concurrently) must execute without deadlocks.
Extensibility (Open/Closed Principle): New account types (e.g., MoneyMarketAccount, CDAccount), new transaction types (e.g., ScheduledPayment), or new monitoring observers must be pluggable without modifying existing core code.
Data Integrity & Consistency: Money cannot be created or destroyed out of thin air. Total balances across the system must remain strictly conserved
AccountType: SAVINGS, CHECKINGTransactionType: DEPOSIT, WITHDRAWAL, TRANSFERTransactionStatus: PENDING, COMPLETED, FAILED, FLAGGEDAccount (Interface): GetID(), GetCustomerID(), GetBalance(), GetAccountType()Deposit(amount), Withdraw(amount), CanWithdraw(amount)CanWithdrawDirect(amount), DepositDirect(amount), WithdrawDirect(amount)AddTransaction(tx), GetStatement(from, to), Lock(), Unlock()BaseAccount: id, customerID, balance, transactions, mu sync.MutexSavingsAccount: interestRate, minBalance, CanWithdrawDirect(), CalculateInterest()CheckingAccount: overdraftLimit, CanWithdrawDirect()Customer: id, name, email, phone, accounts []Account, AddAccount(), RemoveAccount()Transaction (Interface): GetID(), GetFromAccountID(), GetAmount(), GetStatus(), Execute(registry)Deposit: Credits target account, records history, sets COMPLETED.Withdrawal: Debits target account after balance check, records history, sets COMPLETED.Transfer: Holds toAccountID; acquires locks in ascending AccountID order, debits source, credits destination, records history on both accounts.InterestCalculationStrategy: Calculate(balance, rate) →\rightarrow→ SimpleInterestStrategy, CompoundInterestStrategy, TieredInterestStrategy.TransactionObserver: OnTransaction(tx) →\rightarrow→ FraudDetector (checks large amount threshold and sliding-window velocity).TransactionManager: Executes transactions polymorphically and notifies observers.BankService / Bank: High-level facade for customer registration, account opening/closing, and transaction execution.
1. Bank
- Attributes:
* name: string
* customers: Map
* accountRegistry: AccountRegistry
* transactionManager: TransactionManager
* accountCounter: atomic int64
* customerCounter: atomic int64
* txCounter: atomic int64
* customerMu: RWMutex
- Methods:
* registerCustomer(name, email, phone, address): Customer
* createAccount(customer, accountType, initialDeposit): Account
* closeAccount(accountId): void
* deposit(accountId, amount): Transaction
* withdraw(accountId, amount): Transaction
* transfer(fromAccountId, toAccountId, amount): Transaction
2. AccountRegistry (Single Owner of Accounts Map)
- Attributes:
* accounts: Map
* mu: RWMutex
- Methods:
* register(account): void
* unregister(accountId): void
* get(accountId): (Account, bool)
* getSnapshot(): Map
3. Customer
- Attributes:
* customerId: string
* name: string
* email: string
* phone: string
* address: string
* accounts: List
* mu: RWMutex
- Methods:
* addAccount(account): void
* removeAccount(accountId): void
* getAccounts(): List
* getCustomerId(), getName()
4. Account (Interface) & BaseAccount
- Attributes:
* accountId: string
* customerId: string
* accountType: AccountType
* balance: double
* createdAt: timestamp
* transactions: List
* mu: Mutex
- Methods:
* deposit(amount): void
* withdraw(amount): void
* canWithdraw(amount): boolean (abstract)
* canWithdrawDirect(amount): boolean
* depositDirect(amount): void
* withdrawDirect(amount): void
* addTransaction(transaction): void
* getStatement(from, to): List
* getBalance(), getAccountId(), getCustomerId()
* lock(), unlock()
5. SavingsAccount (extends Account)
- Attributes:
* interestRate: double
* minBalance: double
- Methods:
* canWithdrawDirect(amount): boolean -> (balance - amount) >= minBalance
* calculateInterest(): double -> balance * interestRate / 12
6. CheckingAccount (extends Account)
- Attributes:
* overdraftLimit: double
- Methods:
* canWithdrawDirect(amount): boolean -> (balance + overdraftLimit) >= amount
7. Transaction (Interface) & BaseTransaction
- Attributes:
* transactionId: string
* fromAccountId: string
* amount: double
* timestamp: timestamp
* type: TransactionType
* status: TransactionStatus
- Methods:
* execute(registry): void (abstract)
* getTransactionId(), getAmount(), getStatus(), setStatus(status)
8. Deposit (extends Transaction)
- Methods:
* execute(registry): void -> credits target account, appends history, marks COMPLETED
9. Withdrawal (extends Transaction)
- Methods:
* execute(registry): void -> debits target account after balance check, appends history, marks COMPLETED
10. Transfer (extends Transaction)
- Attributes:
* toAccountId: string
- Methods:
* getToAccountId(): string
* execute(registry): void -> locks accounts in ascending ID order (min(from, to) first), debits source, credits destination, appends history to both accounts, marks COMPLETED
11. TransactionManager
- Attributes:
* observers: List
* registry: AccountRegistry
* mu: RWMutex
- Methods:
* processTransaction(transaction): void -> executes transaction polymorphically, calls notifyObservers()
* addObserver(observer): void
* removeObserver(observer): void
* notifyObservers(transaction): void (invokes observers outside lock)
12. FraudDetector (implements TransactionObserver)
- Attributes:
* largeAmountThreshold: double
* velocityThreshold: int
* velocityWindow: duration
* recentActivity: Map
* mu: Mutex
- Methods:
* onTransaction(transaction): void -> prunes sliding window activity in memory, alerts on large amount and high velocity
13. InterestCalculationStrategy (Interface)
- Implementations:
* SimpleInterestStrategy: calculate(balance, rate) -> balance * rate / 12