Skip to content

Commit 8f603fc

Browse files
committed
Banking System correction
1 parent 98626e9 commit 8f603fc

2 files changed

Lines changed: 99 additions & 11 deletions

File tree

src/main/java/com/codingsignaltest/bankingsystem/level1/BankingSystem.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.util.HashMap;
1818

1919
public class BankingSystem {
20-
private HashMap<String, Integer> accounts;
20+
private HashMap<String, Double> accounts;
2121

2222
public BankingSystem(){
2323
accounts = new HashMap<>();
@@ -28,30 +28,37 @@ public boolean createAccount(String timestamp,String accountId){
2828
if(accounts.containsKey(accountId)){
2929
return false; // Account already exists
3030
}
31-
accounts.put(accountId,0);
31+
accounts.put(accountId,0.0);
3232
return true;
3333
}
3434
// Deposit money into accountid, amount, timestamp
35-
public String deposit(String timestamp, String accountId, int amount) {
35+
public String deposit(String timestamp, String accountId, double amount) {
3636
if(!accounts.containsKey(accountId)) {
37-
return "Account does not exist";
37+
return "";
3838
}
39-
int newBalance = accounts.get(accountId) + amount;
39+
40+
if (amount <= 0) {
41+
return String.valueOf(accounts.get(accountId));
42+
}
43+
44+
double newBalance = accounts.get(accountId) + amount;
4045
accounts.put(accountId, newBalance);
4146
return "Deposit successful. New balance: " + newBalance;
4247
}
4348

4449
// Pay <timestamp>, <accountId>, <amount>
45-
public String pay(String timestamp, String accountId, int amount) {
50+
public String pay(String timestamp, String accountId, double amount) {
4651
if(!accounts.containsKey(accountId)){
47-
return "Account does not exist";
52+
return "";
4853
}
49-
50-
int currentBalance = accounts.get(accountId);
54+
if (amount <= 0) {
55+
return String.valueOf(accounts.get(accountId));
56+
}
57+
double currentBalance = accounts.get(accountId);
5158
if(currentBalance < amount) {
52-
return "Insufficient funds";
59+
return "";
5360
}
54-
int newBalance = currentBalance - amount;
61+
double newBalance = currentBalance - amount;
5562
accounts.put(accountId, newBalance);
5663
return String.valueOf(newBalance);
5764
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)