1- import eurosFormatter from './euroFormatter.js ' ;
1+ import eurosFormatter from './euroFormatter.mjs ' ;
22
33function Wallet ( name , cash ) {
44 this . _name = name ;
55 this . _cash = cash ;
6+ this . _dailyAllowance = 40 ;
7+ this . _dayTotalWithdrawals = 0 ;
68}
79
810Wallet . 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 . _dayTotalWithdrawals + amount > this . _dailyAllowance ) {
20+ console . log ( `Insufficient remaining daily allowance!` ) ;
21+ return 0 ;
22+ }
1723
1824 this . _cash -= amount ;
25+ this . _dayTotalWithdrawals += amount ;
1926 return amount ;
2027} ;
2128
@@ -29,6 +36,17 @@ Wallet.prototype.transferInto = function (wallet, amount) {
2936 wallet . deposit ( withdrawnAmount ) ;
3037} ;
3138
39+ Wallet . prototype . resetDailyAllowance = function ( ) {
40+ this . _dayTotalWithdrawals = 0 ;
41+ }
42+
43+ Wallet . prototype . setDailyAllowance = function ( newAllowance ) {
44+ this . _dailyAllowance = newAllowance ;
45+ console . log (
46+ `Daily allowance set to: ${ eurosFormatter . format ( newAllowance ) } `
47+ ) ;
48+ }
49+
3250Wallet . prototype . reportBalance = function ( ) {
3351 console . log (
3452 `Name: ${ this . _name } , balance: ${ eurosFormatter . format ( this . _cash ) } `
@@ -45,6 +63,9 @@ function main() {
4563 const walletJane = new Wallet ( 'Jane' , 20 ) ;
4664
4765 walletJack . transferInto ( walletJoe , 50 ) ;
66+ walletJack . setDailyAllowance ( 80 ) ;
67+ walletJack . transferInto ( walletJoe , 50 ) ;
68+
4869 walletJane . transferInto ( walletJoe , 25 ) ;
4970
5071 walletJane . deposit ( 20 ) ;
0 commit comments