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

Skip to content

Commit ed1d1ba

Browse files
authored
Solved wallet prep-exercises
2 parents f4871f7 + 1b87c7d commit ed1d1ba

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

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

Lines changed: 25 additions & 2 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+
#dayTotalWithdrawals;
7+
#dailyAllowance;
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

@@ -34,12 +43,25 @@ class Wallet {
3443
}`
3544
);
3645
const withdrawnAmount = this.withdraw(amount);
37-
wallet.deposit(withdrawnAmount);
46+
if (withdrawnAmount > 0) {
47+
wallet.deposit(withdrawnAmount);
48+
}
49+
}
50+
51+
setDailyAllowance(newAllowance) {
52+
this.#dailyAllowance = newAllowance;
53+
console.log(
54+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
55+
);
56+
}
57+
58+
resetDailyAllowance() {
59+
this.#dayTotalWithdrawals = 0;
3860
}
3961

4062
reportBalance() {
4163
console.log(
42-
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
64+
`Name: ${this.#name}, balance: ${eurosFormatter.format(this.#cash)}`
4365
);
4466
}
4567
}
@@ -50,6 +72,7 @@ function main() {
5072
const walletJane = new Wallet('Jane', 20);
5173

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

5578
walletJane.deposit(20);

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ 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;
@@ -15,7 +17,13 @@ function createWallet(name, cash = 0) {
1517
return 0;
1618
}
1719

20+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
21+
console.log(`Insufficient remaining daily allowance!`);
22+
return 0;
23+
}
24+
1825
this._cash -= amount;
26+
this._dayTotalWithdrawals += amount;
1927
return amount;
2028
},
2129

@@ -26,7 +34,9 @@ function createWallet(name, cash = 0) {
2634
} to ${wallet.getName()}`
2735
);
2836
const withdrawnAmount = this.withdraw(amount);
29-
wallet.deposit(withdrawnAmount);
37+
if (withdrawnAmount > 0) {
38+
wallet.deposit(withdrawnAmount);
39+
}
3040
},
3141

3242
reportBalance: function () {
@@ -38,6 +48,17 @@ function createWallet(name, cash = 0) {
3848
getName: function () {
3949
return this._name;
4050
},
51+
52+
resetDailyAllowance: function () {
53+
this._dayTotalWithdrawals = 0;
54+
},
55+
56+
setDailyAllowance: function (newAllowance) {
57+
this._dailyAllowance = newAllowance;
58+
console.log(
59+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
60+
);
61+
},
4162
};
4263
}
4364

@@ -47,6 +68,7 @@ function main() {
4768
const walletJane = createWallet('Jane', 20);
4869

4970
walletJack.transferInto(walletJoe, 50);
71+
walletJack.setDailyAllowance(80);
5072
walletJane.transferInto(walletJoe, 25);
5173

5274
walletJane.deposit(20);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ function transferInto(wallet, amount) {
2121
} to ${wallet.getName()}`
2222
);
2323
const withdrawnAmount = this.withdraw(amount);
24-
wallet.deposit(withdrawnAmount);
24+
if (withdrawnAmount > 0) {
25+
wallet.deposit(withdrawnAmount);
26+
}
2527
}
2628

2729
function reportBalance() {
2830
console.log(
2931
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
3032
);
3133
}
34+
function setDailyAllowance(newAllowance) {
35+
this._dailyAllowance = newAllowance;
36+
console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
37+
}
38+
function resetDailyAllowance() {
39+
this._dayTotalWithdrawals = 0;
40+
}
3241

3342
function getName() {
3443
return this._name;
@@ -38,11 +47,15 @@ function createWallet(name, cash = 0) {
3847
return {
3948
_name: name,
4049
_cash: cash,
50+
_dailyAllowance: 40,
51+
_dayTotalWithdrawals: 0,
4152
deposit,
4253
withdraw,
4354
transferInto,
4455
reportBalance,
4556
getName,
57+
setDailyAllowance,
58+
resetDailyAllowance,
4659
};
4760
}
4861

@@ -52,6 +65,7 @@ function main() {
5265
const walletJane = createWallet('Jane', 20);
5366

5467
walletJack.transferInto(walletJoe, 50);
68+
walletJack.setDailyAllowance(80);
5569
walletJane.transferInto(walletJoe, 25);
5670

5771
walletJane.deposit(20);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ 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) {
@@ -26,7 +28,9 @@ Wallet.prototype.transferInto = function (wallet, amount) {
2628
} to ${wallet.getName()}`
2729
);
2830
const withdrawnAmount = this.withdraw(amount);
29-
wallet.deposit(withdrawnAmount);
31+
if (withdrawnAmount > 0) {
32+
wallet.deposit(withdrawnAmount);
33+
}
3034
};
3135

3236
Wallet.prototype.reportBalance = function () {
@@ -35,6 +39,14 @@ Wallet.prototype.reportBalance = function () {
3539
);
3640
};
3741

42+
Wallet.prototype.setDailyAllowance = function (newAllowance) {
43+
this._dailyAllowance = newAllowance;
44+
console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
45+
};
46+
47+
Wallet.prototype.resetDailyAllowance = function () {
48+
this._dayTotalWithdrawals = 0;
49+
};
3850
Wallet.prototype.getName = function () {
3951
return this._name;
4052
};
@@ -45,6 +57,7 @@ function main() {
4557
const walletJane = new Wallet('Jane', 20);
4658

4759
walletJack.transferInto(walletJoe, 50);
60+
walletJack.setDailyAllowance(80);
4861
walletJane.transferInto(walletJoe, 25);
4962

5063
walletJane.deposit(20);

0 commit comments

Comments
 (0)