Java Program to Create a Simple Banking Menu

In this tutorial, we will learn how to write a Java program to create a simple banking menu using the switch statement. The program will display a banking menu that allows users to check their account balance, deposit money, withdraw money, and exit the application.

This program helps you understand the practical use of the switch statement, do-while loop, Scanner class, and conditional statements in Java.

Java Program to Create a Simple Banking Menu Using Switch

Let’s write a Java program that implements a menu-driven banking system using a switch statement and while loop. It provides a simple, interactive console interface allowing users to check their balance, deposit funds, withdraw cash, and exit the application smoothly.

Java Program

 // Import the Scanner class to read user input from the console
import java.util.Scanner;
public class BankingMenu {
    public static void main(String[] args) {
        // Create a Scanner object for capturing console inputs
        Scanner sc = new Scanner(System.in);

        // Initialize account and control variables
        double balance = 1000.00; // Initial default account balance.
        double amount;            // Temporary variable to hold deposit or withdrawal values.
        int choice = 0;           // Variable to store the user's menu choice.

        // While loop keeps the menu running repeatedly until the user chooses to exit (option 4).
        while (choice != 4) {

            // Displaying the console menu options.
            System.out.println("\n===== SIMPLE BANKING MENU =====");
            System.out.println("1. Check Balance");
            System.out.println("2. Deposit Money");
            System.out.println("3. Withdraw Money");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");

            // Read the integer input provided by the user
            choice = sc.nextInt();

            // Switch statement to route execution based on the user's menu selection.
            switch (choice) {

                case 1:
                    // Option 1: Display the current account balance
                    System.out.println("Current Balance: Rs. " + balance);
                    break;

                case 2:
                    // Option 2: Handle money deposits
                    System.out.print("Enter deposit amount: Rs. ");
                    amount = sc.nextDouble();

                    // Check if the entered deposit amount is valid (positive)
                    if (amount > 0) {
                        balance += amount; // Add deposit to the total balance
                        System.out.println("Amount deposited successfully.");
                        System.out.println("Updated Balance: Rs. " + balance);
                    } else {
                        System.out.println("Invalid deposit amount.");
                    }
                    break;

                case 3:
                    // Option 3: Handle cash withdrawals
                    System.out.print("Enter withdrawal amount: Rs. ");
                    amount = sc.nextDouble();

                    // Verify that the amount is positive and the user has enough balance
                    if (amount > 0 && amount <= balance) {
                        balance -= amount; // Deduct withdrawal from the total balance
                        System.out.println("Please collect your money.");
                        System.out.println("Updated Balance: Rs. " + balance);
                    } else if (amount <= 0) {
                        System.out.println("Invalid withdrawal amount.");
                    } else {
                        System.out.println("Insufficient balance.");
                    }
                    break;

                case 4:
                    // Option 4: Exit the banking loop
                    System.out.println("Thank you for using our banking service!");
                    break;

                default:
                    // Handles numbers or inputs outside the valid 1-4 menu range
                    System.out.println("Invalid choice! Please try again.");
            }
        }

        // Close the Scanner resource to prevent memory leaks
        sc.close();
    }
}

Output:

===== SIMPLE BANKING MENU =====
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Enter your choice: 1
Current Balance: Rs. 1000.0

===== SIMPLE BANKING MENU =====
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Enter your choice: 2
Enter deposit amount: Rs. 500
Amount deposited successfully.
Updated Balance: Rs. 1500.0

===== SIMPLE BANKING MENU =====
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Enter your choice: 3
Enter withdrawal amount: Rs. 200
Please collect your money.
Updated Balance: Rs. 1300.0

===== SIMPLE BANKING MENU =====
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Enter your choice: 4
Thank you for using our banking service!

Explanation of the Java Banking Menu Program

Let’s understand how the above Java banking menu program works step by step.

Step 1: Import the Scanner Class

  • The statement import java.util.Scanner; imports the Scanner class from the java.util package to accept input from the user through the keyboard. It allows the program to read the user’s menu choice and the amount to deposit or withdraw.

Step 2: Declare the Main Class and main() Method

  • We declare a public class named BankingMenu and define the main() method inside it.
  • The main() method is the entry point of a Java application. When we run this program, the Java Virtual Machine (JVM) starts executing the code from the main() method.

 Step 3: Create a Scanner Object

  • Then, we create a Scanner object to read user input from the keyboard.
  • The System.in argument specifies that the Scanner object will read input from the standard input device (keyboard).
  • We use the following Scanner methods in this program:
    • nextInt(): Reads an integer value entered by the user.
    • nextDouble(): Reads a decimal or floating-point value entered by the user.

Step 4: Declare and Initialize Variables

We declare and initialize the following variables in the program:

  • The variable balance stores the current account balance. Initially, it is set to Rs. 1000.00.
  • The variable amount stores the amount entered by the user for depositing or withdrawing money.
  • The variable choice stores the user’s selected banking menu option.

We use the double data type for balance and amount because these variables may contain decimal values. The int data type is used for choice because the menu options are whole numbers.

Step 5: Use the while Loop to Display the Banking Menu

  • We use a while loop to repeatedly display the banking menu until the user selects option 4 (Exit).
  • The condition choice != 4 checks whether the value of the choice variable is not equal to 4.
  • Since we initially set choice = 0, the condition evaluates to true, and the loop body executes.
  • Inside the loop, the program displays four banking options and asks the user to enter a choice.
  • The statement choice = sc.nextInt(); reads the user’s input and stores it in the choice variable.
  • After that, the program executes the switch statement to perform the selected banking operation.

Step 6: Use the switch Statement to Perform Banking Operations

  • We use the switch statement to perform different banking operations based on the user’s choice.
  • The switch statement compares the value of the choice variable with the values specified in different case labels.

Let’s understand each case one by one.

Step 7: Check Account Balance (case 1)

  • When the user enters 1, the program executes case 1 and displays the current account balance.
  • For example, if the initial balance is Rs. 1000.00, the output will be “Current Balance: Rs. 1000.0“.
  • The break statement terminates the current case and transfers control outside the switch statement.

After executing the switch statement, the program checks the while loop condition again. If the user has not selected option 4, the menu is displayed again.

Step 8: Deposit Money (case 2)

  • When the user enters 2, the program executes case 2 and asks the user to enter the deposit amount.
  • The statement amount = sc.nextDouble(); accepts the amount entered by the user and stores it in the amount variable.
  • Next, the if-else statement checks whether the entered amount is greater than zero.
  • If the condition is true, the amount is added to the current balance using the += compound assignment operator.
    If the condition is false, the program displays an invalid deposit amount message.
  • For example, suppose the current balance is Rs. 1000 and the user deposits Rs. 500.
    The statement balance += amount; is equivalent to balance = balance + amount;. Therefore, the updated balance becomes Rs. 1500.

Step 9: Withdraw Money (case 3)

  • When the user enters 3, the program executes case 3 and asks the user to enter the withdrawal amount.
  • The if-else if-else statement checks whether the withdrawal amount is valid.
  • The first if condition is amount > 0 && amount <= balance.
  • Here, the logical AND (&&) operator checks two conditions:
  • The withdrawal amount must be greater than zero.
  • The withdrawal amount must be less than or equal to the available account balance.
  • If both conditions are true, the program deducts the withdrawal amount from the current balance using the -= compound assignment operator.
  • The statement balance -= amount; is equivalent to balance = balance - amount;.
  • If the entered amount is zero or negative, the program displays an invalid withdrawal amount message.
  • If the withdrawal amount is greater than the available balance, the program displays an insufficient balance message.

Step 10: Exit the Banking Application (case 4)

  • When the user enters 4, the program executes case 4 and displays a thank-you message.
  • The break statement terminates the switch statement.

Step 11: Handle Invalid Menu Choices (default)

  • The default block executes when the user enters a menu option that does not match any of the specified cases (1, 2, 3, or 4).
  • For example, if the user enters 5, the program displays a message: “Invalid choice! Please try again”.

After displaying the error message, the while loop repeats, and the banking menu appears again.

Step 12: Close the Scanner Object

  • Finally, after the while loop terminates, we close the Scanner object using the close() method. This releases the resources associated with the Scanner object.

How Does the Program Work?

The overall execution flow of this Java banking menu program is as follows:

  • The program starts by initializing the account balance to Rs. 1000.00.
  • The while loop displays the banking menu to enter a menu option.
  • The switch statement executes the corresponding banking operation based on the enter menu option.
  • The program updates or displays the account balance as required.
  • The while loop displays the menu again if the user has not selected option 4.
  • When the user selects option 4, the loop terminates, and the program ends.

Java Banking Menu Program Using a Modern Switch Statement

The modern switch statement replaces traditional case labels and break statements with cleaner arrow syntax (->). It prevents accidental fall-through bugs and significantly improves code readability.

Here is the complete banking menu program rewritten using a modern switch statement.

Java Program

import java.util.Scanner;
public class ModernBankingMenu {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double balance = 1000.00; // Initial account balance
        int choice = 0;           // Variable to store user's menu choice

        // While loop keeps the menu active until the user chooses to exit (Option 4).
        while (choice != 4) {

            // Displaying the interactive banking menu.
            System.out.println("\n===== MODERN BANKING MENU =====");
            System.out.println("1. Check Balance");
            System.out.println("2. Deposit Money");
            System.out.println("3. Withdraw Money");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");

            // Read the user's integer choice.
            choice = sc.nextInt();

            // Modern switch statement (Java 14+) using arrow syntax (->)
            // Notice: No 'break' statements are needed, eliminating fall-through bugs.
            switch (choice) {

                case 1 -> {
                    // Option 1: Display current balance
                    System.out.println("Current Balance: Rs. " + balance);
                }

                case 2 -> {
                    // Option 2: Handle deposits
                    System.out.print("Enter deposit amount: Rs. ");
                    double amount = sc.nextDouble();

                    if (amount > 0) {
                        balance += amount;
                        System.out.println("Amount deposited successfully.");
                        System.out.println("Updated Balance: Rs. " + balance);
                    } else {
                        System.out.println("Invalid deposit amount.");
                    }
                }

                case 3 -> {
                    // Option 3: Handle withdrawals
                    System.out.print("Enter withdrawal amount: Rs. ");
                    double amount = sc.nextDouble();

                    if (amount > 0 && amount <= balance) {
                        balance -= amount;
                        System.out.println("Please collect your money.");
                        System.out.println("Updated Balance: Rs. " + balance);
                    } else if (amount <= 0) { 
                        System.out.println("Invalid withdrawal amount."); 
                    } else { 
                        System.out.println("Insufficient balance."); 
                    } 
                } 
           
                case 4 -> {
                    // Option 4: Exit the program loop
                    System.out.println("Thank you for using our banking service!");
                }

                default -> {
                    // Handles invalid selections outside 1-4
                    System.out.println("Invalid choice! Please try again.");
                }
            }
        }

        // Close the scanner to prevent memory leaks
        sc.close();
    }
}

Output:

===== MODERN BANKING MENU =====
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Enter your choice: 1
Current Balance: Rs. 1000.0

===== MODERN BANKING MENU =====
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Enter your choice: 2
Enter deposit amount: Rs. 2500
Amount deposited successfully.
Updated Balance: Rs. 3500.0

===== MODERN BANKING MENU =====
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Enter your choice: 3
Enter withdrawal amount: Rs. 1200
Please collect your money.
Updated Balance: Rs. 2300.0

===== MODERN BANKING MENU =====
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Enter your choice: 4
Thank you for using our banking service!

 

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.