Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Bank Account Class

Updated 21 Oct 2026

A bank account can be modelled as a class Account whose data members are the holder's name, the account number and the balance. Its methods deposit money, withdraw money and display the details. This note shows the program in which the default constructor creates an empty account, accept() reads its details, and deposit() and withdraw() print the new balance or an error line.

Definition

An account holds a balance that changes only through two operations:

  • Deposit: a positive amount is added to the balance. An amount of 0 or less is refused.
  • Withdrawal: a positive amount is subtracted from the balance, but only if it is not more than the balance. An amount of 0 or less, or more than the balance, is refused. There is no overdraft and no minimum balance.

For example, with a balance of 800: a deposit of $-50$ is refused, and a withdrawal of 900 is refused as $900 > 800$. A withdrawal of exactly 800 is allowed and leaves a balance of 0.

Algorithm

  1. main creates a Scanner sc and an object obj of class Account. The default constructor sets name to an empty string, and accNo and balance to 0.
  2. main calls obj.accept(sc). It reads name, accNo and the opening balance, and returns true if balance >= 0, otherwise false.
  3. If accept() returned false, main prints the invalid input message and stops.
  4. Otherwise main reads the deposit amount into d and calls obj.deposit(d):
    • if amt > 0, add amt to balance and print the new balance;
    • otherwise print the deposit rejected message.
  5. main reads the withdrawal amount into w and calls obj.withdraw(w):
    • if amt > 0 and amt <= balance, subtract amt from balance and print the new balance;
    • otherwise print the withdrawal rejected message.
  6. main calls obj.display(), which prints the name, account number and balance.

The balance is printed to 2 decimal places with System.out.printf("%.2f%n", ...).

Program

The data members are name, accNo and balance. The methods are accept(), deposit(), withdraw() and display(); main only creates the object, reads the two amounts and calls the methods.

import java.util.Scanner;

/* Class Account: a simple bank account with deposit and withdrawal. */
public class Account
{
    String name;                // data member: name of the account holder
    long accNo;                 // data member: account number
    double balance;             // data member: current balance in rupees

    /* Default constructor: sets the data members to empty values. */
    Account()
    {
        name = "";
        accNo = 0;
        balance = 0.0;
    }

    /* Reads the name, account number and opening balance using the given Scanner.
       Returns true if the opening balance is not negative, otherwise false. */
    boolean accept(Scanner sc)
    {
        System.out.print("Enter account holder name: ");
        name = sc.nextLine();
        System.out.print("Enter account number: ");
        accNo = sc.nextLong();
        System.out.print("Enter opening balance: ");
        balance = sc.nextDouble();
        return balance >= 0;
    }

    /* Adds amt to the balance if amt > 0, otherwise prints an error line.
       Parameter amt: the amount to be deposited. */
    void deposit(double amt)
    {
        if (amt > 0)
        {
            balance = balance + amt;    // add the amount
            System.out.printf("Deposit successful. Balance: %.2f%n", balance);
        }
        else
        {
            System.out.println("Deposit rejected. Amount must be greater than 0.");
        }
    }

    /* Subtracts amt from the balance if amt > 0 and amt <= balance
       (no overdraft), otherwise prints an error line.
       Parameter amt: the amount to be withdrawn. */
    void withdraw(double amt)
    {
        if (amt > 0 && amt <= balance)
        {
            balance = balance - amt;    // subtract the amount
            System.out.printf("Withdrawal successful. Balance: %.2f%n", balance);
        }
        else
        {
            System.out.println("Withdrawal rejected. Amount must be greater than 0 and not more than the balance.");
        }
    }

    /* Prints the name, account number and balance (2 decimal places). */
    void display()
    {
        System.out.println("Account holder: " + name);
        System.out.println("Account number: " + accNo);
        System.out.printf("Balance: %.2f%n", balance);
    }

    /* main: creates an object of Account, reads one deposit and one withdrawal
       amount and calls the methods. */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);    // reads the input
        Account obj = new Account();            // object of the class
        if (obj.accept(sc))
        {
            System.out.print("Enter amount to deposit: ");
            double d = sc.nextDouble();         // amount to deposit
            obj.deposit(d);
            System.out.print("Enter amount to withdraw: ");
            double w = sc.nextDouble();         // amount to withdraw
            obj.withdraw(w);
            obj.display();
        }
        else
        {
            System.out.println("Invalid input. Opening balance cannot be negative.");
        }
    }
}

Variable description table

VariableData TypePurpose
nameStringData member holding the name of the account holder
accNolongData member holding the account number
balancedoubleData member holding the current balance
scScannerReads the input; created in main and passed to accept()
amtdoubleParameter of deposit() and of withdraw(): the amount to be deposited or withdrawn
objAccountObject of the class, created in main
ddoubleAmount to deposit, read in main
wdoubleAmount to withdraw, read in main
argsString[]Command line arguments of main; not used

Sample input and output

Sample 1

Enter account holder name: Aviral Sharma
Enter account number: 1001
Enter opening balance: 5000
Enter amount to deposit: 2500.50
Deposit successful. Balance: 7500.50
Enter amount to withdraw: 1500.25
Withdrawal successful. Balance: 6000.25
Account holder: Aviral Sharma
Account number: 1001
Balance: 6000.25

Sample 2

Enter account holder name: Riya Das
Enter account number: 2002
Enter opening balance: 1000
Enter amount to deposit: 0
Deposit rejected. Amount must be greater than 0.
Enter amount to withdraw: 1000
Withdrawal successful. Balance: 0.00
Account holder: Riya Das
Account number: 2002
Balance: 0.00

Sample 3

Enter account holder name: Kabir Rao
Enter account number: 3003
Enter opening balance: 800
Enter amount to deposit: -50
Deposit rejected. Amount must be greater than 0.
Enter amount to withdraw: 900
Withdrawal rejected. Amount must be greater than 0 and not more than the balance.
Account holder: Kabir Rao
Account number: 3003
Balance: 800.00

Sample 4

Enter account holder name: Meera Nair
Enter account number: 4004
Enter opening balance: -200
Invalid input. Opening balance cannot be negative.

One value per line

Dry run

Take the first sample: name Aviral Sharma, account number 1001, opening balance 5000, deposit 2500.50 and withdrawal 1500.25.

StepCallConditionbalance afterPrinted
1accept(sc)$5000 \ge 0$ is true5000.0nothing, returns true
2deposit(2500.50)$2500.50 > 0$ is true7500.50Deposit successful. Balance: 7500.50
3withdraw(1500.25)$1500.25 > 0$ and $1500.25 \le 7500.50$ is true6000.25Withdrawal successful. Balance: 6000.25
4display()none6000.25the name, the account number 1001 and Balance: 6000.25

The last line printed is Balance: 6000.25, as in the first sample run.

Variations

  1. Menu-driven account "Write a menu-driven program for the class Account with the options deposit, withdraw, display balance and exit."
  2. Minimum balance "Modify withdraw() so that a withdrawal is refused if the balance would fall below a minimum balance."
  3. Transfer "Write a method that transfers an amount from one Account object to another."