├── .vscode └── settings.json ├── 1_constructor_function.js ├── 2_prototype.js ├── 3_classes.js ├── 4_inheritance.js ├── 5_encapsulation.js ├── 6_static.js └── index.html /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "liveServer.settings.port": 5501 3 | } 4 | -------------------------------------------------------------------------------- /1_constructor_function.js: -------------------------------------------------------------------------------- 1 | function BankAccount(customerName, balance = 0) { 2 | this.customerName = customerName; 3 | this.accountNumber = Date.now(); 4 | this.balance = balance; 5 | 6 | // this.deposit = function (amount) { 7 | // this.balance += amount; 8 | // }; 9 | 10 | // this.withdraw = (amount) => { 11 | // this.balance -= amount; 12 | // }; 13 | } 14 | 15 | // const rakeshAccount = new BankAccount('Rakesh K', 1000); 16 | // const johnAccount = new BankAccount('John Doe'); 17 | // rakeshAccount.deposit(5000); 18 | // johnAccount.deposit(1000); 19 | // rakeshAccount.withdraw(2000); 20 | 21 | // console.log(rakeshAccount, johnAccount); 22 | 23 | // ============================ 24 | const accounts = []; 25 | const accountForm = document.querySelector('#accountForm'); 26 | const customerName = document.querySelector('#customerName'); 27 | const balance = document.querySelector('#balance'); 28 | 29 | const depositForm = document.querySelector('#depositForm'); 30 | const accountNumber = document.querySelector('#accountNumber'); 31 | const amount = document.querySelector('#amount'); 32 | 33 | accountForm.addEventListener('submit', (e) => { 34 | e.preventDefault(); 35 | const account = new BankAccount(customerName.value, +balance.value); 36 | accounts.push(account); 37 | console.log(accounts); 38 | }); 39 | 40 | depositForm.addEventListener('submit', (e) => { 41 | e.preventDefault(); 42 | const account = accounts.find( 43 | (account) => account.accountNumber === +accountNumber.value 44 | ); 45 | account.deposit(+amount.value); 46 | console.log(accounts); 47 | }); 48 | -------------------------------------------------------------------------------- /2_prototype.js: -------------------------------------------------------------------------------- 1 | function BankAccount(customerName, balance = 0) { 2 | this.customerName = customerName; 3 | this.accountNumber = Date.now(); 4 | this.balance = balance; 5 | 6 | // this.deposit = function (amount) { 7 | // this.balance += amount; 8 | // }; 9 | 10 | // this.withdraw = (amount) => { 11 | // this.balance -= amount; 12 | // }; 13 | } 14 | 15 | const rakeshAccount = new BankAccount('Rakesh K'); 16 | const johnAccount = new BankAccount('John Doe', 1000); 17 | // console.log(rakeshAccount, johnAccount); 18 | 19 | BankAccount.prototype.deposit = function (amount) { 20 | this.balance += amount; 21 | }; 22 | 23 | BankAccount.prototype.withdraw = (amount) => { 24 | this.balance -= amount; 25 | }; 26 | 27 | // johnAccount.deposit(3000); 28 | 29 | console.log(johnAccount); 30 | -------------------------------------------------------------------------------- /3_classes.js: -------------------------------------------------------------------------------- 1 | class BankAccount { 2 | customerName; 3 | accountNumber; 4 | balance = 0; 5 | 6 | constructor(customerName, balance = 0) { 7 | this.customerName = customerName; 8 | this.accountNumber = Date.now(); 9 | this.balance = balance; 10 | } 11 | 12 | deposit(amount) { 13 | this.balance += amount; 14 | } 15 | 16 | withdraw(amount) { 17 | this.balance -= amount; 18 | } 19 | } 20 | 21 | // const rakeshAccount = new BankAccount('Rakesh K', 1000); 22 | // rakeshAccount.deposit(4000); 23 | 24 | // const johnAccount = new BankAccount('John Doe'); 25 | // johnAccount.deposit(500); 26 | // johnAccount.withdraw(200); 27 | // console.log(rakeshAccount); 28 | -------------------------------------------------------------------------------- /4_inheritance.js: -------------------------------------------------------------------------------- 1 | class BankAccount { 2 | customerName; 3 | accountNumber; 4 | balance = 0; 5 | 6 | constructor(customerName, balance = 0) { 7 | this.customerName = customerName; 8 | this.accountNumber = Date.now(); 9 | this.balance = balance; 10 | } 11 | 12 | deposit(amount) { 13 | this.balance += amount; 14 | } 15 | 16 | withdraw(amount) { 17 | this.balance -= amount; 18 | } 19 | } 20 | 21 | class CurrentAccount extends BankAccount { 22 | transactionLimit = 50000; 23 | 24 | constructor(customerName, balance = 0) { 25 | super(customerName, balance); 26 | } 27 | 28 | takeBusinessLoan(amount) { 29 | console.log('Taking business loan: ' + amount); 30 | } 31 | } 32 | 33 | class SavingAccount extends BankAccount { 34 | transactionLimit = 10000; 35 | 36 | constructor(customerName, balance = 0) { 37 | super(customerName, balance); 38 | } 39 | 40 | takePersonalLoan(amount) { 41 | console.log('Taking personal loan: ' + amount); 42 | } 43 | } 44 | 45 | const rakeshAccount = new SavingAccount('Rakesh K', 500); 46 | rakeshAccount.deposit(500); 47 | rakeshAccount.withdraw(100); 48 | rakeshAccount.takePersonalLoan(40000); 49 | console.log(rakeshAccount); 50 | 51 | // function BankAccount(customerName, balance = 0) { 52 | // this.customerName = customerName; 53 | // this.accountNumber = Date.now(); 54 | // this.balance = balance; 55 | // } 56 | 57 | // BankAccount.prototype.deposit = function (amount) { 58 | // this.balance += amount; 59 | // }; 60 | 61 | // BankAccount.prototype.withdraw = function (amount) { 62 | // this.balance -= amount; 63 | // }; 64 | 65 | // function CurrentAccount(customerName, balance = 0) { 66 | // BankAccount.call(this, customerName, balance); 67 | // this.transactionLimit = 50000; 68 | // } 69 | 70 | // CurrentAccount.prototype = Object.create(BankAccount.prototype); 71 | 72 | // CurrentAccount.prototype.takeBusinessLoan = function (amount) { 73 | // console.log('Taking business loan: ' + amount); 74 | // }; 75 | 76 | // function SavingAccount(customerName, balance = 0) { 77 | // BankAccount.call(this, customerName, balance); 78 | // this.transactionLimit = 10000; 79 | // } 80 | // SavingAccount.prototype = Object.create(BankAccount.prototype); 81 | 82 | // SavingAccount.prototype.takePersonalLoan = function (amount) { 83 | // console.log('Taking personal loan: ' + amount); 84 | // }; 85 | 86 | // const rakeshAccount = new SavingAccount('Rakesh K', 500); 87 | // rakeshAccount.deposit(500); 88 | // rakeshAccount.withdraw(100); 89 | // rakeshAccount.takePersonalLoan(40000); 90 | // // console.log(rakeshAccount); 91 | -------------------------------------------------------------------------------- /5_encapsulation.js: -------------------------------------------------------------------------------- 1 | class BankAccount { 2 | customerName; 3 | accountNumber; 4 | #balance = 0; 5 | 6 | constructor(customerName, balance = 0) { 7 | this.customerName = customerName; 8 | this.accountNumber = Date.now(); 9 | this.#balance = balance; 10 | } 11 | 12 | deposit(amount) { 13 | this.#balance += amount; 14 | } 15 | 16 | withdraw(amount) { 17 | this.#balance -= amount; 18 | } 19 | 20 | set balance(amount) { 21 | if (isNaN(amount)) { 22 | throw new Error('Amount is not a valid input'); 23 | } 24 | this.#balance = amount; 25 | } 26 | 27 | get balance() { 28 | return this.#balance; 29 | } 30 | } 31 | 32 | class CurrentAccount extends BankAccount { 33 | transactionLimit = 50000; 34 | 35 | constructor(customerName, balance = 0) { 36 | super(customerName, balance); 37 | } 38 | 39 | #calculateInterest(amount) { 40 | console.log('Calculating interest'); 41 | } 42 | 43 | takeBusinessLoan(amount) { 44 | // Logic 45 | this.#calculateInterest(amount); 46 | console.log('Taking business loan: ' + amount); 47 | } 48 | } 49 | 50 | const rakeshAccount = new CurrentAccount('Rakesh K', 2000); 51 | // rakeshAccount.setBalance(400); 52 | // rakeshAccount.balance = 5000; 53 | rakeshAccount.takeBusinessLoan(40000); 54 | console.log(rakeshAccount); 55 | -------------------------------------------------------------------------------- /6_static.js: -------------------------------------------------------------------------------- 1 | // class Config { 2 | // static dbUser = 'usename'; 3 | // static dbPassword = 'secret'; 4 | // static apiToken = 'abcd'; 5 | // } 6 | 7 | // console.log(Config.apiToken); 8 | 9 | // // Increment 10 | class User { 11 | // static id = 1; 12 | static cache = { 13 | 1: 'some value', 14 | }; 15 | 16 | constructor(name, age, income) { 17 | this.name = name; 18 | this.age = age; 19 | this.income = income; 20 | this.id = User.id++; 21 | } 22 | 23 | // checkCache() {} 24 | 25 | static { 26 | console.log('Initialized'); 27 | } 28 | 29 | static hasInCache() { 30 | console.log(User.cache); 31 | } 32 | 33 | // static compareByAge(user1, user2) { 34 | // return user1.age - user2.age; 35 | // } 36 | 37 | // static compareByIncome(user1, user2) { 38 | // return user1.income - user2.income; 39 | // } 40 | } 41 | 42 | // User.hasInCache(); 43 | // const user1 = new User('Rakesh K', 30, 5000); 44 | User.hasInCache(); 45 | User.hasInCache(); 46 | // const user2 = new User('John Doe', 40, 10000); 47 | // const user3 = new User('Jane Doe', 20, 7000); 48 | 49 | // console.log(user1, user2, user3); 50 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | OOP 8 | 9 | 10 |

Coder's Bank

11 |
12 | 13 | 14 | 15 |
16 |
17 |
18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | --------------------------------------------------------------------------------