-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.js
61 lines (49 loc) · 1.58 KB
/
account.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 Transaction = require('./transaction')
class Account {
constructor(transaction = Transaction) {
this.balance = 0;
this.transaction = transaction;
this.allTransactions = [];
}
setBalance(amount) {
this.balance += amount;
}
deposit(amount) {
const creditAmount = this.#formatMoney(amount);
this.balance += amount;
const newTransaction = new Transaction({ credit: amount, balance: this.balance });
this.allTransactions.unshift(newTransaction);
return `£${creditAmount} deposited. Balance is £${this.#formatMoney(this.balance)}`
}
withdraw(amount) {
if (this.#checkWithdrawal(amount)){
const debitAmount = this.#formatMoney(amount);
if (debitAmount > this.balance) return "Must acquire additional resources"
this.balance -= amount;
const newTransaction = new Transaction({ debit: amount, balance: this.balance });
this.allTransactions.unshift(newTransaction);
return `Withdrew £${debitAmount}. Balance is £${this.#formatMoney(this.balance)}`
} else {
return "Withdrawal amount must be positive";
}
}
printStatement() {
if (this.allTransactions.length !== 0) {
return this.printHeading() + this.allTransactions.map((item) => {
return(`${item.showTransaction()}`);
}).join("\n");
} else {
console.log(` || || || `);
}
}
printHeading() {
return "DATE || CREDIT || DEBIT || BALANCE\n"
}
#checkWithdrawal(amount) {
return (amount < 0 ? false : true)
}
#formatMoney(value) {
return value.toFixed(2);
}
}
module.exports = Account