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

Skip to content

Commit 2db0b81

Browse files
committed
finishedPrepEx
1 parent 0335b38 commit 2db0b81

File tree

4 files changed

+77
-19
lines changed

4 files changed

+77
-19
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from "./euroFormatter.js";
22

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

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

1216
get name() {
@@ -22,8 +26,12 @@ class Wallet {
2226
console.log(`Insufficient funds!`);
2327
return 0;
2428
}
25-
29+
if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) {
30+
console.log(`Insufficient remaining daily allowance!`);
31+
return 0;
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+
setDailyAllowance(newAllowance) {
49+
this.#dailyAllowance = newAllowance;
50+
console.log(
51+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
52+
);
53+
}
54+
55+
resetDailyAllowance() {
56+
this.#dayTotalWithdrawals = 0;
57+
}
4058
reportBalance() {
4159
console.log(
4260
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
@@ -45,11 +63,12 @@ class Wallet {
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);
5169

5270
walletJack.transferInto(walletJoe, 50);
71+
walletJack.setDailyAllowance(80);
5372
walletJane.transferInto(walletJoe, 25);
5473

5574
walletJane.deposit(20);

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from "./euroFormatter.js";
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;
@@ -29,6 +31,16 @@ function createWallet(name, cash = 0) {
2931
wallet.deposit(withdrawnAmount);
3032
},
3133

34+
setDailyAllowance: function (newAllowance) {
35+
this._dailyAllowance = newAllowance;
36+
console.log(
37+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
38+
);
39+
},
40+
41+
resetDailyAllowance: function () {
42+
this._dayTotalWithdrawals = 0;
43+
},
3244
reportBalance: function () {
3345
console.log(
3446
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -42,11 +54,12 @@ function createWallet(name, cash = 0) {
4254
}
4355

4456
function main() {
45-
const walletJack = createWallet('Jack', 100);
46-
const walletJoe = createWallet('Joe', 10);
47-
const walletJane = createWallet('Jane', 20);
57+
const walletJack = createWallet("Jack", 100);
58+
const walletJoe = createWallet("Joe", 10);
59+
const walletJane = createWallet("Jane", 20);
4860

4961
walletJack.transferInto(walletJoe, 50);
62+
walletJack.setDailyAllowance(80);
5063
walletJane.transferInto(walletJoe, 25);
5164

5265
walletJane.deposit(20);

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from "./euroFormatter.js";
22

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

@@ -33,25 +37,37 @@ function reportBalance() {
3337
function getName() {
3438
return this._name;
3539
}
40+
function resetDailyAllowance() {
41+
this._dayTotalWithdrawals = 0;
42+
}
43+
44+
function setDailyAllowance(amount) {
45+
this._dailyAllowance = amount;
46+
}
3647

3748
function createWallet(name, cash = 0) {
3849
return {
3950
_name: name,
4051
_cash: cash,
52+
_dailyAllowance: 40,
53+
_dayTotalWithdrawals: 0,
4154
deposit,
4255
withdraw,
4356
transferInto,
57+
setDailyAllowance,
58+
resetDailyAllowance,
4459
reportBalance,
4560
getName,
4661
};
4762
}
4863

4964
function main() {
50-
const walletJack = createWallet('Jack', 100);
51-
const walletJoe = createWallet('Joe', 10);
52-
const walletJane = createWallet('Jane', 20);
65+
const walletJack = createWallet("Jack", 100);
66+
const walletJoe = createWallet("Joe", 10);
67+
const walletJane = createWallet("Jane", 20);
5368

5469
walletJack.transferInto(walletJoe, 50);
70+
walletJack.setDailyAllowance(80);
5571
walletJane.transferInto(walletJoe, 25);
5672

5773
walletJane.deposit(20);

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from "./euroFormatter.js";
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) {
@@ -29,6 +31,14 @@ Wallet.prototype.transferInto = function (wallet, amount) {
2931
wallet.deposit(withdrawnAmount);
3032
};
3133

34+
Wallet.prototype.setDailyAllowance = function (newAllowance) {
35+
this._dailyAllowance = newAllowance;
36+
console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
37+
};
38+
39+
Wallet.prototype.resetDailyAllowance = function () {
40+
this._dayTotalWithdrawals = 0;
41+
};
3242
Wallet.prototype.reportBalance = function () {
3343
console.log(
3444
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -40,9 +50,9 @@ Wallet.prototype.getName = function () {
4050
};
4151

4252
function main() {
43-
const walletJack = new Wallet('Jack', 100);
44-
const walletJoe = new Wallet('Joe', 10);
45-
const walletJane = new Wallet('Jane', 20);
53+
const walletJack = new Wallet("Jack", 100);
54+
const walletJoe = new Wallet("Joe", 10);
55+
const walletJane = new Wallet("Jane", 20);
4656

4757
walletJack.transferInto(walletJoe, 50);
4858
walletJane.transferInto(walletJoe, 25);

0 commit comments

Comments
 (0)