Skip to content

Commit 2e8d071

Browse files
committed
Add solution to prep exercises week4
1 parent 3ba2581 commit 2e8d071

6 files changed

Lines changed: 95 additions & 6 deletions

File tree

File renamed without changes.

Week4/prep-exercises/1-wallet/ex1-closure-example.js renamed to Week4/prep-exercises/1-wallet/ex1-closure-example.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import eurosFormatter from "./euroFormatter.js";
1+
import eurosFormatter from "./euroFormatter.mjs";
22

33
/**
4-
* This is the closure way of doing things and we have already completed it for you so you don't need to do anything.
4+
* This is the closure way of doing things and we have already completed it for you so
5+
* you don't need to do anything.
56
* We leave it here as an example of how your other implementations should work!
67
*/
78

Week4/prep-exercises/1-wallet/ex2-classes.js renamed to Week4/prep-exercises/1-wallet/ex2-classes.mjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from './euroFormatter.mjs';
22

33
class Wallet {
44
#name;
55
#cash;
6+
#dailyAllowance;
7+
#dayTotalWithdrawals;
68

79
constructor(name, cash) {
810
this.#name = name;
911
this.#cash = cash;
12+
this.#dailyAllowance = 40;
13+
this.#dayTotalWithdrawals = 0;
1014
}
1115

1216
get name() {
@@ -22,8 +26,13 @@ class Wallet {
2226
console.log(`Insufficient funds!`);
2327
return 0;
2428
}
29+
if(this.#dayTotalWithdrawals + amount > this.#dailyAllowance){
30+
console.log(`Insufficient remaining daily allowance!`);
31+
return 0;
32+
}
2533

2634
this.#cash -= amount;
35+
this.#dayTotalWithdrawals += amount;
2736
return amount;
2837
}
2938

@@ -37,6 +46,17 @@ class Wallet {
3746
wallet.deposit(withdrawnAmount);
3847
}
3948

49+
resetDailyAllowance(){
50+
this.#dayTotalWithdrawals = 0;
51+
}
52+
53+
setDailyAllowance(newAllowance){
54+
this.#dailyAllowance = newAllowance;
55+
console.log(
56+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
57+
);
58+
}
59+
4060
reportBalance() {
4161
console.log(
4262
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
@@ -50,6 +70,9 @@ function main() {
5070
const walletJane = new Wallet('Jane', 20);
5171

5272
walletJack.transferInto(walletJoe, 50);
73+
walletJack.setDailyAllowance(80);
74+
walletJack.transferInto(walletJoe, 50);
75+
5376
walletJane.transferInto(walletJoe, 25);
5477

5578
walletJane.deposit(20);

Week4/prep-exercises/1-wallet/ex3-object.js renamed to Week4/prep-exercises/1-wallet/ex3-object.mjs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from './euroFormatter.mjs';
22

33
function createWallet(name, cash = 0) {
44
return {
55
_name: name,
66
_cash: cash,
7+
_dailyAllowance: 40,
8+
_dayTotalWithdrawals: 0,
79

810
deposit: function (amount) {
911
this._cash += amount;
@@ -14,8 +16,13 @@ function createWallet(name, cash = 0) {
1416
console.log(`Insufficient funds!`);
1517
return 0;
1618
}
19+
if(this._dayTotalWithdrawals + amount > this._dailyAllowance){
20+
console.log(`Insufficient remaining daily allowance!`);
21+
return 0;
22+
}
1723

1824
this._cash -= amount;
25+
this._dayTotalWithdrawals += amount;
1926
return amount;
2027
},
2128

@@ -29,6 +36,17 @@ function createWallet(name, cash = 0) {
2936
wallet.deposit(withdrawnAmount);
3037
},
3138

39+
resetDailyAllowance: function() {
40+
this._dayTotalWithdrawals = 0;
41+
},
42+
43+
setDailyAllowance: function (newAllowance){
44+
this._dailyAllowance = newAllowance;
45+
console.log(
46+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
47+
);
48+
},
49+
3250
reportBalance: function () {
3351
console.log(
3452
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -47,6 +65,9 @@ function main() {
4765
const walletJane = createWallet('Jane', 20);
4866

4967
walletJack.transferInto(walletJoe, 50);
68+
walletJack.setDailyAllowance(80);
69+
walletJack.transferInto(walletJoe, 50);
70+
5071
walletJane.transferInto(walletJoe, 25);
5172

5273
walletJane.deposit(20);

Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js renamed to Week4/prep-exercises/1-wallet/ex4-object-shared-methods.mjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from './euroFormatter.mjs';
22

33
function deposit(amount) {
44
this._cash += amount;
@@ -9,8 +9,13 @@ function withdraw(amount) {
99
console.log(`Insufficient funds!`);
1010
return 0;
1111
}
12+
if(this._dayTotalWithdrawals + amount > this._dailyAllowance){
13+
console.log(`Insufficient remaining daily allowance!`);
14+
return 0;
15+
}
1216

1317
this._cash -= amount;
18+
this._dayTotalWithdrawals += amount;
1419
return amount;
1520
}
1621

@@ -24,6 +29,17 @@ function transferInto(wallet, amount) {
2429
wallet.deposit(withdrawnAmount);
2530
}
2631

32+
function resetDailyAllowance() {
33+
this._dayTotalWithdrawals = 0;
34+
}
35+
36+
function setDailyAllowance(newAllowance) {
37+
this._dailyAllowance = newAllowance;
38+
console.log(
39+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
40+
);
41+
}
42+
2743
function reportBalance() {
2844
console.log(
2945
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -38,9 +54,13 @@ function createWallet(name, cash = 0) {
3854
return {
3955
_name: name,
4056
_cash: cash,
57+
_dailyAllowance: 40,
58+
_dayTotalWithdrawals: 0,
4159
deposit,
4260
withdraw,
4361
transferInto,
62+
resetDailyAllowance,
63+
setDailyAllowance,
4464
reportBalance,
4565
getName,
4666
};
@@ -52,6 +72,9 @@ function main() {
5272
const walletJane = createWallet('Jane', 20);
5373

5474
walletJack.transferInto(walletJoe, 50);
75+
walletJack.setDailyAllowance(80);
76+
walletJack.transferInto(walletJoe, 50);
77+
5578
walletJane.transferInto(walletJoe, 25);
5679

5780
walletJane.deposit(20);

Week4/prep-exercises/1-wallet/ex5-prototype.js renamed to Week4/prep-exercises/1-wallet/ex5-prototype.mjs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from './euroFormatter.mjs';
22

33
function Wallet(name, cash) {
44
this._name = name;
55
this._cash = cash;
6+
this._dailyAllowance = 40;
7+
this._dayTotalWithdrawals = 0;
68
}
79

810
Wallet.prototype.deposit = function (amount) {
@@ -14,8 +16,13 @@ Wallet.prototype.withdraw = function (amount) {
1416
console.log(`Insufficient funds!`);
1517
return 0;
1618
}
19+
if(this._dayTotalWithdrawals + amount > this._dailyAllowance){
20+
console.log(`Insufficient remaining daily allowance!`);
21+
return 0;
22+
}
1723

1824
this._cash -= amount;
25+
this._dayTotalWithdrawals += amount;
1926
return amount;
2027
};
2128

@@ -29,6 +36,17 @@ Wallet.prototype.transferInto = function (wallet, amount) {
2936
wallet.deposit(withdrawnAmount);
3037
};
3138

39+
Wallet.prototype.resetDailyAllowance = function(){
40+
this._dayTotalWithdrawals = 0;
41+
}
42+
43+
Wallet.prototype.setDailyAllowance = function (newAllowance){
44+
this._dailyAllowance = newAllowance;
45+
console.log(
46+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
47+
);
48+
}
49+
3250
Wallet.prototype.reportBalance = function () {
3351
console.log(
3452
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -45,6 +63,9 @@ function main() {
4563
const walletJane = new Wallet('Jane', 20);
4664

4765
walletJack.transferInto(walletJoe, 50);
66+
walletJack.setDailyAllowance(80);
67+
walletJack.transferInto(walletJoe, 50);
68+
4869
walletJane.transferInto(walletJoe, 25);
4970

5071
walletJane.deposit(20);

0 commit comments

Comments
 (0)