BankAccount Class
Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 21 of 56.
Challenge
In this project we will create a BankAccount class from scratch to manage a customer's bank account.
Your first task is to create a class named BankAccount with:
- Constructor that takes two parameters:
accountHolder(string): The name of the account ownerinitialBalance(number, optional): Starting balance (defaults to 0)
- Method
<strong>getBalance()</strong>which:- Returns the current account balance
- No parameters needed
- Method
<strong>getAccountInfo()</strong>that:- Returns a formatted string:
"${accountHolder}: ${balance}" - Example: If accountHolder is "John" and balance is 500, returns
"John: $500"
- Returns a formatted string:
Try it yourself
// TODO: Create a class named BankAccount
// TODO: Add a constructor that takes two parameters: accountHolder and initialBalance (defaults to 0)
// TODO: Add a method getBalance() that returns the current account balance
// TODO: Add a method getAccountInfo() that returns a formatted string: "${accountHolder}: $${balance}"
// Test
const testAccount = new BankAccount('Alex Johnson', 500);
console.log('Initial state:');
console.log(testAccount.getAccountInfo()); // "Alex Johnson: $500"
console.log('Balance:', testAccount.getBalance()); // 500All lessons in Object Oriented Programming
1Objects & The this Keyword
Quick Review: ObjectsAdding Methods to ObjectsUnderstanding the this KeywordConstructor FunctionsThe new KeywordRecap Challenge7 Inheritance & The extends Key
InheritanceThe "is-a" RelationshipThe extends KeywordThe super() MethodInheriting Properties&MethodsRecap Challenge2Organizing Code
What are Modules?Exporting with exportImporting with importDefault vs. Named Exports8Organizing OOP Code
Organize Classes into Modules11Project: A Shape Renderer
Setup: Shape Class & ExportCircle Class Inheritance9Static Methods & Properties
Class-Level vs. Instance-LevelStatic PropertiesStatic Utility MethodsRecap challenge