-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
61 lines (53 loc) · 1.78 KB
/
main.js
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
const userInput = require('./userInput');
class Account {
constructor(name, balance) {
this.name = name;
this.balance = balance;
}
transaction(to, amount) {
this.balance = this.balance - amount;
to.balance = to.balance + amount;
}
}
exports.performProcessing = function(names, transactions, logger) {
while (true) {
let accountList = getAccounts(names);
processTransfers(transactions, accountList, logger);
processUserCommand(accountList, transactions);
}
};
function processTransfers(transactions, accountList, logger) {
transactions.forEach((transaction) => {
let fromPerson = accountList.find((account) => {
return account.name === transaction.from;
});
let toPerson = accountList.find((account) => {
return account.name === transaction.to;
});
if (!isNaN(transaction.amount)) {
fromPerson.transaction(toPerson, parseFloat(transaction.amount));
} else {
logger.debug('transfer amount is not a number');
}
});
}
function getAccounts(names) {
return names.map((name) => {
return new Account(name, 0);
});
}
function processUserCommand(accountList, transactions) {
let command = userInput.getStringWithPrompt('What would you like to know?');
if (command === 'List All') {
console.log(accountList);
} else if (command.substr(0, 4) === 'List') {
let person = command.substr(5);
let personTrans = getTransfersForPerson(person, transactions);
console.log(personTrans);
}
}
function getTransfersForPerson(person, transactions) {
return transactions.filter((transaction) => {
return transaction.from === person || transaction.to === person;
});
}