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
maincreates a Scannerscand reads the name intonm, the account number intonoand the opening balance intobal.- If
bal < 0,mainprints the invalid input message and stops. - Otherwise
maincreatesobj = new Account(nm, no, bal). The constructor copies the three values into the data membersname,accNoandbalance, usingthisbecause the parameters have the same names. mainreads the deposit amountdand callsobj.deposit(d):- if
amt > 0,balanceis increased byamtand the method returnstrue; - otherwise it returns
false.
mainprints the new balance if the result istrue, or the deposit rejected message if it isfalse.- if
mainreads the withdrawal amountwand callsobj.withdraw(w):- if
amt > 0andamt <= balance,balanceis decreased byamtand the method returnstrue; - otherwise it returns
falseand the balance is unchanged.
mainprints the new balance or the withdrawal rejected message in the same way.- if
maincallsobj.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
| Variable | Data Type | Purpose |
|---|---|---|
name | String | Data member holding the name of the account holder; also the first parameter of the constructor |
accNo | long | Data member holding the account number; also the second parameter of the constructor |
balance | double | Data member holding the current balance; also the third parameter of the constructor |
amt | double | Parameter of deposit() and of withdraw(): the amount to be deposited or withdrawn |
sc | Scanner | Reads the input typed by the user |
nm | String | Name of the holder, read in main and passed to the constructor |
no | long | Account number, read in main and passed to the constructor |
bal | double | Opening balance, read in main and checked to be not negative |
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 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).
| Step | Call | Condition | Returns | balance after | Printed by main |
|---|---|---|---|---|---|
| 1 | deposit(-50) | $-50 > 0$ is false | false | 800.0 | Deposit rejected. Amount must be greater than 0. |
| 2 | withdraw(900) | $900 > 0$ is true, but $900 \le 800$ is false | false | 800.0 | Withdrawal rejected. Amount must be greater than 0 and not more than the balance. |
| 3 | display() | none | no value | 800.0 | the name, the account number 3003 and Balance: 800.00 |
The last line printed is Balance: 800.00, as in the third 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."