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

Skip to content

Commit e3ffc31

Browse files
committed
week4
1 parent 26e722f commit e3ffc31

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

Week4/prep-exercises/1-wallet/ex1-closure-example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function createWallet(name, cash = 0) {
6868
}
6969

7070
function main() {
71-
const walletJack = createWallet("Jack", 100);
71+
const walletJack = createWallet("Jack", 100);
7272
const walletJoe = createWallet("Joe", 10);
7373
const walletJane = createWallet("Jane", 20);
7474

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ import eurosFormatter from './euroFormatter.js';
33
class Wallet {
44
#name;
55
#cash;
6+
#dailyAllowance;
7+
#dayTotalWithdrawals
8+
69

710
constructor(name, cash) {
811
this.#name = name;
912
this.#cash = cash;
13+
this.#dailyAllowance = 40;
14+
this.#dayTotalWithdrawals = 0;
1015
}
1116

1217
get name() {
1318
return this.#name;
1419
}
20+
if
1521

1622
deposit(amount) {
1723
this.#cash += amount;
@@ -22,8 +28,13 @@ class Wallet {
2228
console.log(`Insufficient funds!`);
2329
return 0;
2430
}
31+
if(this.#dayTotalWithdrawals + amount > this.#dailyAllowance){
32+
console.log(`Insufficient remaining daily allowance!`);
33+
return 0;
34+
}
2535

2636
this.#cash -= amount;
37+
this.#dayTotalWithdrawals += amount;
2738
return amount;
2839
}
2940

@@ -36,6 +47,15 @@ class Wallet {
3647
const withdrawnAmount = this.withdraw(amount);
3748
wallet.deposit(withdrawnAmount);
3849
}
50+
51+
setDailyAllowance(newAllowance){
52+
this.#dailyAllowance = newAllowance;
53+
console.log(`daily allowance ${eurosFormatter.format(newAllowance)}`)
54+
}
55+
resetDailyAllowance(){
56+
this.#dayTotalWithdrawals = 0;
57+
}
58+
3959

4060
reportBalance() {
4161
console.log(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ function createWallet(name, cash = 0) {
44
return {
55
_name: name,
66
_cash: cash,
7+
_dailyAllowance: 40,
8+
_dayTotalWithdrawals: 0,
9+
710

811
deposit: function (amount) {
912
this._cash += amount;
@@ -14,8 +17,13 @@ function createWallet(name, cash = 0) {
1417
console.log(`Insufficient funds!`);
1518
return 0;
1619
}
20+
if(this._dayTotalWithdrawals + amount > this._dailyAllowance){
21+
console.log(`Insufficient remaining daily allowance!`);
22+
return 0;
23+
}
1724

1825
this._cash -= amount;
26+
this._dayTotalWithdrawals += amount;
1927
return amount;
2028
},
2129

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

40+
setDailyAllowance: function (newAllowance){
41+
this._dailyAllowance = newAllowance;
42+
console.log(`Allowance set to ${eurosFormatter.format(newAllowance)} `)
43+
},
44+
resetDailyAllowance: function(){
45+
this._dayTotalWithdrawals = 0;
46+
},
47+
3248
reportBalance: function () {
3349
console.log(
3450
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ function withdraw(amount) {
99
console.log(`Insufficient funds!`);
1010
return 0;
1111
}
12+
if(this._dayTotalWithdrawals + amount > this._dailyAllowance){
13+
console.log(`Insufficient remaining daily allowance!`);
14+
return 0;
15+
}
1216

1317
this._cash -= amount;
18+
this._dayTotalWithdrawals += amount;
1419
return amount;
1520
}
1621

@@ -29,6 +34,15 @@ function reportBalance() {
2934
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
3035
);
3136
}
37+
function setDailyAllowance(newAllowance){
38+
this._dailyAllowance = newAllowance;
39+
console.log(
40+
`Daily allowance ${eurosFormatter.format(newAllowance)}`
41+
);
42+
}
43+
function resetDailyAllowance(){
44+
this._dayTotalWithdrawals = 0;
45+
}
3246

3347
function getName() {
3448
return this._name;
@@ -38,6 +52,8 @@ 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,

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

Lines changed: 20 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._dayTotalWithdrawal = 0;
68
}
79

810
Wallet.prototype.deposit = function (amount) {
@@ -14,8 +16,13 @@ Wallet.prototype.withdraw = function (amount) {
1416
console.log(`Insufficient funds!`);
1517
return 0;
1618
}
19+
if(this._dayTotalWithdrawal + amount > this._dailyAllowance){
20+
console.log(`Insufficient remaining daily allowance!`);
21+
return 0 ;
22+
}
1723

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

@@ -28,13 +35,26 @@ Wallet.prototype.transferInto = function (wallet, amount) {
2835
const withdrawnAmount = this.withdraw(amount);
2936
wallet.deposit(withdrawnAmount);
3037
};
38+
wallet.prototype.setDailyAllowance = function(newAllowance){
39+
this._dailyAllowance = newAllowance;
40+
41+
console.log(
42+
`Daily allowance ${eurosFormatter.format(newAllowance)}`
43+
);
44+
45+
}
46+
wallet.prototype.resetDailyAllowance = function(){
47+
this._dayTotalWithdrawal = 0 ;
48+
};
3149

3250
Wallet.prototype.reportBalance = function () {
3351
console.log(
3452
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
3553
);
3654
};
3755

56+
57+
3858
Wallet.prototype.getName = function () {
3959
return this._name;
4060
};

0 commit comments

Comments
 (0)