Created
August 19, 2020 05:21
-
-
Save velotiotech/849964d1e2f93c985bf987a2bf8a987a to your computer and use it in GitHub Desktop.
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
// Here AccountManager is responsible to create new account of type | |
// Savings or Current with the unique account number | |
let currentAccountNumber = 0; | |
class AccountManager { | |
createAccount(type, details) { | |
const accountNumber = AccountManager.getUniqueAccountNumber(); | |
let account; | |
if (type === 'current') { | |
account = new CurrentAccount(); | |
} else { | |
account = new SavingsAccount(); | |
} | |
return account.addAccount({ accountNumber, details }); | |
} | |
static getUniqueAccountNumber() { | |
return ++currentAccountNumber; | |
} | |
} | |
// class Accounts maintains the list of all accounts created | |
class Accounts { | |
constructor() { | |
this.accounts = []; | |
} | |
addAccount(account) { | |
this.accounts.push(account); | |
return this.successMessage(complaint); | |
} | |
getAccount(accountNumber) { | |
return this.accounts.find(account => account.accountNumber === accountNumber); | |
} | |
successMessage(account) {} | |
} | |
// CurrentAccounts extends the implementation of Accounts for providing more specific success messages on successful account creation | |
class CurrentAccounts extends Accounts { | |
constructor() { | |
super(); | |
if (CurrentAccounts.exists) { | |
return CurrentAccounts.instance; | |
} | |
CurrentAccounts.instance = this; | |
CurrentAccounts.exists = true; | |
return this; | |
} | |
successMessage({ accountNumber, details }) { | |
return `Current Account created with ${details}. ${accountNumber} is your account number.`; | |
} | |
} | |
// Same here, SavingsAccount extends the implementation of Accounts for providing more specific success messages on successful account creation | |
class SavingsAccount extends Accounts { | |
constructor() { | |
super(); | |
if (SavingsAccount.exists) { | |
return SavingsAccount.instance; | |
} | |
SavingsAccount.instance = this; | |
SavingsAccount.exists = true; | |
return this; | |
} | |
successMessage({ accountNumber, details }) { | |
return `Savings Account created with ${details}. ${accountNumber} is your account number.`; | |
} | |
} | |
// usage | |
// Here we are hiding the complexities of creating account | |
const accountManager = new AccountManager(); | |
const currentAccount = accountManager.createAccount('current', { name: 'John Snow', address: 'pune' }); | |
const savingsAccount = accountManager.createAccount('savings', { name: 'Petter Kim', address: 'mumbai' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment