-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankCodeTest.ts
124 lines (110 loc) · 3.96 KB
/
BankCodeTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Test was purposely changed to avoid giving away any answers.
* -- I failed this one, but I wanted to remember the solution I came up with.
* In my actual test, I had a lot more validation and error checking / static messages.
*/
interface Account
{
name : string,
timestamp : string,
balance : number,
transferred : number;
}
interface CashBack
{
amount : number,
timestamp : Date,
accountId : string
}
class Bank {
private accounts : Account[] = [];
private cashBacks : CashBack[] = [];
public createAccount = (account : Account) => {
account.balance = 0;
this.accounts.push(account);
}
public deposit = (accountId : string, amount : number) => {
if (amount > 0) {
const account = this.accounts.find(x => x.name === accountId);
if (account) account.balance += amount;
}
}
public transfer = (fromAccountId : string, toAccountId : string, amount : number) => {
if (amount > 0) {
const fromAccount = this.accounts.find(x => x.name === fromAccountId);
const toAccount = this.accounts.find(x => x.name === toAccountId);
if (fromAccount && toAccount && fromAccount.balance > amount) {
fromAccount.balance -= amount;
fromAccount.transferred += amount;
toAccount.balance += amount;
}
}
}
public getTopSpenders = (numberToReturn : number) => {
if (numberToReturn > 0) {
return this.accounts.sort((a, b) => b.transferred - a.transferred).slice(0, numberToReturn).map(x => `${x.name}(${x.transferred})`);
}
}
public pay = (accountId : string, amount : number) => {
if (amount > 0) {
const account = this.accounts.find(x => x.name === accountId);
if (account) account.balance -= amount;
this.cashBacks.push({ amount: Math.floor(amount * 0.02), timestamp: new Date(), accountId });
}
}
public payCashback = (i : number) => {
if (this.cashBacks.length > i) {
const cashBack = this.cashBacks[i];
const account = this.accounts.find(x => x.name === cashBack.accountId);
if (account) account.balance += cashBack.amount;
this.cashBacks.slice(i, 1);
this.payCashback(i + 1);
}
}
}
enum TransactionType
{
CreateAccount = "create_account",
Deposit = "deposit",
Transfer = "transfer",
PrintSpenders = "print_spenders",
Pay = "pay"
}
/* Provided by the test */
function solution(input: string[][]) {
const bank = new Bank();
for (let i = 0, l = input.length; i < l; i++)
{
const assertedTuple = input[i];
const transactionType = (assertedTuple[0] as TransactionType);
switch(transactionType) {
case TransactionType.CreateAccount:
bank.createAccount({ name: assertedTuple[2], timestamp: assertedTuple[1], balance: 0, transferred: 0 });
break;
case TransactionType.Deposit:
bank.deposit(assertedTuple[2], parseFloat(assertedTuple[3]));
break;
case TransactionType.Transfer:
bank.transfer(assertedTuple[2], assertedTuple[3], parseFloat(assertedTuple[4]));
break;
case TransactionType.PrintSpenders:
bank.getTopSpenders(parseInt(assertedTuple[2]))?.join(', ');
break;
case TransactionType.Pay:
bank.pay(assertedTuple[2], parseFloat(assertedTuple[3]));
break;
}
}
}
solution(
[
["create_account", "1", "account1"],
["create_account", "2", "account2"],
["deposit", "3", "account1", "12"],
["deposit", "4", "account2", "30"],
["transfer", "5", "account1", "account2", "10"],
["transfer", "6", "account2", "account1", "10"],
["print_spenders", "7", "2"],
["pay", "8", "account1", "6"],
]
)