Write a Java program to create a class named "Bank." In the "Bank" class, take three instance variables
acno, name, and balance. Create a parameterized constructor to initialize instance variables. Now create the
following methods in the "Bank" class:
i. deposit() - This method is used to deposit money in the account.
ii. withdraw() - This method is used to withdraw money from the account after checking the balance.
iii. enquiry() - This method provides balance enquiry.
import java.io.*;
class Bank {
int accountno;
String name;
double balance;
Bank(int accountno, String name, double balance) {
this.accountno = accountno;
this.name = name;
this.balance = balance;
}
public void deposit(int acno, double amt) {
if (accountno == acno) {
balance += amt;
System.out.println("Amount : " + amt + " Rs. is credited in A/c : " + acno);
} else {
System.out.println("Account does not exist!!");
}
}
public void withdraw(int acno, double amt) {
if (accountno == acno) {
if (amt <= balance) {
balance -= amt;
System.out.println("Amount : " + amt + " Rs. is debited from A/c : " + acno);
} else {
System.out.println("Enter less amount. Your balance is not sufficient!!");
}
} else {
System.out.println("Account does not exist!!");
}
}
public void enquiry(int acno) {
if (accountno == acno) {
System.out.println("Your Current Balance is : " + balance);
} else {
System.out.println("Account does not exist!!");
}
}
}
class T18 {
public static void main(String[] args) throws IOException {
nt acno, ch = 0;
double bal, amt;
String name;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Account Number : ");
acno = Integer.parseInt(br.readLine());
System.out.print("Enter Account Holder Name : ");
name = br.readLine();
System.out.print("Enter Initial Balance : ");
bal = Double.parseDouble(br.readLine());
Bank u1 = new Bank(acno, name, bal);
System.out.println("Congrats!! Your Account is created.");
while (ch != 4) {
System.out.println("Enter 1 for Deposit.");
System.out.println("Enter 2 for Withdraw.");
System.out.println("Enter 3 for Enquiry.");
System.out.println("Enter 4 for Exit.");
System.out.print("Enter your choice : ");
ch = Integer.parseInt(br.readLine());
switch (ch) {
case 1:
System.out.print("Enter Account No. : ");
acno = Integer.parseInt(br.readLine());
System.out.print("Enter Amount to deposit : ");
amt = Double.parseDouble(br.readLine());
u1.deposit(acno, amt);
break;
case 2:
System.out.print("Enter Account No. : ");
acno = Integer.parseInt(br.readLine());
System.out.print("Enter Amount to Withdraw : ");
amt = Double.parseDouble(br.readLine());
u1.withdraw(acno, amt);
break;
case 3:
System.out.print("Enter Account No. : ");
acno = Integer.parseInt(br.readLine());
u1.enquiry(acno);
break;
case 4:
break;
default:
System.out.println("Enter Valid Choice!!");
break;
}
}
}
}
Copy Code