Skip to content

Instantly share code, notes, and snippets.

@rajeevAgilehub
Last active January 10, 2021 02:42
Show Gist options
  • Save rajeevAgilehub/a04af55781018cbda98b14d8c8c7b905 to your computer and use it in GitHub Desktop.
Save rajeevAgilehub/a04af55781018cbda98b14d8c8c7b905 to your computer and use it in GitHub Desktop.
var MSG_FACTORY = {
'INSUFFICIENT_BALANCE': 'You have not sufficient balance in your account',
'WITHDRAW_LIMIT': 'Withdrawal amount is more than limit',
'WITHDRAW': 'Money withdrawn from your account',
'DEPOSIT': 'Money deposited to your account'
}
/* This module is to notify the user whenever there is an account balance related alert required,
Its takes input and alert based on msg type */
function notifyUser(msg){
switch(msg){
case MSG_FACTORY.INSUFFICIENT_BALANCE:
return alert('You have not sufficient balance in your account');
case MSG_FACTORY.WITHDRAW:
return alert('You have withdrawn money from your account');
case MSG_FACTORY.DEPOSIT:
return alert('You have deposited money to your account');
}
}
/* This module is to LOG the msg whenever there is an activity in the account. Like:- deposit or withdrawal */
function logger(msg, amount){
switch(msg){
case MSG_FACTORY.WITHDRAW:
console.log('You have withdrawn money from your account '+amount+ 'on Date: ', new Date());
return;
case MSG_FACTORY.DEPOSIT:
console.log('You have deposited money to your account '+amount+ 'on Date: ', new Date());
return;
}
}
/* AccountManager class is responsible for only managing deposit or withdrawal of money from account, Also it notify and alert
user*/
class AccountManager{
constructor(){
this.amount = 0;
this.withdrawLimit = 10000;
}
deposit(depositAmount) {
this.amount = this.amount + depositAmount;
logger(MSG_FACTORY.DEPOSIT, depositAmount);
}
withdraw(withdrawalAmount) {
if(withdrawalAmount > this.amount) {
notifyUser(MSG_FACTORY.INSUFFICIENT_BALANCE);
} else if(withdrawalAmount > this.withdrawLimit){
notifyUser(MSG_FACTORY.WITHDRAW_LIMIT);
} else {
this.amount = this.amount - withdrawalAmount;
logger(MSG_FACTORY.WITHDRAW, withdrawalAmount);
}
}
}
var obj = new AccountManager();
obj.deposit(1000);
obj.withdraw(500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment