You are on page 1of 2

Q) Create an UserAccount class with 3 methods like deposit(),withraw() and

getBalance(). so when user want to withraw the amount then if check if sufficient
balance is not available then throw custom exception for Insufficient Balance.

package assignments;

import java.util.Scanner;

public class Accounts{


Scanner sc=new Scanner(System.in);

static int balance;


static int deposite;
static int withdraw;
public static void main(String[] args) {
deposite();
getBalance();
withdraw();
}

static void deposite() {


Scanner sc=new Scanner(System.in);
deposite=5000;
System.out.println("Deposite Amount: "+deposite);
}
static void getBalance() {

balance=4000+deposite;
System.out.println("Balance: "+balance);
}
static void withdraw() {
Scanner sc=new Scanner(System.in);
System.out.print("Enter withdraw amount: ");
withdraw=sc.nextInt();
//withdraw=2000;
if(withdraw>balance) {
try {
throw new InSufficientException();
}
catch(InSufficientException i) {
System.out.println(i);
}
}
else {
balance=balance-withdraw;
System.out.println("\nAmount Deducted");
System.out.println("balance: "+balance);
}
}

}
class InSufficientException extends Exception{
private String msg;
public InSufficientException() {
msg="\nInSufficient Balance Exception";
}
@Override
public String toString() {
return msg;
}
}

You might also like