-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
50 lines (40 loc) · 1.35 KB
/
main.js
File metadata and controls
50 lines (40 loc) · 1.35 KB
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
'use strict';
const Bank = require('./bank.js');
const EventBus = require('./eventBus.js');
const FraudService = require('./services/fraud.js');
const DebitService = require('./services/debit.js');
const CreditService = require('./services/credit.js');
const NotificationService = require('./services/notifications.js');
const AuditService = require('./services/audit.js');
const accounts = require('./data/accounts.json');
const transactions = require('./data/transactions.json');
const bank = new Bank(accounts);
const bus = new EventBus();
const services = [
new FraudService(bus, ['Commodus']),
new DebitService(bus, bank),
new CreditService(bus, bank),
new NotificationService(bus, ['Antoninus Pius']),
new AuditService(bus),
];
const showBalances = () => {
console.log('--- Balances ---');
for (const [name] of bank.accounts) {
const balance = bank.getBalance(name);
console.log(`${name}: ${balance}`);
}
};
const runTransfer = (context) => {
const { from, to, amount } = context;
console.log(`\nTransfer requested: ${from} -> ${to} (${amount})`);
bus.send('TransferRequested', { ...context });
showBalances();
};
services.forEach((service) => {
console.log(`Service: ${service.constructor.name} is ready`);
});
console.log('\nInitial balances:');
showBalances();
for (const transaction of transactions) {
runTransfer(transaction);
}