Skip to content

Commit d1b3715

Browse files
committed
completed prep exercises
1 parent 42621a1 commit d1b3715

6 files changed

Lines changed: 110 additions & 7 deletions

File tree

6 KB
Binary file not shown.

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

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

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

1216
get name() {
@@ -23,23 +27,42 @@ class Wallet {
2327
return 0;
2428
}
2529

30+
// added new code here
31+
if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance){
32+
console.log(`Insufficient remaining daily allowance!`);
33+
return 0;
34+
}
35+
2636
this.#cash -= amount;
37+
this.#dayTotalWithdrawals += amount;
2738
return amount;
2839
}
2940

41+
// modified this code
3042
transferInto(wallet, amount) {
31-
console.log(
32-
`Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${
33-
wallet.name
34-
}`
35-
);
43+
console.log(
44+
`Transferring ${eurosFormatter.format(amount)} from ${this.#name} to ${wallet.#name}`
45+
);
3646
const withdrawnAmount = this.withdraw(amount);
3747
wallet.deposit(withdrawnAmount);
3848
}
3949

50+
// added this code
51+
setDailyAllowance(newAllowance) {
52+
this.#dailyAllowance = newAllowance;
53+
console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
54+
);
55+
}
56+
57+
// added this function
58+
resetDailyAllowance(){
59+
this.#dayTotalWithdrawals = 0;
60+
}
61+
62+
// modified here
4063
reportBalance() {
4164
console.log(
42-
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
65+
`Name: ${this.#name}, balance: ${eurosFormatter.format(this.#cash)}`
4366
);
4467
}
4568
}
@@ -50,6 +73,9 @@ function main() {
5073
const walletJane = new Wallet('Jane', 20);
5174

5275
walletJack.transferInto(walletJoe, 50);
76+
walletJack.setDailyAllowance(80);
77+
walletJack.transferInto(walletJoe, 50);
78+
5379
walletJane.transferInto(walletJoe, 25);
5480

5581
walletJane.deposit(20);

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

Lines changed: 21 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;
@@ -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+
setDailyAllowance: function (newAllowance) {
41+
this._dailyAllowance = newAllowance;
42+
console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
43+
);
44+
},
45+
46+
resetDailyAllowance: function () {
47+
this._dayTotalWithdrawals = 0;
48+
},
49+
3250
reportBalance: function () {
3351
console.log(
3452
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -47,6 +65,9 @@ function main() {
4765
const walletJane = createWallet('Jane', 20);
4866

4967
walletJack.transferInto(walletJoe, 50);
68+
walletJack.setDailyAllowance(80);
69+
walletJack.transferInto(walletJoe, 50);
70+
5071
walletJane.transferInto(walletJoe, 25);
5172

5273
walletJane.deposit(20);

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

Lines changed: 24 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(`Insufficient remaining daily allowance!`);
15+
return 0;
16+
}
17+
1318
this._cash -= amount;
19+
this._dayTotalWithdrawals += amount;
1420
return amount;
1521
}
1622

@@ -30,6 +36,17 @@ function reportBalance() {
3036
);
3137
}
3238

39+
function setDailyAllowance(newAllowance) {
40+
this._dailyAllowance = newAllowance;
41+
console.log(
42+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
43+
);
44+
}
45+
46+
function resetDailyAllowance() {
47+
this._dayTotalWithdrawals = 0;
48+
}
49+
3350
function getName() {
3451
return this._name;
3552
}
@@ -38,10 +55,14 @@ function createWallet(name, cash = 0) {
3855
return {
3956
_name: name,
4057
_cash: cash,
58+
_dailyAllowance: 40,
59+
_dayTotalWithdrawals: 0,
4160
deposit,
4261
withdraw,
4362
transferInto,
4463
reportBalance,
64+
resetDailyAllowance,
65+
setDailyAllowance,
4566
getName,
4667
};
4768
}
@@ -52,6 +73,9 @@ function main() {
5273
const walletJane = createWallet('Jane', 20);
5374

5475
walletJack.transferInto(walletJoe, 50);
76+
walletJack.setDailyAllowance(80);
77+
walletJack.transferInto(walletJoe, 50);
78+
5579
walletJane.transferInto(walletJoe, 25);
5680

5781
walletJane.deposit(20);

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._dailyTotalWithdrawals = 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._dailyTotalWithdrawals + amount > this._dailyAllowance) {
21+
console.log(`Insufficient remaining daily allowance`);
22+
return 0;
23+
}
24+
1825
this._cash -= amount;
26+
this._dailyTotalWithdrawals += amount;
1927
return amount;
2028
};
2129

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

40+
Wallet.prototype.setDailyAllowance = function (newAllowance) {
41+
this._dailyAllowance = newAllowance;
42+
console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
43+
};
44+
45+
Wallet.prototype.resetDailyAllowance = function () {
46+
this._dailyTotalWithdrawals = 0;
47+
};
48+
3249
Wallet.prototype.reportBalance = function () {
3350
console.log(
3451
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -45,6 +62,9 @@ function main() {
4562
const walletJane = new Wallet('Jane', 20);
4663

4764
walletJack.transferInto(walletJoe, 50);
65+
walletJack.setDailyAllowance(80);
66+
walletJack.transferInto(walletJoe, 50);
67+
4868
walletJane.transferInto(walletJoe, 25);
4969

5070
walletJane.deposit(20);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "1-wallet",
3+
"version": "1.0.0",
4+
"description": "> Created by the one and only Jim, you can find him on our [Slack](https://hackyourfuture.slack.com/team/U383PTTK9) and on [GitHub](https://github.com/remarcmij)!",
5+
"main": "euroFormatter.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "Fressia",
11+
"license": "ISC"
12+
}

0 commit comments

Comments
 (0)