-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbank.js
More file actions
46 lines (39 loc) · 917 Bytes
/
bank.js
File metadata and controls
46 lines (39 loc) · 917 Bytes
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
'use strict';
class BankAccount {
constructor(name) {
this.name = name;
this.balance = 0;
}
}
const OPERATIONS = {
create: (command, bank) => {
const account = bank.find(command.account);
if (!account) bank.createAccount(command.account);
},
withdraw: (command, bank) => {
const account = bank.find(command.account);
account.balance -= command.amount;
},
income: (command, bank) => {
const account = bank.find(command.account);
account.balance += command.amount;
},
};
class Bank {
constructor() {
this.accounts = new Map();
}
createAccount(name) {
const account = new BankAccount(name);
this.accounts.set(name, account);
}
find(name) {
return this.accounts.get(name);
}
execute(command) {
const operation = OPERATIONS[command.operation];
operation(command, this);
console.dir(command);
}
}
module.exports = { Bank };