forked from HackYourFuture/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2-classes.js
More file actions
93 lines (74 loc) · 1.9 KB
/
Copy pathex2-classes.js
File metadata and controls
93 lines (74 loc) · 1.9 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
85
86
87
88
89
90
91
92
93
import eurosFormatter from "./euroFormatter.js";
class Wallet {
#name;
#cash;
#dailyAllowance;
#dayTotalWithdrawals;
constructor(name, cash = 0) {
this.#name = name;
this.#cash = cash;
this.#dailyAllowance = 40;
this.#dayTotalWithdrawals = 0;
}
get name() {
return this.#name;
}
deposit(amount) {
this.#cash += amount;
}
withdraw(amount) {
if (this.#cash < amount) {
console.log(`Insufficient funds!`);
return 0;
}
if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) {
console.log(`Insufficient remaining daily allowance!`);
return 0;
}
this.#cash -= amount;
this.#dayTotalWithdrawals += amount;
return amount;
}
transferInto(wallet, amount) {
console.log(
`Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${
wallet.name
}`
);
const withdrawnAmount = this.withdraw(amount);
if (withdrawnAmount > 0) {
wallet.deposit(withdrawnAmount);
}
}
setDailyAllowance(newAllowance) {
this.#dailyAllowance = newAllowance;
console.log(
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
);
}
resetDailyAllowance() {
this.#dayTotalWithdrawals = 0;
}
reportBalance() {
console.log(
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
);
}
}
function main() {
const walletJack = new Wallet("Jack", 100);
const walletJoe = new Wallet("Joe", 10);
const walletJane = new Wallet("Jane", 20);
walletJack.transferInto(walletJoe, 50);
walletJane.transferInto(walletJoe, 25);
walletJane.deposit(20);
walletJane.transferInto(walletJoe, 25);
walletJack.reportBalance();
walletJoe.reportBalance();
walletJane.reportBalance();
walletJane.setDailyAllowance(100);
walletJane.resetDailyAllowance();
walletJane.transferInto(walletJoe, 50);
walletJane.reportBalance();
}
main();