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

Skip to content

Commit 63c63b2

Browse files
committed
Solved prep exercises
1 parent c4a01c2 commit 63c63b2

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import eurosFormatter from './euroFormatter.js';
33
class Wallet {
44
#name;
55
#cash;
6+
#dailyAllowance = 40;
7+
#dayTotalWithdrawals = 0;
68

79
constructor(name, cash) {
810
this.#name = name;
@@ -23,7 +25,13 @@ class Wallet {
2325
return 0;
2426
}
2527

28+
if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) {
29+
console.log(`Insufficient remaining daily allowance!`);
30+
return 0;
31+
}
32+
2633
this.#cash -= amount;
34+
this.#dayTotalWithdrawals += amount;
2735
return amount;
2836
}
2937

@@ -37,6 +45,16 @@ class Wallet {
3745
wallet.deposit(withdrawnAmount);
3846
}
3947

48+
resetDailyAllowance() {
49+
this.#dayTotalWithdrawals = 0;
50+
console.log(`Daily withdrawal has been reset`);
51+
}
52+
53+
setDailyAllowance(newAllowance) {
54+
this.#dailyAllowance = newAllowance;
55+
console.log(`New daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
56+
}
57+
4058
reportBalance() {
4159
console.log(
4260
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
@@ -49,8 +67,12 @@ function main() {
4967
const walletJoe = new Wallet('Joe', 10);
5068
const walletJane = new Wallet('Jane', 20);
5169

70+
walletJack.setDailyAllowance(50);
71+
walletJack.withdraw(30);
72+
walletJack.reportBalance();
5273
walletJack.transferInto(walletJoe, 50);
53-
walletJane.transferInto(walletJoe, 25);
74+
walletJack.resetDailyAllowance();
75+
walletJack.withdraw(50);
5476

5577
walletJane.deposit(20);
5678
walletJane.transferInto(walletJoe, 25);

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

@@ -29,6 +37,16 @@ function createWallet(name, cash = 0) {
2937
wallet.deposit(withdrawnAmount);
3038
},
3139

40+
resetDailyAllowance: function () {
41+
this._dayTotalWithdrawals = 0;
42+
console.log(`Daily withdrawals have been reset.`);
43+
},
44+
45+
setDailyAllowance: function(newAllowance) {
46+
this._dailyAllowance = newAllowance;
47+
console.log(`New daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
48+
},
49+
3250
reportBalance: function () {
3351
console.log(
3452
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -46,8 +64,12 @@ function main() {
4664
const walletJoe = createWallet('Joe', 10);
4765
const walletJane = createWallet('Jane', 20);
4866

67+
walletJack.setDailyAllowance(50);
68+
walletJack.withdraw(30);
69+
walletJack.reportBalance();
4970
walletJack.transferInto(walletJoe, 50);
50-
walletJane.transferInto(walletJoe, 25);
71+
walletJack.resetDailyAllowance();
72+
walletJack.withdraw(50);
5173

5274
walletJane.deposit(20);
5375
walletJane.transferInto(walletJoe, 25);

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ function withdraw(amount) {
1010
return 0;
1111
}
1212

13+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
14+
console.log(`Insufficient remaining daily allowance`);
15+
return 0;
16+
}
17+
1318
this._cash -= amount;
19+
this._dayTotalWithdrawals += amount;
1420
return amount;
1521
}
1622

@@ -24,6 +30,16 @@ function transferInto(wallet, amount) {
2430
wallet.deposit(withdrawnAmount);
2531
}
2632

33+
function resetDailyAllowance() {
34+
this._dayTotalWithdrawals = 0;
35+
console.log('Daily withdrawals have been reset.');
36+
}
37+
38+
function setDailyAllowance(newAllowance) {
39+
this._dailyAllowance = newAllowance;
40+
console.log(`New daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
41+
}
42+
2743
function reportBalance() {
2844
console.log(
2945
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -41,6 +57,8 @@ function createWallet(name, cash = 0) {
4157
deposit,
4258
withdraw,
4359
transferInto,
60+
resetDailyAllowance,
61+
setDailyAllowance,
4462
reportBalance,
4563
getName,
4664
};
@@ -51,8 +69,12 @@ function main() {
5169
const walletJoe = createWallet('Joe', 10);
5270
const walletJane = createWallet('Jane', 20);
5371

72+
walletJack.setDailyAllowance(50);
73+
walletJack.withdraw(30);
74+
walletJack.reportBalance();
5475
walletJack.transferInto(walletJoe, 50);
55-
walletJane.transferInto(walletJoe, 25);
76+
walletJack.resetDailyAllowance();
77+
walletJack.withdraw(50);
5678

5779
walletJane.deposit(20);
5880
walletJane.transferInto(walletJoe, 25);

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

Lines changed: 23 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) {
@@ -15,7 +17,13 @@ Wallet.prototype.withdraw = function (amount) {
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

@@ -29,6 +37,16 @@ Wallet.prototype.transferInto = function (wallet, amount) {
2937
wallet.deposit(withdrawnAmount);
3038
};
3139

40+
Wallet.prototype.resetDailyAllowance = function () {
41+
this._dayTotalWithdrawals = 0;
42+
console.log(`Daily withdrawals have been reset.`);
43+
};
44+
45+
Wallet.prototype.setDailyAllowance = function (newAllowance) {
46+
this._dailyAllowance = newAllowance;
47+
console.log(`New daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
48+
};
49+
3250
Wallet.prototype.reportBalance = function () {
3351
console.log(
3452
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -44,8 +62,12 @@ function main() {
4462
const walletJoe = new Wallet('Joe', 10);
4563
const walletJane = new Wallet('Jane', 20);
4664

65+
walletJack.setDailyAllowance(50);
66+
walletJack.withdraw(30);
67+
walletJack.reportBalance();
4768
walletJack.transferInto(walletJoe, 50);
48-
walletJane.transferInto(walletJoe, 25);
69+
walletJack.resetDailyAllowance();
70+
walletJack.withdraw(50);
4971

5072
walletJane.deposit(20);
5173
walletJane.transferInto(walletJoe, 25);

0 commit comments

Comments
 (0)