Skip to content

Commit a21408d

Browse files
committed
Done prep-w4
1 parent dc2ab77 commit a21408d

5 files changed

Lines changed: 260 additions & 166 deletions

File tree

Lines changed: 72 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,84 @@
11
import eurosFormatter from './euroFormatter.js';
22

33
class Wallet {
4-
#name;
5-
#cash;
6-
7-
constructor(name, cash) {
8-
this.#name = name;
9-
this.#cash = cash;
10-
}
11-
12-
get name() {
13-
return this.#name;
14-
}
15-
16-
deposit(amount) {
17-
this.#cash += amount;
18-
}
19-
20-
withdraw(amount) {
21-
if (this.#cash - amount < 0) {
22-
console.log(`Insufficient funds!`);
23-
return 0;
24-
}
25-
26-
this.#cash -= amount;
27-
return amount;
28-
}
29-
30-
transferInto(wallet, amount) {
31-
console.log(
32-
`Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${
33-
wallet.name
34-
}`
35-
);
36-
const withdrawnAmount = this.withdraw(amount);
37-
wallet.deposit(withdrawnAmount);
38-
}
39-
40-
reportBalance() {
41-
console.log(
42-
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
43-
);
44-
}
4+
#name;
5+
#cash;
6+
7+
constructor(name, cash = 0) {
8+
this.#name = name;
9+
this.#cash = cash;
10+
this.dailyAllowance = 40;
11+
this.dayTotalWithdrawals = 0;
12+
}
13+
14+
get name() {
15+
return this.#name;
16+
}
17+
18+
deposit(amount) {
19+
this.#cash += amount;
20+
}
21+
22+
withdraw(amount) {
23+
if (this.#cash - amount < 0) {
24+
console.log(`Insufficient funds!`);
25+
return 0;
26+
}
27+
28+
if (this.dayTotalWithdrawals + amount > this.dailyAllowance) {
29+
console.log(`Insufficient remaining daily allowance!`);
30+
return 0;
31+
}
32+
33+
this.#cash -= amount;
34+
return amount;
35+
}
36+
37+
transferInto(wallet, amount) {
38+
console.log(
39+
`Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${wallet.name
40+
}`
41+
);
42+
const withdrawnAmount = this.withdraw(amount);
43+
wallet.deposit(withdrawnAmount);
44+
}
45+
46+
setDailyAllowance(newAllowance) {
47+
this.dailyAllowance = newAllowance;
48+
console.log(
49+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
50+
);
51+
}
52+
53+
resetDailyAllowance() {
54+
this.dayTotalWithdrawals = 0;
55+
}
56+
57+
58+
reportBalance() {
59+
console.log(
60+
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
61+
);
62+
}
4563
}
4664

4765
function main() {
48-
const walletJack = new Wallet('Jack', 100);
49-
const walletJoe = new Wallet('Joe', 10);
50-
const walletJane = new Wallet('Jane', 20);
66+
const walletJack = new Wallet('Jack', 100);
67+
const walletJoe = new Wallet('Joe', 10);
68+
const walletJane = new Wallet('Jane', 20);
69+
70+
walletJack.transferInto(walletJoe, 50);
71+
72+
walletJack.setDailyAllowance(80);
5173

52-
walletJack.transferInto(walletJoe, 50);
53-
walletJane.transferInto(walletJoe, 25);
74+
walletJane.transferInto(walletJoe, 25);
5475

55-
walletJane.deposit(20);
56-
walletJane.transferInto(walletJoe, 25);
76+
walletJane.deposit(20);
77+
walletJane.transferInto(walletJoe, 25);
5778

58-
walletJack.reportBalance();
59-
walletJoe.reportBalance();
60-
walletJane.reportBalance();
79+
walletJack.reportBalance();
80+
walletJoe.reportBalance();
81+
walletJane.reportBalance();
6182
}
6283

6384
main();
Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,80 @@
11
import eurosFormatter from './euroFormatter.js';
22

33
function createWallet(name, cash = 0) {
4-
return {
5-
_name: name,
6-
_cash: cash,
7-
8-
deposit: function (amount) {
9-
this._cash += amount;
10-
},
11-
12-
withdraw: function (amount) {
13-
if (this._cash - amount < 0) {
14-
console.log(`Insufficient funds!`);
15-
return 0;
16-
}
17-
18-
this._cash -= amount;
19-
return amount;
20-
},
21-
22-
transferInto: function (wallet, amount) {
23-
console.log(
24-
`Transferring ${eurosFormatter.format(amount)} from ${
25-
this._name
26-
} to ${wallet.getName()}`
27-
);
28-
const withdrawnAmount = this.withdraw(amount);
29-
wallet.deposit(withdrawnAmount);
30-
},
31-
32-
reportBalance: function () {
33-
console.log(
34-
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
35-
);
36-
},
37-
38-
getName: function () {
39-
return this._name;
40-
},
41-
};
4+
return {
5+
_name: name,
6+
_cash: cash,
7+
dailyAllowance: 40,
8+
dayTotalWithdrawals: 0,
9+
10+
deposit: function (amount) {
11+
this._cash += amount;
12+
},
13+
14+
withdraw: function (amount) {
15+
if (this._cash - amount < 0) {
16+
console.log(`Insufficient funds!`);
17+
return 0;
18+
}
19+
20+
if (this.dayTotalWithdrawals + amount > this.dailyAllowance) {
21+
console.log(`Insufficient remaining daily allowance!`);
22+
return 0;
23+
}
24+
25+
this._cash -= amount;
26+
return amount;
27+
},
28+
29+
transferInto: function (wallet, amount) {
30+
console.log(
31+
`Transferring ${eurosFormatter.format(amount)} from ${this._name
32+
} to ${wallet.getName()}`
33+
);
34+
const withdrawnAmount = this.withdraw(amount);
35+
wallet.deposit(withdrawnAmount);
36+
},
37+
38+
setDailyAllowance: function (newAllowance) {
39+
this.dailyAllowance = newAllowance;
40+
console.log(
41+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
42+
);
43+
},
44+
45+
resetDailyAllowance: function () {
46+
dayTotalWithdrawals = 0;
47+
},
48+
49+
reportBalance: function () {
50+
console.log(
51+
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
52+
);
53+
},
54+
55+
getName: function () {
56+
return this._name;
57+
},
58+
};
4259
}
4360

4461
function main() {
45-
const walletJack = createWallet('Jack', 100);
46-
const walletJoe = createWallet('Joe', 10);
47-
const walletJane = createWallet('Jane', 20);
62+
const walletJack = createWallet('Jack', 100);
63+
const walletJoe = createWallet('Joe', 10);
64+
const walletJane = createWallet('Jane', 20);
65+
66+
walletJack.transferInto(walletJoe, 50);
67+
68+
walletJack.setDailyAllowance(80);
4869

49-
walletJack.transferInto(walletJoe, 50);
50-
walletJane.transferInto(walletJoe, 25);
70+
walletJane.transferInto(walletJoe, 25);
5171

52-
walletJane.deposit(20);
53-
walletJane.transferInto(walletJoe, 25);
72+
walletJane.deposit(20);
73+
walletJane.transferInto(walletJoe, 25);
5474

55-
walletJack.reportBalance();
56-
walletJoe.reportBalance();
57-
walletJane.reportBalance();
75+
walletJack.reportBalance();
76+
walletJoe.reportBalance();
77+
walletJane.reportBalance();
5878
}
5979

6080
main();
Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,87 @@
11
import eurosFormatter from './euroFormatter.js';
22

33
function deposit(amount) {
4-
this._cash += amount;
4+
this._cash += amount;
55
}
66

77
function withdraw(amount) {
8-
if (this._cash - amount < 0) {
9-
console.log(`Insufficient funds!`);
10-
return 0;
11-
}
8+
if (this._cash - amount < 0) {
9+
console.log(`Insufficient funds!`);
10+
return 0;
11+
}
1212

13-
this._cash -= amount;
14-
return amount;
13+
if (this.dayTotalWithdrawals + amount > this.dailyAllowance) {
14+
console.log(`Insufficient remaining daily allowance!`);
15+
return 0;
16+
}
17+
18+
this._cash -= amount;
19+
return amount;
1520
}
1621

1722
function transferInto(wallet, amount) {
18-
console.log(
19-
`Transferring ${eurosFormatter.format(amount)} from ${
20-
this._name
21-
} to ${wallet.getName()}`
22-
);
23-
const withdrawnAmount = this.withdraw(amount);
24-
wallet.deposit(withdrawnAmount);
23+
console.log(
24+
`Transferring ${eurosFormatter.format(amount)} from ${this._name
25+
} to ${wallet.getName()}`
26+
);
27+
const withdrawnAmount = this.withdraw(amount);
28+
wallet.deposit(withdrawnAmount);
29+
}
30+
31+
function setDailyAllowance(newAllowance) {
32+
this.dailyAllowance = newAllowance;
33+
console.log(
34+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
35+
);
2536
}
2637

38+
39+
function resetDailyAllowance() {
40+
this.dayTotalWithdrawals = 0;
41+
}
42+
43+
2744
function reportBalance() {
28-
console.log(
29-
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
30-
);
45+
console.log(
46+
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
47+
);
3148
}
3249

3350
function getName() {
34-
return this._name;
51+
return this._name;
3552
}
3653

3754
function createWallet(name, cash = 0) {
38-
return {
39-
_name: name,
40-
_cash: cash,
41-
deposit,
42-
withdraw,
43-
transferInto,
44-
reportBalance,
45-
getName,
46-
};
55+
return {
56+
_name: name,
57+
_cash: cash,
58+
dailyAllowance: 40,
59+
dayTotalWithdrawals: 0,
60+
deposit,
61+
withdraw,
62+
transferInto,
63+
setDailyAllowance,
64+
resetDailyAllowance,
65+
reportBalance,
66+
getName,
67+
};
4768
}
4869

4970
function main() {
50-
const walletJack = createWallet('Jack', 100);
51-
const walletJoe = createWallet('Joe', 10);
52-
const walletJane = createWallet('Jane', 20);
71+
const walletJack = createWallet('Jack', 100);
72+
const walletJoe = createWallet('Joe', 10);
73+
const walletJane = createWallet('Jane', 20);
5374

54-
walletJack.transferInto(walletJoe, 50);
55-
walletJane.transferInto(walletJoe, 25);
75+
walletJack.transferInto(walletJoe, 50);
76+
walletJack.setDailyAllowance(80);
77+
walletJane.transferInto(walletJoe, 25);
5678

57-
walletJane.deposit(20);
58-
walletJane.transferInto(walletJoe, 25);
79+
walletJane.deposit(20);
80+
walletJane.transferInto(walletJoe, 25);
5981

60-
walletJack.reportBalance();
61-
walletJoe.reportBalance();
62-
walletJane.reportBalance();
82+
walletJack.reportBalance();
83+
walletJoe.reportBalance();
84+
walletJane.reportBalance();
6385
}
6486

6587
main();

0 commit comments

Comments
 (0)