Skip to content

Commit 66c1001

Browse files
committed
prep-ex-w4
1 parent 1e87db3 commit 66c1001

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

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

Lines changed: 14 additions & 1 deletion
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() {
@@ -24,6 +28,7 @@ class Wallet {
2428
}
2529

2630
this.#cash -= amount;
31+
this.#dayTotalWithdrawals += amount;
2732
return amount;
2833
}
2934

@@ -37,9 +42,17 @@ class Wallet {
3742
wallet.deposit(withdrawnAmount);
3843
}
3944

45+
reportDailyAllowance () {
46+
this.#dayTotalWithdrawals = 0;
47+
}
48+
49+
setDailyAllowance (newAllowance) {
50+
this.#dailyAllowance = newAllowance;
51+
}
52+
4053
reportBalance() {
4154
console.log(
42-
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
55+
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}, daily allowance ${eurosFormatter.format(this.#dailyAllowance)}, total withdrawn today : ${eurosFormatter.format(this.#dayTotalWithdrawals)}`
4356
);
4457
}
4558
}

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

Lines changed: 11 additions & 0 deletions
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;
@@ -16,6 +18,7 @@ function createWallet(name, cash = 0) {
1618
}
1719

1820
this._cash -= amount;
21+
this._dayTotalWithdrawals += amount;
1922
return amount;
2023
},
2124

@@ -38,6 +41,14 @@ function createWallet(name, cash = 0) {
3841
getName: function () {
3942
return this._name;
4043
},
44+
45+
resetDailyAllowance: function (newAllowance) {
46+
this._dayTotalWithdrawals = 0;
47+
},
48+
49+
setDailyAllowance : function (newAllowance) {
50+
this._dailyAllowance = newAllowance;
51+
},
4152
};
4253
}
4354

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

Lines changed: 18 additions & 0 deletions
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(`surpassed daily allowance`);
15+
return 0;
16+
}
17+
1318
this._cash -= amount;
19+
this._dayTotalWithdrawals += amount;
1420
return amount;
1521
}
1622

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

43+
function resetDailyAllowance () {
44+
this._dayTotalWithdrawals = 0;
45+
}
46+
47+
function setDailyAllowance (newAllowance) {
48+
this._dailyAllowance = newAllowance;
49+
}
50+
3751
function createWallet(name, cash = 0) {
3852
return {
3953
_name: name,
4054
_cash: cash,
55+
_dailyAllowance : 40,
56+
_dayTotalWithdrawals : 0,
4157
deposit,
4258
withdraw,
4359
transferInto,
4460
reportBalance,
4561
getName,
62+
resetDailyAllowance,
63+
setDailyAllowance
4664
};
4765
}
4866

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

Lines changed: 16 additions & 0 deletions
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(`surpassed daily allowance`);
22+
return 0;
23+
}
24+
1825
this._cash -= amount;
26+
this._dayTotalWithdrawals += amount;
1927
return amount;
2028
};
2129

@@ -39,6 +47,14 @@ Wallet.prototype.getName = function () {
3947
return this._name;
4048
};
4149

50+
Wallet.prototype.setDailyAllowance = function () {
51+
this._dayTotalWithdrawals = 0;
52+
}
53+
54+
Wallet.prototype.setDailyAllowance = function (newAllowance) {
55+
this._dailyAllowance = newAllowance;
56+
}
57+
4258
function main() {
4359
const walletJack = new Wallet('Jack', 100);
4460
const walletJoe = new Wallet('Joe', 10);

0 commit comments

Comments
 (0)