|
| 1 | +package com.codingsignaltest.bankingsystem.level2; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +public class BankingSystem { |
| 10 | + private Map<String, Long> accountTransactionValues; // Stores total transaction value for each account |
| 11 | + |
| 12 | + public BankingSystem() { |
| 13 | + this.accountTransactionValues = new HashMap<>(); |
| 14 | + } |
| 15 | + |
| 16 | + // Simulates a deposit transaction |
| 17 | + public void deposit(String accountId, long amount) { |
| 18 | + accountTransactionValues.merge(accountId, amount, Long::sum); |
| 19 | + } |
| 20 | + |
| 21 | + // Simulates a withdrawal transaction |
| 22 | + public void withdraw(String accountId, long amount) { |
| 23 | + accountTransactionValues.merge(accountId, amount, Long::sum); |
| 24 | + } |
| 25 | + |
| 26 | + // Simulates a transfer transaction (ignoring for now as per problem description) |
| 27 | + public void transfer(String fromAccountId, String toAccountId, long amount) { |
| 28 | + // For now, we'll just add the 'amount' to both accounts' total transaction value |
| 29 | + // as per the definition "sum of all transactions ... including ... transferred" |
| 30 | + accountTransactionValues.merge(fromAccountId, amount, Long::sum); |
| 31 | + accountTransactionValues.merge(toAccountId, amount, Long::sum); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns the top n accounts with the highest total value of transactions in descending order. |
| 36 | + * The timestamp parameter is currently ignored as transaction history isn't maintained in this simplified model. |
| 37 | + * |
| 38 | + * @param timestamp An ignored timestamp (for future extensions). |
| 39 | + * @param n The number of top accounts to return. |
| 40 | + * @return A string representing an array of accounts and transaction values in the format "<accountId1>(<transactionValue1>)". |
| 41 | + */ |
| 42 | + public String topActivity(long timestamp, int n) { |
| 43 | + // Create a list of map entries to sort |
| 44 | + List<Map.Entry<String, Long>> sortedAccounts = new ArrayList<>(accountTransactionValues.entrySet()); |
| 45 | + |
| 46 | + // Sort accounts by transaction value in descending order |
| 47 | + sortedAccounts.sort(Map.Entry.<String, Long>comparingByValue().reversed()); |
| 48 | + |
| 49 | + // Get the top n accounts or all accounts if less than n exist |
| 50 | + List<Map.Entry<String, Long>> topNAccounts = sortedAccounts.stream() |
| 51 | + .limit(n) |
| 52 | + .collect(Collectors.toList()); |
| 53 | + |
| 54 | + // Format the output string |
| 55 | + StringBuilder result = new StringBuilder("["); |
| 56 | + for (int i = 0; i < topNAccounts.size(); i++) { |
| 57 | + Map.Entry<String, Long> entry = topNAccounts.get(i); |
| 58 | + result.append(entry.getKey()).append("(").append(entry.getValue()).append(")"); |
| 59 | + if (i < topNAccounts.size() - 1) { |
| 60 | + result.append(", "); |
| 61 | + } |
| 62 | + } |
| 63 | + result.append("]"); |
| 64 | + return result.toString(); |
| 65 | + } |
| 66 | + |
| 67 | + public static void main(String[] args) { |
| 68 | + BankingSystem bankingSystem = new BankingSystem(); |
| 69 | + |
| 70 | + bankingSystem.deposit("accountA", 100); |
| 71 | + bankingSystem.withdraw("accountB", 50); |
| 72 | + bankingSystem.deposit("accountA", 200); |
| 73 | + bankingSystem.transfer("accountA", "accountC", 75); |
| 74 | + bankingSystem.deposit("accountD", 300); |
| 75 | + |
| 76 | + System.out.println(bankingSystem.topActivity(0, 2)); // Expected: [accountA(375), accountD(300)] |
| 77 | + System.out.println(bankingSystem.topActivity(0, 3)); // Expected: [accountA(375), accountD(300), accountC(75)] |
| 78 | + System.out.println(bankingSystem.topActivity(0, 5)); // Expected: [accountA(375), accountD(300), accountC(75), accountB(50)] |
| 79 | + System.out.println(bankingSystem.topActivity(0, 1)); // Expected: [accountA(375)] |
| 80 | + } |
| 81 | +} |
0 commit comments