JavaScript Banking System
A simple banking system where users can manage their accounts.

ES6 Classes and Modules
Classes in JavaScript make it much easier and quicker to create and update multiple Objects. They are a template for how Objects can be used. Instead of writing a new piece of code for every Object, you can easily refer to the Parameters and Properties within the defined Class. It then automatically executes a Method for every instance of a defined Object.
Classes use a constructor Method that can hold multiple Parameters. The Class name is defined with a capital letter to easily note it as a Class instead of an Object.
class ClassName {
constructor() {}
In this project, I wanted to create multiple Parameters for a bank account. I added an (account number), an (account holder) and a (balance). I added a value of 0 to (balance) which sets a numerical data type that doesn't add onto any set balances.
class BankAccount {
constructor(accountNumber, accountHolder, balance = 0) {}
I then defined each Parameter with a Property of the same name. No Objects have been created yet, but using the this keyword refers to any Objects that will be created later and acts like a placeholder.
class BankAccount {
constructor(accountNumber, accountHolder, balance = 0) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = balance;
}
I created a separate file to store the account users. I used the import and export keywords to be able to pass information between both JavaScript files. This helps break JavaScript code into smaller, manageable chunks.
export default BankAccount;
import BankAccount from "./bankAccount.js";
I then created the Objects which represent each user. Using the new keyword creates a new instance of the (BankAccount) Class, with their set Values for each Parameter.
const secondBank = new BankAccount (75694284, "John Doe");
const thirdBank = new BankAccount (58544932, "Tom Smith", 1000);
Methods and Functions used
I then implemented Methods for depositing, withdrawing and checking balance. These were written inside the BankAccount Class.
For depositing, I created a Method (deposit) with a Parameter (amount).
deposit(amount) {}
I added this.balance to refer to the balance Property. I used the Addition Assignment Operator += to add the amount to the balance and assign a new value.
When executed, the console outputs the amount the user has deposited and their current balance. I used Template Literals and String Interpolation to implement the Object values into Strings.
deposit(amount) {
this.balance += amount;
console.log(`$${amount} deposited. Current balance: $${this.balance}`);
To withdraw an amount, I added a Conditional Statement. If the amount wanted withdrawing is larger than the current balance, the console will output "Insufficient funds".
Otherwise the amount is subtracted from the balance and assigned a new value. The console will output the amount withdrawn and the current balance.
withdraw(amount) {
if (amount > this.balance) {
console.log ("Insufficient funds");
} else {
this.balance -= amount;
console.log (`$${amount} withdrawn. Current balance: $${this.balance}`);
}
To check a balance, I created a Function that returns an output. It takes the name of the account holder and tells them their current balance.
checkBalance() {
console.log(`Account balance for ${this.accountHolder}: $${this.balance}`);
To show the outputs through the browser, I created a basic HTML document incorporating the Script tag with the type set to module and linked my JavaScript files.
<body>
<h1>Banking System</h1>
<h4>Withdraw money</h4>
<h4>Deposit money</h4>
<h4>Check balance</h4>
<script type="module" src="bankAccount.js"></script>
<script type="module" src="index.js"></script>
</body>
Using the Methods
To call the Functions, type in the Object that you want to access and what Method you want to apply with a Value if needed.
To deposit 100:
secondBank.deposit(100)
To withdraw 50:
secondBank.withdraw(50);
To check the balance:
secondBank.checkBalance();
This demonstrates all the various outputs
What I learned from this project
This project helped me understand the purpose for using Classes and how to use them. If you want to create multiple objects that use the same formula, using Classes is a great way to speed up the process. I know how to define a Class and the syntax used.
I also understand how to apply functions and methods that are tied to a defined Class. When using multiple objects that need to be called by the same function/ method, using the this keyword is beneficial. I understand how to test my methods work using the console and input different values to show different results.
View the Live Site
Full code on GitHub
Watch the YT Walkthrough
Comments
Join the discussionNo comments yet. Be the first to comment.
