1- import eurosFormatter from ' ./euroFormatter.js' ;
1+ import eurosFormatter from " ./euroFormatter.js" ;
22
33class 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
4765function 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 ) ;
0 commit comments