Java
Java

 

Simulate a basic bank account with deposit, withdrawal, and balance check features using Java programming language.

Introduction

The Bank Account Simulator is a simple Java program that allows users to interact with a virtual bank account. This program enables users to deposit money, withdraw money, and check the balance of their account. It is an excellent way to learn the basics of object-oriented programming, classes, methods, and user input in Java.

Objective

The objective of this project is to simulate the functionality of a bank account. The program will allow the user to perform the following operations:

  • Deposit money into the account
  • Withdraw money from the account
  • Check the current balance

Java Code for Bank Account Simulator


        import java.util.Scanner;

        // BankAccount class
        class BankAccount {
            private double balance;

            // Constructor
            public BankAccount(double initialBalance) {
                if (initialBalance > 0) {
                    balance = initialBalance;
                } else {
                    balance = 0;
                }
            }

            // Deposit method
            public void deposit(double amount) {
                if (amount > 0) {
                    balance += amount;
                    System.out.println("Deposited: $" + amount);
                } else {
                    System.out.println("Deposit amount must be positive.");
                }
            }

            // Withdraw method
            public void withdraw(double amount) {
                if (amount > 0 && amount <= balance) { balance -= amount; System.out.println("Withdrew: $" + amount); } else if (amount > balance) {
                    System.out.println("Insufficient balance.");
                } else {
                    System.out.println("Withdrawal amount must be positive.");
                }
            }

            // Check balance method
            public void checkBalance() {
                System.out.println("Current Balance: $" + balance);
            }
        }

        // Main class to run the program
        public class BankAccountSimulator {
            public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
                System.out.println("Enter initial balance for your account: ");
                double initialBalance = scanner.nextDouble();

                // Create BankAccount object
                BankAccount account = new BankAccount(initialBalance);

                while (true) {
                    System.out.println("\nChoose an operation:");
                    System.out.println("1. Deposit");
                    System.out.println("2. Withdraw");
                    System.out.println("3. Check Balance");
                    System.out.println("4. Exit");

                    int choice = scanner.nextInt();

                    switch (choice) {
                        case 1:
                            System.out.print("Enter amount to deposit: ");
                            double depositAmount = scanner.nextDouble();
                            account.deposit(depositAmount);
                            break;
                        case 2:
                            System.out.print("Enter amount to withdraw: ");
                            double withdrawAmount = scanner.nextDouble();
                            account.withdraw(withdrawAmount);
                            break;
                        case 3:
                            account.checkBalance();
                            break;
                        case 4:
                            System.out.println("Exiting program...");
                            scanner.close();
                            return;
                        default:
                            System.out.println("Invalid choice! Please try again.");
                    }
                }
            }
        }
        

Explanation of the Program

This Java program simulates a simple bank account with features for depositing, withdrawing, and checking the balance. Here’s a breakdown of how the program works:

  1. BankAccount Class: This class contains methods for performing operations such as deposit, withdrawal, and checking the balance. It also has a constructor that initializes the balance with a starting value.
  2. Deposit Method: Allows the user to deposit a specified amount into the account. The deposit must be a positive number.
  3. Withdraw Method: Allows the user to withdraw a specified amount from the account, provided they have enough balance.
  4. Check Balance Method: Displays the current balance of the account.
  5. Main Method: The program runs in an infinite loop, allowing the user to select options like deposit, withdraw, check balance, or exit. It uses a scanner to get input from the user.

How to Run the Program

To run the Bank Account Simulator program:

  1. Ensure that you have Java installed on your system.
  2. Create a new file named BankAccountSimulator.java and copy the Java code into this file.
  3. Compile the program using the command javac BankAccountSimulator.java.
  4. Run the compiled program using java BankAccountSimulator.
  5. Follow the on-screen prompts to deposit, withdraw, and check your account balance.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)