Create an object-oriented banking service that manages customer accounts and supports the following operations:
N accounts with the greatest number of successful transactions.The design should report invalid operations clearly, such as using an unknown account, reusing an account identifier, supplying a non-positive amount, or withdrawing or transferring more money than is available. Consider how the design could later enforce correctness when multiple operations execute concurrently.
Use an object-oriented interface with behavior equivalent to the following signatures:
BankingSystem()
void createAccount(String accountId)
void deposit(String accountId, long amount)
void withdraw(String accountId, long amount)
void transfer(String fromAccountId, String toAccountId, long amount)
List<Transaction> getTransactions(String accountId)
List<String> getTopActiveAccounts(int n)
A successful deposit, withdrawal, or transfer must be logged for every account affected by that operation. For a transfer, the source and destination accounts each receive a corresponding transaction record. Failed operations must not change balances or histories.
Define a deterministic tie rule for accounts with equal activity; account identifiers in ascending order are used here. An account's activity is the number of successful transaction records associated with it. The result should contain at most n account identifiers.
Input:
createAccount("acct-14")
deposit("acct-14", 80)
withdraw("acct-14", 25)
getTransactions("acct-14")
Output:
balance = 55
transactions = [DEPOSIT 80, WITHDRAW 25]
The account receives two successful operations, and its balance is 80 - 25 = 55.
Input:
createAccount("acct-21")
createAccount("acct-34")
deposit("acct-21", 60)
transfer("acct-21", "acct-34", 15)
getTopActiveAccounts(2)
Output:
acct-21 balance = 45
acct-34 balance = 15
top accounts = ["acct-21", "acct-34"]
The deposit and outgoing transfer give acct-21 two records, while the incoming transfer gives acct-34 one, so the source account ranks first.
Input:
createAccount("acct-52")
withdraw("acct-52", 10)
getTransactions("acct-52")
Output:
error: insufficient funds
balance = 0
transactions = []
The withdrawal is rejected because the newly created account has no money, so neither its balance nor its history changes.
n must be non-negative.N queries?