@@ -86,9 +86,9 @@ const displayMoviments = function(moviments){
8686
8787} ;
8888
89- const calcDisplayBalance = function ( movements ) {
90- const balance = movements . reduce ( ( acc , mov ) => acc + mov , 0 )
91- labelBalance . textContent = `R$ ${ balance } `
89+ const calcDisplayBalance = function ( acc ) {
90+ acc . balance = acc . movements . reduce ( ( acc , mov ) => acc + mov , 0 )
91+ labelBalance . textContent = `R$ ${ acc . balance } `
9292}
9393
9494
@@ -124,7 +124,13 @@ const createUserNames = function(accs){
124124
125125} ;
126126createUserNames ( accounts )
127-
127+ const updateUI = function ( acc ) {
128+ displayMoviments ( acc . movements ) ;
129+ //Display balance
130+ calcDisplayBalance ( currentAccount ) ;
131+ //Display summary
132+ calcDisplaySummary ( currentAccount ) ;
133+ }
128134//Event Handler
129135let currentAccount ;
130136
@@ -143,13 +149,22 @@ containerApp.style.opacity = 100;
143149inputLoginUsername . value = inputLoginPin . value = '' ;
144150inputLoginPin . blur ( ) ;
145151 //Display movements
146- displayMoviments ( currentAccount . movements ) ;
147- //Display balance
148- calcDisplayBalance ( currentAccount . movements ) ;
149- //Display summary
150- calcDisplaySummary ( currentAccount ) ;
152+ updateUI ( currentAccount ) ;
151153} )
152154
155+ btnTransfer . addEventListener ( 'click' , function ( e ) {
156+ e . preventDefault ( ) ;
157+ const amount = Number ( inputTransferAmount . value ) ;
158+ const receiverAcc = accounts . find ( ( acc => acc . username === inputTransferTo . value ) ) ;
159+ inputTransferAmount . value = inputTransferTo . value = ''
160+ if ( amount && receiverAcc && currentAccount . balance >= amount
161+ && receiverAcc ?. username !== currentAccount . username ) {
162+ currentAccount . movements . push ( - amount ) ;
163+ receiverAcc . movements . push ( amount )
164+
165+ updateUI ( currentAccount ) ;
166+ }
167+ } ) ;
153168/////////////////////////////////////////////////
154169/////////////////////////////////////////////////
155170// LECTURES
0 commit comments