Java Program for Bank Account Deposit Withdraw

Bank Account Program in Java:

At first, we created six Java files to develop this Java Banking Account Project.
Such as 1. bankingapp.java
2. login.java
3. menu.java
4. banking_account.java
5. deposit.java
6. invalid_transaction.java

Source code for bankingapp.java:

public class bankingapp {
public static void main(String[] args) throws Exception {
System.out.println("Welcome to ABC Bank !");
banking_account b = new banking_account();
b.initiate();
}
}

Source code for login.java:

import java.io.*;
import java.util.Scanner;
class login{
int ac_number = 1249498014;
int ac_pass = 41089494;
int ac, pw;
public void acceptInput(){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter ABC Bank Account Number: ");
ac = scanner.nextInt();
System.out.print("Enter Password: ");
pw = scanner.nextInt();
}
public void verify() throws Exception{
if(ac == ac_number && pw == ac_pass)
{
System.out.println("Your ABC Bank Account Login Successfully !");
banking_account b = new banking_account();
System.out.println(" ");
System.out.println("Your Account Balance is: "+b.getBalance()+" Rupees");
System.out.println(" ");
menu m1 = new menu();
m1.showMenu();
}else{
invalid_transaction loginfailed = new invalid_transaction("Incorrect Login Credentials !");
System.out.println(loginfailed.getMessage());
throw loginfailed;
}
}
}

Source code for menu.java:

import java.util.Scanner;
class menu{
int selectedOption;
banking_account b = new banking_account();
public void showMenu()
{
System.out.println("Please Select an option below:");
System.out.println("Press 1 to Deposit Amount.");
System.out.println("Press 2 to Withdraw Amount.");
System.out.println("Press 3 to View Balance");
System.out.println("Press any key to Exit");
System.out.println(" ");
Scanner scanner = new Scanner(System.in);
System.out.print("Press any key: ");
selectedOption = scanner.nextInt();
switch (selectedOption) {
case 1:
deposit d = new deposit();
int depamt = d.userInput();
b.add(depamt);
showMenu();
break;
case 2:
System.out.print("Please Enter the amount to withdraw: ");
int withamt=scanner.nextInt();
b.withdraw(withamt);
showMenu();
break;
case 3:
System.out.println("Your Account Balance is "+b.getBalance()+" Rupees");
System.out.println(" ");
showMenu();
break;
default:
System.out.println("Transaction Ended, Your ABC Bank Account Logout Successfully !");
System.exit(0);
break;
}
}
}

Source code for banking_account.java:

import java.util.Scanner;
class banking_account{
int amount = 5000;
public void initiate()
{
login l1 = new login();
try{
l1.acceptInput();
l1.verify();
}catch(Exception e)
{
try{
l1.acceptInput();
l1.verify();
}catch(Exception f)
{
}
}
}
public int getBalance(){
return amount;
}
public void add(int amt){
amount = amount + amt;
System.out.println("Amount deposited Successfully");
System.out.println(" ");
System.out.println("Total Balance: " +amount);
System.out.println(" ");
}
public void withdraw(int amt){
System.out.println(" ");
if(amount < amt)
{
invalid_transaction invalidWithDraw = new invalid_transaction("InValid Withdrawal Amount");
System.out.println(invalidWithDraw.getMessage());
}else{
amount = (amount - amt);
System.out.println("Please Collect your " + amt +" Rupees");
System.out.println(" ");
System.out.println("Available Balance: " +amount);
System.out.println(" ");
}
}
}

Source code for deposit.java:

import java.util.Scanner;
class deposit{
int amt= 0;
public int userInput()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the amount to be deposited: ");
amt = scanner.nextInt();
if(amt<=0)
{
invalid_transaction depositnegativeError = new invalid_transaction("Invalid Deposit Amount");
System.out.println(depositnegativeError.getMessage());
userInput();
}else{
return amt;
}
return amt;
}
}

Source code for invalid_transaction.java:

class invalid_transaction extends Exception
{
String errorMessage;
public invalid_transaction(String message)
{
errorMessage = message;
}
public String getMessage()
{
return errorMessage;
}
}

Some important methods:

getBalance(): It simply returns the available bank account balance.

add(amt): It is used to deposit the amount or update the balance(amount).

withdrw(amt): It removes the amount from the balance amount from a bank account.

acceptInput(): It is used to ask and take input from the user.

verify(): This method is used to check if the login was successful or not successful. If the log is successful then use the banking class to fetch the balance and then show a menu-driven option to the user to select the menu.

If the login isn’t successful then show a proper message to a user by using the invalid_transaction Customized Exception class.

invalid_transaction: This exception class is used to handle all user errors.

Note: To log in, you have to enter the account number = 1249498014 and password = 41089494

Output-1:

Login Account:
Java Program for Bank Account Deposit Withdraw

Output-2:

Deposit:
Java Program for Bank Account Deposit Withdraw

Output-3:

Withdraw:
Java Program for Bank Account Deposit Withdraw

Output-4:

View Balance & Logout Account:
Java Program for Bank Account Deposit Withdraw