Here I have created Bank, account classes and an exception class of InsufficientFundsException
public class Bank {
Account acct;
String name;
Bank(String name){
this.name = name;
}
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
Bank bank = new Bank("SBI");
bank.acct=new Account(656565656);
System.out.println("depositing 500");
bank.acct.deposit(500);
System.out.println("withdrawing 300");
try{
bank.acct.withdraw(300);
}
catch(InsufficientFundException e){
//e.printStackTrace();
System.out.println(e.getMessage());
}
//System.out.println(bank.acct.bal);
System.out.println("again withdrawing 300");
}
public void makeWithdraw(int amt){
try{
acct.withdraw(amt);
}
catch(InsufficientFundException e){
System.out.println(e.getMessage());
}
}
}
class Account{
long acctNo;
int bal;
Account(long an){
acctNo = an;
}
public void deposit(int amt){
bal +=amt;
}
public void withdraw(int amt) throws InsufficientFundException {
if(amt<=bal){
bal -=amt;
}
else{
throw new InsufficientFundException("Not enough balance, balance is "+bal);
}
}
}
public class InsufficientFundException extends Exception {
public InsufficientFundException(String str){
super(str);
}
}
Bank.java
public class Bank {
Account acct;
String name;
Bank(String name){
this.name = name;
}
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
Bank bank = new Bank("SBI");
bank.acct=new Account(656565656);
System.out.println("depositing 500");
bank.acct.deposit(500);
System.out.println("withdrawing 300");
try{
bank.acct.withdraw(300);
}
catch(InsufficientFundException e){
//e.printStackTrace();
System.out.println(e.getMessage());
}
//System.out.println(bank.acct.bal);
System.out.println("again withdrawing 300");
}
public void makeWithdraw(int amt){
try{
acct.withdraw(amt);
}
catch(InsufficientFundException e){
System.out.println(e.getMessage());
}
}
}
class Account{
long acctNo;
int bal;
Account(long an){
acctNo = an;
}
public void deposit(int amt){
bal +=amt;
}
public void withdraw(int amt) throws InsufficientFundException {
if(amt<=bal){
bal -=amt;
}
else{
throw new InsufficientFundException("Not enough balance, balance is "+bal);
}
}
}
InsufficientFundException
public class InsufficientFundException extends Exception {
public InsufficientFundException(String str){
super(str);
}
}
No comments:
Post a Comment