Overview
The application consists of three main classes:- BankAccount: This class represents a single bank account.
- BankManager: This class manages a collection of bank accounts and performs operations such as account creation, deposits, withdrawals, and transfers.
- Main: This class contains the main method and user interface for interacting with the banking system.
Step 1: Define the BankAccount Class
Create a file named BankAccount.java and define the BankAccount class. This class stores account information and includes methods for depositing, withdrawing, and transferring money, as well as displaying account details.package net.javaguides.banking;
public class BankAccount {
private String accountNumber;
private String accountHolder;
private double balance;
// Constructor
public BankAccount(String accountNumber, String accountHolder) {
this.accountNumber = accountNumber;
this.accountHolder = accountHolder;
this.balance = 0.0; // Initial balance is set to 0.0
}
// Deposit money into the account
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Amount deposited successfully. Current Balance: " + balance);
} else {
System.out.println("Invalid amount. Please enter a positive number.");
}
}
// Withdraw money from the account
public void withdraw(double amount) {
if (amount <= 0) {
System.out.println("Invalid amount. Please enter a positive number.");
} else if (amount > balance) {
System.out.println("Insufficient balance. Transaction failed.");
} else {
balance -= amount;
System.out.println("Amount withdrawn successfully. Remaining Balance: " + balance);
}
}
// Transfer money to another bank account
public void transfer(BankAccount otherAccount, double amount) {
if (amount <= balance) {
this.withdraw(amount); // Withdraw from this account
otherAccount.deposit(amount); // Deposit into the other account
System.out.println("Transfer successful. New Balance: " + this.balance);
} else {
System.out.println("Insufficient balance. Transfer failed.");
}
}
// Display account details
public void displayAccountDetails() {
System.out.println("Account Number: " + accountNumber + ", Account Holder: " + accountHolder + ", Balance: " + balance);
}
// Getters and Setters
public String getAccountNumber() { return accountNumber; }
public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; }
public String getAccountHolder() { return accountHolder; }
public void setAccountHolder(String accountHolder) { this.accountHolder = accountHolder; }
public double getBalance() { return balance; }
// No setter for balance to prevent direct modification outside deposit/withdraw methods
}
deposit(double amount): Adds a specified amount to the account's balance if the amount is positive.
withdraw(double amount): Deducts a specified amount from the account's balance if the amount is positive and available.
transfer(BankAccount otherAccount, double amount): Transfers a specified amount from the current account to another, given sufficient balance.
displayAccountDetails(): Prints the account number, holder's name, and current balance to the console.
Step 2: Implementing the BankManager Class
package net.javaguides.banking;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BankManager {
private List<BankAccount> accounts = new ArrayList<>();
// Create a new bank account
public void createAccount(String accountNumber, String accountHolder) {
BankAccount newAccount = new BankAccount(accountNumber, accountHolder);
accounts.add(newAccount);
System.out.println("New account created for " + accountHolder + " with Account Number: " + accountNumber);
}
// Find an account by account number
private BankAccount findAccount(String accountNumber) {
for (BankAccount account : accounts) {
if (account.getAccountNumber().equals(accountNumber)) {
return account;
}
}
return null;
}
// Deposit money into an account
public void depositToAccount(String accountNumber, double amount) {
BankAccount account = findAccount(accountNumber);
if (account != null) {
account.deposit(amount);
} else {
System.out.println("Account not found.");
}
}
// Withdraw money from an account
public void withdrawFromAccount(String accountNumber, double amount) {
BankAccount account = findAccount(accountNumber);
if (account != null) {
account.withdraw(amount);
} else {
System.out.println("Account not found.");
}
}
// Transfer money between two accounts
public void transferBetweenAccounts(String fromAccountNumber, String toAccountNumber, double amount) {
BankAccount fromAccount = findAccount(fromAccountNumber);
BankAccount toAccount = findAccount(toAccountNumber);
if (fromAccount != null && toAccount != null) {
fromAccount.transfer(toAccount, amount);
} else {
System.out.println("One or both accounts not found.");
}
}
// Display details for all accounts
public void displayAllAccounts() {
for (BankAccount account : accounts) {
account.displayAccountDetails();
}
}
}
createAccount(String accountNumber, String accountHolder): Creates and adds a new BankAccount to the accounts list with the given details.
findAccount(String accountNumber): Searches for and returns a BankAccount in the accounts list by the account number.
depositToAccount(String accountNumber, double amount): Finds an account by number and deposits a specified amount into it.
withdrawFromAccount(String accountNumber, double amount): Finds an account by number and withdraws a specified amount from it.
transferBetweenAccounts(String fromAccountNumber, String toAccountNumber, double amount): Transfers a specified amount from one account to another.
displayAllAccounts(): Iterates through and displays details of all accounts in the accounts list.
Step 4: Building the User Interface
package net.javaguides.banking;
import java.util.Scanner;
public class Main {
private static BankManager manager = new BankManager();
public static void main(String[] args) {
startBanking(); // Start the banking operations
}
public static void startBanking() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nWelcome to the Banking Application");
System.out.println("1. Create New Account");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Transfer Money");
System.out.println("5. Display Account Details");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
createAccountUI(scanner);
break;
case 2:
depositUI(scanner);
break;
case 3:
withdrawUI(scanner);
break;
case 4:
transferUI(scanner);
break;
case 5:
manager.displayAllAccounts();
break;
case 6:
System.out.println("Thank you for using the Banking Application.");
return;
default:
System.out.println("Invalid choice. Please choose again.");
}
}
}
private static void createAccountUI(Scanner scanner) {
System.out.print("Enter Account Number: ");
String accountNumber = scanner.nextLine();
System.out.print("Enter Account Holder Name: ");
String accountHolder = scanner.nextLine();
manager.createAccount(accountNumber, accountHolder);
}
private static void depositUI(Scanner scanner) {
System.out.print("Enter Account Number: ");
String accountNumber = scanner.nextLine();
System.out.print("Enter Amount to Deposit: ");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
manager.depositToAccount(accountNumber, amount);
}
private static void withdrawUI(Scanner scanner) {
System.out.print("Enter Account Number: ");
String accountNumber = scanner.nextLine();
System.out.print("Enter Amount to Withdraw: ");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
manager.withdrawFromAccount(accountNumber, amount);
}
private static void transferUI(Scanner scanner) {
System.out.print("Enter From Account Number: ");
String fromAccount = scanner.nextLine();
System.out.print("Enter To Account Number: ");
String toAccount = scanner.nextLine();
System.out.print("Enter Amount to Transfer: ");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
manager.transferBetweenAccounts(fromAccount, toAccount, amount);
}
}
startBanking(): Provides a console-based UI for users to interact with the banking application, allowing them to perform various banking operations.
createAccountUI(Scanner scanner): Prompts the user for account creation details and calls createAccount on the BankManager.
depositUI(Scanner scanner): Prompts the user for deposit details (account number and amount) and calls depositToAccount on the BankManager.
withdrawUI(Scanner scanner): Prompts the user for withdrawal details (account number and amount) and calls withdrawFromAccount on the BankManager.
transferUI(Scanner scanner): Prompts the user for transfer details (from and to account numbers and amount) and calls transferBetweenAccounts on the BankManager.
Step 5: Running the Application
Welcome to the Banking Application
1. Create New Account
2. Deposit Money
3. Withdraw Money
4. Transfer Money
5. Display Account Details
6. Exit
Enter your choice: 1
Enter Account Number: 123
Enter Account Holder Name: Ramesh
New account created for Ramesh with Account Number: 123
Welcome to the Banking Application
1. Create New Account
2. Deposit Money
3. Withdraw Money
4. Transfer Money
5. Display Account Details
6. Exit
Enter your choice: 1
Enter Account Number: 456
Enter Account Holder Name: Vijay
New account created for Vijay with Account Number: 456
Welcome to the Banking Application
1. Create New Account
2. Deposit Money
3. Withdraw Money
4. Transfer Money
5. Display Account Details
6. Exit
Enter your choice: 2
Enter Account Number: 123
Enter Amount to Deposit: 10000
Amount deposited successfully. Current Balance: 10000.0
Welcome to the Banking Application
1. Create New Account
2. Deposit Money
3. Withdraw Money
4. Transfer Money
5. Display Account Details
6. Exit
Enter your choice: 3
Enter Account Number: 123
Enter Amount to Withdraw: 5000
Amount withdrawn successfully. Remaining Balance: 5000.0
Welcome to the Banking Application
1. Create New Account
2. Deposit Money
3. Withdraw Money
4. Transfer Money
5. Display Account Details
6. Exit
Enter your choice: 4
Enter From Account Number: 123
Enter To Account Number: 456
Enter Amount to Transfer: 2000
Amount withdrawn successfully. Remaining Balance: 3000.0
Amount deposited successfully. Current Balance: 2000.0
Transfer successful. New Balance: 3000.0
Welcome to the Banking Application
1. Create New Account
2. Deposit Money
3. Withdraw Money
4. Transfer Money
5. Display Account Details
6. Exit
Enter your choice: 5
Account Number: 123, Account Holder: Ramesh, Balance: 3000.0
Account Number: 456, Account Holder: Vijay, Balance: 2000.0
Welcome to the Banking Application
1. Create New Account
2. Deposit Money
3. Withdraw Money
4. Transfer Money
5. Display Account Details
6. Exit
Enter your choice: 6
Thank you for using the Banking Application.
Comments
Post a Comment
Leave Comment