The Bank Management System is a popular project among students and professionals aiming to understand object-oriented programming (OOP) principles, specifically in Java. This blog post will introduce you to the development of a simple bank management system in Java, explaining the code and showcasing the expected output.
System Overview
Our Bank Management System will handle:
- Creating a new bank account.
- Checking an account's balance.
- Depositing money.
- Withdrawing money.
- Displaying all accounts.
Implementation
1. Account Class:
This class will define the basic properties and behaviors of a bank account.
public class Account {
private int accountNumber;
private String holderName;
private double balance;
public Account(int accountNumber, String holderName, double balance) {
this.accountNumber = accountNumber;
this.holderName = holderName;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds!");
}
}
public double getBalance() {
return balance;
}
@Override
public String toString() {
return "AccountNumber: " + accountNumber + ", Holder: " + holderName + ", Balance: $" + balance;
}
}
2. Bank Class:
This class will manage multiple accounts and the operations related to them.
import java.util.ArrayList;
import java.util.List;
public class Bank {
private List<Account> accounts;
public Bank() {
accounts = new ArrayList<>();
}
public void createAccount(int accountNumber, String holderName, double initialBalance) {
Account newAccount = new Account(accountNumber, holderName, initialBalance);
accounts.add(newAccount);
}
public Account getAccount(int accountNumber) {
for (Account account : accounts) {
if (account.getAccountNumber() == accountNumber) {
return account;
}
}
return null;
}
public void displayAccounts() {
for (Account account : accounts) {
System.out.println(account);
}
}
}
3. Main Application:
The Main class will serve as our application entry point.
public class Main {
public static void main(String[] args) {
Bank myBank = new Bank();
// Create accounts
myBank.createAccount(101, "John Doe", 1000.50);
myBank.createAccount(102, "Jane Smith", 500.75);
// Display accounts
myBank.displayAccounts();
// Deposit money
Account johnsAccount = myBank.getAccount(101);
johnsAccount.deposit(200);
System.out.println("John's updated balance: $" + johnsAccount.getBalance());
// Withdraw money
johnsAccount.withdraw(50);
System.out.println("John's updated balance after withdrawal: $" + johnsAccount.getBalance());
}
}
Complete Code and Output
import java.util.ArrayList;
import java.util.List;
public class BankManagementSystem {
static class Account {
private int accountNumber;
private String holderName;
private double balance;
public Account(int accountNumber, String holderName, double balance) {
this.accountNumber = accountNumber;
this.holderName = holderName;
this.balance = balance;
}
public int getAccountNumber() {
return accountNumber;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds!");
}
}
public double getBalance() {
return balance;
}
@Override
public String toString() {
return "AccountNumber: " + accountNumber + ", Holder: " + holderName + ", Balance: $" + balance;
}
}
static class Bank {
private List<Account> accounts;
public Bank() {
accounts = new ArrayList<>();
}
public void createAccount(int accountNumber, String holderName, double initialBalance) {
Account newAccount = new Account(accountNumber, holderName, initialBalance);
accounts.add(newAccount);
}
public Account getAccount(int accountNumber) {
for (Account account : accounts) {
if (account.getAccountNumber() == accountNumber) {
return account;
}
}
return null;
}
public void displayAccounts() {
for (Account account : accounts) {
System.out.println(account);
}
}
}
public static void main(String[] args) {
Bank myBank = new Bank();
// Create accounts
myBank.createAccount(101, "John Doe", 1000.50);
myBank.createAccount(102, "Jane Smith", 500.75);
// Display accounts
System.out.println("Displaying all accounts:");
myBank.displayAccounts();
// Deposit money
Account johnsAccount = myBank.getAccount(101);
johnsAccount.deposit(200);
System.out.println("\nJohn's updated balance after depositing $200: $" + johnsAccount.getBalance());
// Withdraw money
johnsAccount.withdraw(50);
System.out.println("John's updated balance after withdrawing $50: $" + johnsAccount.getBalance());
// Attempt to withdraw more than balance
System.out.println("\nAttempting to withdraw $2000 from Jane's account:");
Account janesAccount = myBank.getAccount(102);
janesAccount.withdraw(2000);
}
}
Output:
Displaying all accounts:
AccountNumber: 101, Holder: John Doe, Balance: $1000.5
AccountNumber: 102, Holder: Jane Smith, Balance: $500.75
John's updated balance after depositing $200: $1200.5
John's updated balance after withdrawing $50: $1150.5
Attempting to withdraw $2000 from Jane's account:
Insufficient funds!
.
Conclusion
Java, being an object-oriented programming language, allows us to model real-world entities like bank accounts naturally. Our Bank Management System is a basic representation of how OOP can simplify and organize code. As a next step, you can enhance this system by integrating database operations, adding transaction histories, or even incorporating simple GUI components using Java's Swing or JavaFX libraries. The goal is to start small and continuously iterate, adding more features and improving the system as you progress in your learning journey.