Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a64990c

Browse files
committed
Changes added to exercises
1 parent c064546 commit a64990c

File tree

4 files changed

+127
-28
lines changed

4 files changed

+127
-28
lines changed

Week4/prep-exercises/1-wallet/ex2-classes.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import eurosFormatter from './euroFormatter.js';
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() {
@@ -42,21 +46,36 @@ class Wallet {
4246
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
4347
);
4448
}
49+
resetDailyAllowance() {
50+
this.#dayTotalWithdrawals = 0;
51+
console.log(
52+
`Daily allowance reset to: ${eurosFormatter.format(this.#dailyAllowance)}`
53+
);
54+
}
55+
setDailyAllowance(newAllowance) {
56+
this.#dailyAllowance = newAllowance;
57+
console.log(
58+
`New daily withdrawal limit set to: ${eurosFormatter.format(this.#dailyAllowance)}`
59+
);
60+
}
61+
4562
}
4663

4764
function main() {
4865
const walletJack = new Wallet('Jack', 100);
4966
const walletJoe = new Wallet('Joe', 10);
5067
const walletJane = new Wallet('Jane', 20);
5168

52-
walletJack.transferInto(walletJoe, 50);
53-
walletJane.transferInto(walletJoe, 25);
54-
55-
walletJane.deposit(20);
56-
walletJane.transferInto(walletJoe, 25);
69+
walletJack.setDailyAllowance(70);
70+
walletJack.withdraw(10);
71+
walletJack.reportBalance();
5772

73+
walletJack.resetDailyAllowance();
74+
walletJack.withdraw(20);
5875
walletJack.reportBalance();
59-
walletJoe.reportBalance();
76+
77+
walletJane.deposit(50);
78+
walletJane.transferInto(walletJoe, 40);
6079
walletJane.reportBalance();
6180
}
6281

Week4/prep-exercises/1-wallet/ex3-object.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@ 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;
1012
},
1113

1214
withdraw: function (amount) {
15+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
16+
console.log(`Insufficient remaining daily allowance!`);
17+
return 0;
18+
}
1319
if (this._cash - amount < 0) {
1420
console.log(`Insufficient funds!`);
1521
return 0;
1622
}
1723

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

@@ -38,23 +45,46 @@ function createWallet(name, cash = 0) {
3845
getName: function () {
3946
return this._name;
4047
},
41-
};
42-
}
48+
resetDailyAllowance: function () {
49+
this._dayTotalWithdrawals = 0;
50+
console.log(
51+
`Daily allowance reset to: ${eurosFormatter.format(
52+
this._dailyAllowance
53+
)}`
54+
);
55+
},
56+
setDailyAllowance: function (newAllowance) {
57+
this._dailyAllowance = newAllowance;
58+
console.log(
59+
`New daily withdrawal limit set to: ${eurosFormatter.format(
60+
this._dailyAllowance
61+
)}`
62+
);
63+
},
64+
}
65+
66+
67+
}
68+
69+
4370

4471
function main() {
4572
const walletJack = createWallet('Jack', 100);
4673
const walletJoe = createWallet('Joe', 10);
4774
const walletJane = createWallet('Jane', 20);
4875

49-
walletJack.transferInto(walletJoe, 50);
50-
walletJane.transferInto(walletJoe, 25);
51-
52-
walletJane.deposit(20);
53-
walletJane.transferInto(walletJoe, 25);
54-
76+
walletJack.setDailyAllowance(70);
77+
walletJack.withdraw(10);
5578
walletJack.reportBalance();
56-
walletJoe.reportBalance();
79+
walletJack.resetDailyAllowance();
80+
walletJack.withdraw(20);
81+
walletJack.reportBalance();
82+
83+
walletJane.deposit(50);
84+
walletJane.transferInto(walletJoe, 40);
5785
walletJane.reportBalance();
86+
walletJoe.reportBalance();
87+
5888
}
5989

6090
main();

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ function deposit(amount) {
55
}
66

77
function withdraw(amount) {
8+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
9+
console.log(`Insufficient remaining daily allowance!`);
10+
return 0;
11+
}
12+
813
if (this._cash - amount < 0) {
914
console.log(`Insufficient funds!`);
1015
return 0;
1116
}
1217

1318
this._cash -= amount;
19+
this._dayTotalWithdrawals += amount;
1420
return amount;
1521
}
1622

@@ -34,15 +40,33 @@ function getName() {
3440
return this._name;
3541
}
3642

43+
function resetDailyAllowance() {
44+
this._dayTotalWithdrawals = 0;
45+
console.log(
46+
`Daily allowance reset to: ${eurosFormatter.format(this._dailyAllowance)}`
47+
);
48+
}
49+
50+
function setDailyAllowance(newAllowance) {
51+
this._dailyAllowance = newAllowance;
52+
console.log(
53+
`New daily withdrawal limit set to: ${eurosFormatter.format(this._dailyAllowance)}`
54+
);
55+
}
56+
3757
function createWallet(name, cash = 0) {
3858
return {
3959
_name: name,
4060
_cash: cash,
61+
_dailyAllowance: 40,
62+
_dayTotalWithdrawals: 0,
4163
deposit,
4264
withdraw,
4365
transferInto,
4466
reportBalance,
4567
getName,
68+
resetDailyAllowance,
69+
setDailyAllowance,
4670
};
4771
}
4872

@@ -51,15 +75,17 @@ function main() {
5175
const walletJoe = createWallet('Joe', 10);
5276
const walletJane = createWallet('Jane', 20);
5377

54-
walletJack.transferInto(walletJoe, 50);
55-
walletJane.transferInto(walletJoe, 25);
56-
57-
walletJane.deposit(20);
58-
walletJane.transferInto(walletJoe, 25);
59-
78+
walletJack.setDailyAllowance(70);
79+
walletJack.withdraw(10);
80+
walletJack.reportBalance();
81+
walletJack.resetDailyAllowance();
82+
walletJack.withdraw(20);
6083
walletJack.reportBalance();
61-
walletJoe.reportBalance();
84+
85+
walletJane.deposit(50);
86+
walletJane.transferInto(walletJoe, 40);
6287
walletJane.reportBalance();
88+
walletJoe.reportBalance();
6389
}
6490

6591
main();

Week4/prep-exercises/1-wallet/ex5-prototype.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ import eurosFormatter from './euroFormatter.js';
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) {
911
this._cash += amount;
1012
};
1113

1214
Wallet.prototype.withdraw = function (amount) {
15+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
16+
console.log(`Insufficient remaining daily allowance!`);
17+
return 0;
18+
}
1319
if (this._cash - amount < 0) {
1420
console.log(`Insufficient funds!`);
1521
return 0;
@@ -39,20 +45,38 @@ Wallet.prototype.getName = function () {
3945
return this._name;
4046
};
4147

48+
Wallet.prototype.resetDailyAllowance = function () {
49+
this._dayTotalWithdrawals = 0;
50+
console.log(
51+
`Daily allowance reset to: ${eurosFormatter.format(this._dailyAllowance)}`
52+
);
53+
}
54+
55+
Wallet.prototype.setDailyAllowance = function (newAllowance) {
56+
this._dailyAllowance = newAllowance;
57+
console.log(
58+
`New daily withdrawal limit set to: ${eurosFormatter.format(this._dailyAllowance)}`
59+
);
60+
}
61+
4262
function main() {
4363
const walletJack = new Wallet('Jack', 100);
4464
const walletJoe = new Wallet('Joe', 10);
4565
const walletJane = new Wallet('Jane', 20);
4666

47-
walletJack.transferInto(walletJoe, 50);
48-
walletJane.transferInto(walletJoe, 25);
49-
50-
walletJane.deposit(20);
51-
walletJane.transferInto(walletJoe, 25);
67+
walletJack.setDailyAllowance(70);
68+
walletJack.withdraw(10);
69+
walletJack.reportBalance();
5270

71+
walletJack.resetDailyAllowance();
72+
walletJack.withdraw(20);
5373
walletJack.reportBalance();
54-
walletJoe.reportBalance();
74+
75+
walletJane.deposit(50);
76+
walletJane.transferInto(walletJoe, 40);
5577
walletJane.reportBalance();
78+
walletJoe.reportBalance();
79+
5680
}
5781

5882
main();

0 commit comments

Comments
 (0)