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

ISC Practicals and Theory - Bank Account Class (OOP)

Updated 21 Oct 2026

A bank account can be modelled as a class Account with the data members name, account number and balance. In this version the account is created with a parameterised constructor Account(String name, long accNo, double balance), and deposit() and withdraw() **return true or false** to say whether the operation was done, so that main decides which message to print.

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 only if it is not more than the balance. Otherwise it is refused. There is no overdraft and no minimum balance.

For example, with a balance of 800, a withdrawal of 900 is refused, and a withdrawal of exactly 800 is allowed and leaves 0.

Algorithm

  1. main creates a Scanner sc and reads the name into nm, the account number into no and the opening balance into bal.
  2. If bal < 0, main prints the invalid input message and stops.
  3. Otherwise main creates obj = new Account(nm, no, bal). The constructor copies the three values into the data members name, accNo and balance, using this because the parameters have the same names.
  4. main reads the deposit amount d and calls obj.deposit(d):
    • if amt > 0, balance is increased by amt and the method returns true;
    • otherwise it returns false.
    main prints the new balance if the result is true, or the deposit rejected message if it is false.
  5. main reads the withdrawal amount w and calls obj.withdraw(w):
    • if amt > 0 and amt <= balance, balance is decreased by amt and the method returns true;
    • otherwise it returns false and the balance is unchanged.
    main prints the new balance or the withdrawal rejected message in the same way.
  6. main calls obj.display() to print 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 deposit() and withdraw() only change the balance and return the result; all the input and the messages are in main.

import java.util.Scanner;

/* Class Account: a simple bank account with deposit and withdrawal.
   deposit() and withdraw() return true or false, and main prints the message. */
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

    /* Parameterised constructor: stores the account details.
       Parameters: name, accNo and balance are the values for the data members. */
    Account(String name, long accNo, double balance)
    {
        this.name = name;       // this.name is the data member, name is the parameter
        this.accNo = accNo;
        this.balance = balance;
    }

    /* Adds amt to the balance if amt > 0.
       Parameter amt: the amount to be deposited.
       Returns true if the deposit was made, false if it was refused. */
    boolean deposit(double amt)
    {
        if (amt > 0)
        {
            balance = balance + amt;    // add the amount
            return true;
        }
        return false;
    }

    /* Subtracts amt from the balance if amt > 0 and amt <= balance (no overdraft).
       Parameter amt: the amount to be withdrawn.
       Returns true if the withdrawal was made, false if it was refused. */
    boolean withdraw(double amt)
    {
        if (amt > 0 && amt <= balance)
        {
            balance = balance - amt;    // subtract the amount
            return true;
        }
        return false;
    }

    /* 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: reads the details, creates an object of Account, reads one deposit
       and one withdrawal amount and prints a message for each operation. */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);    // reads the input
        System.out.print("Enter account holder name: ");
        String nm = sc.nextLine();              // name of the holder
        System.out.print("Enter account number: ");
        long no = sc.nextLong();                // account number
        System.out.print("Enter opening balance: ");
        double bal = sc.nextDouble();           // opening balance
        if (bal < 0)
        {
            System.out.println("Invalid input. Opening balance cannot be negative.");
        }
        else
        {
            Account obj = new Account(nm, no, bal);     // object of the class
            System.out.print("Enter amount to deposit: ");
            double d = sc.nextDouble();         // amount to deposit
            if (obj.deposit(d))
                System.out.printf("Deposit successful. Balance: %.2f%n", obj.balance);
            else
                System.out.println("Deposit rejected. Amount must be greater than 0.");
            System.out.print("Enter amount to withdraw: ");
            double w = sc.nextDouble();         // amount to withdraw
            if (obj.withdraw(w))
                System.out.printf("Withdrawal successful. Balance: %.2f%n", obj.balance);
            else
                System.out.println("Withdrawal rejected. Amount must be greater than 0 and not more than the balance.");
            obj.display();
        }
    }
}

Variable description table

VariableData TypePurpose
nameStringData member holding the name of the account holder; also the first parameter of the constructor
accNolongData member holding the account number; also the second parameter of the constructor
balancedoubleData member holding the current balance; also the third parameter of the constructor
amtdoubleParameter of deposit() and of withdraw(): the amount to be deposited or withdrawn
scScannerReads the input typed by the user
nmStringName of the holder, read in main and passed to the constructor
nolongAccount number, read in main and passed to the constructor
baldoubleOpening balance, read in main and checked to be not negative
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 third sample: name Kabir Rao, account number 3003, opening balance 800, deposit $-50$ and withdrawal 900. bal = 800 is not negative, so obj = new Account("Kabir Rao", 3003, 800).

StepCallConditionReturnsbalance afterPrinted by main
1deposit(-50)$-50 > 0$ is falsefalse800.0Deposit rejected. Amount must be greater than 0.
2withdraw(900)$900 > 0$ is true, but $900 \le 800$ is falsefalse800.0Withdrawal rejected. Amount must be greater than 0 and not more than the balance.
3display()noneno value800.0the name, the account number 3003 and Balance: 800.00

The last line printed is Balance: 800.00, as in the third 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."