-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to manage events with one handler function
- Loading branch information
Dhayananth Santhamoorthy
committed
Mar 20, 2022
1 parent
17be4e7
commit d48b0f8
Showing
1 changed file
with
48 additions
and
44 deletions.
There are no files selected for viewing
92 changes: 48 additions & 44 deletions
92
Dhaya Santhamoorthy/wk02 - starts 14th Mar/5-fri/atm/js/atm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,73 @@ | ||
|
||
$('#checking-deposit').on('click', function () { | ||
const depositValue = Number($('#checking-amount').val()); | ||
if (depositValue > 0) { | ||
manageDeposit( '#checking-balance', depositValue ); // function call passing the id of checking-balance and deposit value to calculate total checking-balance after deposit | ||
} | ||
$('#checking-amount').val(""); // Clearing the input field for checking amount | ||
}); | ||
$('html').click(function (event) { | ||
let target = event.target; | ||
|
||
$('#savings-deposit').on('click', function () { | ||
const depositValue = Number($('#savings-amount').val()); | ||
if (depositValue > 0) { | ||
manageDeposit( '#savings-balance', depositValue ); // function call passing the id of savings-balance and deposit value to calculate total savings-balance after deposit | ||
if ( target.id === 'checking-deposit' ) { | ||
const depositValue = Number($('#checking-amount').val()); | ||
if (depositValue > 0) { | ||
manageDeposit( '#checking-balance', depositValue ); // function call passing the id of checking-balance and deposit value to calculate total checking-balance after deposit | ||
} | ||
$('#checking-amount').val(""); // Clearing the input field for checking amount | ||
} | ||
$('#savings-amount').val(""); // Clearing the input field for balance amount | ||
}); | ||
|
||
const manageDeposit = function ( idOfAccount, depositValue ) { // Helper function to manage deposits | ||
let currBalance = Number( $(idOfAccount).text().substring(1) ); // To extract only tne number portion of the balance removing the "$" symbol. | ||
currBalance += depositValue; | ||
$(idOfAccount).text( '$' + currBalance ); | ||
setAccountColor( idOfAccount, currBalance ); // function call to set account color | ||
}; | ||
if ( target.id === 'savings-deposit' ) { | ||
const depositValue = Number($('#savings-amount').val()); | ||
if (depositValue > 0) { | ||
manageDeposit( '#savings-balance', depositValue ); // function call passing the id of savings-balance and deposit value to calculate total savings-balance after deposit | ||
} | ||
$('#savings-amount').val(""); // Clearing the input field for balance amount | ||
} | ||
|
||
$('#checking-withdraw').on('click', function () { | ||
const withDrawValue = Number($('#checking-amount').val()); | ||
if (withDrawValue > 0) { | ||
manageWithdraw('#checking-balance', '#savings-balance', withDrawValue); // function call passing the withdraw value, checking account as primary account and saving account as secondary account | ||
} | ||
$('#checking-amount').val(""); | ||
}); | ||
if ( target.id === 'checking-withdraw' ) { | ||
const withDrawValue = Number($('#checking-amount').val()); | ||
if (withDrawValue > 0) { | ||
manageWithdraw('#checking-balance', '#savings-balance', withDrawValue); // function call passing the withdraw value, checking account as primary account and saving account as secondary account | ||
} | ||
$('#checking-amount').val(""); | ||
} | ||
|
||
$('#savings-withdraw').on('click', function () { | ||
const withDrawValue = Number($('#savings-amount').val()); | ||
if (withDrawValue > 0) { | ||
manageWithdraw('#savings-balance', '#checking-balance', withDrawValue); // function call passing the saving account as primary and checking account as secondary and withdraw value | ||
if ( target.id === 'savings-withdraw' ) { | ||
const withDrawValue = Number($('#savings-amount').val()); | ||
if (withDrawValue > 0) { | ||
manageWithdraw('#savings-balance', '#checking-balance', withDrawValue); // function call passing the saving account as primary and checking account as secondary and withdraw value | ||
} | ||
$('#savings-amount').val(""); | ||
} | ||
$('#savings-amount').val(""); | ||
}); | ||
|
||
const manageWithdraw = function ( idOfPrimaryAccount, idOfSecondaryAccount, withDrawValue ) { // Helper function to manage withdrawals | ||
let primaryAccountBalance = Number( $(idOfPrimaryAccount).text().substring(1) ); | ||
let SecondaryAccountBalance = Number( $(idOfSecondaryAccount).text().substring(1) ); | ||
const manageDeposit = function ( iDOfAccount, depositValue ) { // Helper function to manage deposits | ||
let currBalance = Number( $(iDOfAccount).text().substring(1) ); // To extract only tne number portion of the balance removing the "$" symbol. | ||
currBalance = depositValue + currBalance; | ||
$(iDOfAccount).text( '$' + currBalance ); | ||
setAccountColor( iDOfAccount, currBalance ); // function call to set account color | ||
}; | ||
|
||
const manageWithdraw = function ( iDOfPrimaryAccount, iDOfSecondaryAccount, withDrawValue ) { // Helper function to manage withdrawals | ||
let primaryAccountBalance = Number( $(iDOfPrimaryAccount).text().substring(1) ); | ||
let SecondaryAccountBalance = Number( $(iDOfSecondaryAccount).text().substring(1) ); | ||
|
||
if ( primaryAccountBalance >= withDrawValue ) { | ||
primaryAccountBalance -= withDrawValue; | ||
$(idOfPrimaryAccount).text( '$' + primaryAccountBalance ); | ||
primaryAccountBalance = primaryAccountBalance - withDrawValue; | ||
$(iDOfPrimaryAccount).text( '$' + primaryAccountBalance ); | ||
} else { | ||
let totalBalance = primaryAccountBalance + SecondaryAccountBalance; | ||
if ( totalBalance >= withDrawValue ) { | ||
totalBalance -= withDrawValue; | ||
totalBalance = totalBalance - withDrawValue; | ||
primaryAccountBalance = 0; | ||
SecondaryAccountBalance = totalBalance; | ||
$(idOfPrimaryAccount).text( '$' + primaryAccountBalance ); | ||
$(idOfSecondaryAccount).text( '$' + SecondaryAccountBalance ); | ||
$(iDOfPrimaryAccount).text( '$' + primaryAccountBalance ); | ||
$(iDOfSecondaryAccount).text( '$' + SecondaryAccountBalance ); | ||
} | ||
} | ||
|
||
setAccountColor( idOfPrimaryAccount, primaryAccountBalance ); // function call to set primary account color | ||
setAccountColor( idOfSecondaryAccount, SecondaryAccountBalance ); // function call to set secondary account color | ||
setAccountColor( iDOfPrimaryAccount, primaryAccountBalance ); // function call to set primary account color | ||
setAccountColor( iDOfSecondaryAccount, SecondaryAccountBalance ); // function call to set secondary account color | ||
}; | ||
|
||
const setAccountColor = function ( idOfAccount, balance ) { // Helper function to set the account color | ||
const setAccountColor = function ( iDOfAccount, balance ) { // Helper function to set the class for the account | ||
if ( balance === 0 ) { | ||
$(idOfAccount).addClass('zero'); | ||
$(iDOfAccount).addClass('zero'); | ||
} else { | ||
$(idOfAccount).removeClass('zero'); | ||
$(iDOfAccount).removeClass('zero'); | ||
} | ||
}; |