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
maincreates a Scannerscand an objectobjof classAccount. The default constructor setsnameto an empty string, andaccNoandbalanceto 0.maincallsobj.accept(sc). It readsname,accNoand the openingbalance, and returnstrueifbalance >= 0, otherwisefalse.- If
accept()returnedfalse,mainprints the invalid input message and stops. - Otherwise
mainreads the deposit amount intodand callsobj.deposit(d):- if
amt > 0, addamttobalanceand print the new balance; - otherwise print the deposit rejected message.
- if
mainreads the withdrawal amount intowand callsobj.withdraw(w):- if
amt > 0andamt <= balance, subtractamtfrombalanceand print the new balance; - otherwise print the withdrawal rejected message.
- if
maincallsobj.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
| Variable | Data Type | Purpose |
|---|---|---|
name | String | Data member holding the name of the account holder |
accNo | long | Data member holding the account number |
balance | double | Data member holding the current balance |
sc | Scanner | Reads the input; created in main and passed to accept() |
amt | double | Parameter of deposit() and of withdraw(): the amount to be deposited or withdrawn |
obj | Account | Object of the class, created in main |
d | double | Amount to deposit, read in main |
w | double | Amount to withdraw, read in main |
args | String[] | 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.25Sample 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.00Sample 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.00Sample 4
Enter account holder name: Meera Nair
Enter account number: 4004
Enter opening balance: -200
Invalid input. Opening balance cannot be negative.Dry run
Take the first sample: name Aviral Sharma, account number 1001, opening balance 5000, deposit 2500.50 and withdrawal 1500.25.
| Step | Call | Condition | balance after | Printed |
|---|---|---|---|---|
| 1 | accept(sc) | $5000 \ge 0$ is true | 5000.0 | nothing, returns true |
| 2 | deposit(2500.50) | $2500.50 > 0$ is true | 7500.50 | Deposit successful. Balance: 7500.50 |
| 3 | withdraw(1500.25) | $1500.25 > 0$ and $1500.25 \le 7500.50$ is true | 6000.25 | Withdrawal successful. Balance: 6000.25 |
| 4 | display() | none | 6000.25 | the name, the account number 1001 and Balance: 6000.25 |
The last line printed is Balance: 6000.25, as in the first sample run.
Variations
- Menu-driven account "Write a menu-driven program for the class Account with the options deposit, withdraw, display balance and exit."
- Minimum balance "Modify withdraw() so that a withdrawal is refused if the balance would fall below a minimum balance."
- Transfer "Write a method that transfers an amount from one Account object to another."