forked from HackYourFuture/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex3-object.js
More file actions
84 lines (67 loc) · 2 KB
/
Copy pathex3-object.js
File metadata and controls
84 lines (67 loc) · 2 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import eurosFormatter from "./euroFormatter.js";
function createWallet(name, cash = 0) {
return {
_name: name,
_cash: cash,
_dailyAllowance: 40,
_dayTotalWithdrawals: 0,
deposit: function (amount) {
this._cash += amount;
},
withdraw: function (amount) {
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
console.log(`Daily allowance exceeded!`);
return 0;
}
if (this._cash - amount < 0) {
console.log(`Insufficient funds!`);
return 0;
}
this._cash -= amount;
this._dayTotalWithdrawals += amount;
return amount;
},
transferInto: function (wallet, amount) {
console.log(
`Transferring ${eurosFormatter.format(amount)} from ${
this._name
} to ${wallet.getName()}`
);
const withdrawnAmount = this.withdraw(amount);
if (withdrawnAmount > 0) {
wallet.deposit(withdrawnAmount);
}
},
reportBalance: function () {
console.log(
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
);
},
getName: function () {
return this._name;
},
resetDailyAllowance: function () {
this._dayTotalWithdrawals = 0;
},
setDailyAllowance: function (newAllowance) {
this._dailyAllowance = newAllowance;
},
};
}
function main() {
const walletJack = createWallet("Jack", 100);
const walletJoe = createWallet("Joe", 10);
const walletJane = createWallet("Jane", 20);
walletJack.transferInto(walletJoe, 50);
walletJane.transferInto(walletJoe, 25); // Exceeds limit (default = 40)
walletJane.deposit(20);
walletJane.transferInto(walletJoe, 25); // OK if still within daily limit
walletJack.reportBalance();
walletJoe.reportBalance();
walletJane.reportBalance();
walletJane.setDailyAllowance(100);
walletJane.resetDailyAllowance();
walletJane.transferInto(walletJoe, 50); // Now works after reset + increased limit
walletJane.reportBalance();
}
main();