-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAction.js
48 lines (36 loc) · 975 Bytes
/
Action.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
const History = require("./History");
class Action {
constructor(amount, balance) {
this.amount = amount;
this.balance = balance;
this.action = "";
}
canWithdraw(balance) {
if ((balance - this.amount) < 0 ) {
this.amount = this.calculateWithdrawal(balance);
}
}
withdraw(amount, balance) {
this.action = 'withdraw';
this.amount = amount;
this.canWithdraw(balance);
this.balance = balance - this.amount;
return this.saveHistory();
}
calculateWithdrawal(balance) {
let difference = this.amount - balance;
return this.amount - difference;
}
deposit(amount, balance) {
this.action = 'deposit';
this.amount = amount;
this.balance = balance + amount;
return this.saveHistory();
}
saveHistory() {
const history = new History();
const actionHistory = history.returnHistory(this.amount, this.balance, this.action);
return actionHistory;
}
}
module.exports = Action;