-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankAccount.js
51 lines (42 loc) · 1.4 KB
/
bankAccount.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
const Transaction = require('./transaction')
class BankAccount {
constructor(transaction = Transaction) {
this.balance = 0.00;
this.transactions = [];
this.transaction = transaction;
this.statement = [];
this.header = 'date || credit || debit || balance';
}
getBalance() {
return this.balance;
}
depositMoney(money) {
this.balance += money;
this.transactions.push(new this.transaction('deposit', money, this.balance))
return this.balance;
}
withdrawMoney(money) {
this.balance -= money;
this.transactions.push(new this.transaction('withdraw', money, this.balance))
return this.balance;
}
viewStatement(transactions = this.transactions ) {
const statement = `${this.header}\n`
+ `${this.sortTransactions(transactions)}`;
return console.log(statement);
}
sortTransactions(transactions) {
transactions.forEach((transaction) => {
this.transactionFormat(transaction);
})
return this.statement.reverse().join('\n');
}
transactionFormat(transaction) {
if(transaction.type === 'deposit') {
this.statement.push(`${transaction.date} || ${transaction.amount.toFixed(2)} || || ${transaction.balance.toFixed(2)}`);
} if(transaction.type === 'withdraw') {
this.statement.push(`${transaction.date} || || ${transaction.amount.toFixed(2)} || ${transaction.balance.toFixed(2)}`);
}
}
}
module.exports = BankAccount;