1// Aim: Program to perform Exception Handling operations (InterfaceTask).
2interface Account {
3 void deposit(double amt);
4 void withdraw(double amt);
5 double viewBalance();
6}
7
8class InterfaceTask implements Account{
9
10 private double bal;
11 private double intrate;
12
13 public InterfaceTask(double bal,double intrate){
14 this.bal=bal;
15 this.intrate=intrate;
16 }
17
18 public void deposit(double amt){
19 if(amt>0){
20 bal+=amt;
21 System.out.println("deposited :"+amt);
22 System.out.println("new balance :"+bal);
23 }
24 else{
25 System.out.println("invalid amount");
26 }
27 }
28
29 public void withdraw(double amt){
30 if(amt>0 && bal>=amt){
31 bal-=amt;
32 System.out.println("withdrawn :"+amt);
33 System.out.println("new balance :"+bal);
34 }
35 else{
36 System.out.println("invalid amount");
37 }
38 }
39
40 public double viewBalance(){
41 return bal;
42 }
43
44 public void addInterest(){
45 bal+=bal*intrate/100;
46 }
47}
48
49class CurrentAccount implements Account{
50
51 private double bal;
52 private double overdraft;
53
54 public CurrentAccount(double bal,double overdraft){
55 this.bal=bal;
56 this.overdraft=overdraft;
57 }
58
59 public void deposit(double amt){
60 if(amt>0){
61 bal+=amt;
62 System.out.println("deposited :"+amt);
63 System.out.println("new balance :"+bal);
64 }
65 else{
66 System.out.println("invalid amount");
67 }
68 }
69
70 public void withdraw(double amt){
71 if(amt>0 && bal+overdraft>=amt){
72 bal-=amt;
73 System.out.println("withdrawn :"+amt);
74 System.out.println("new balance :"+bal);
75 }
76 else{
77 System.out.println("invalid amount");
78 }
79 }
80
81 public double viewBalance(){
82 return bal;
83 }
84
85 public void overdraft(){
86 System.out.println("overdraft :"+overdraft);
87 }
88}
89
90public class Interface{
91 public static void main(String[] args){
92 InterfaceTask sa=new InterfaceTask(1000,5);
93 CurrentAccount ca=new CurrentAccount(1000,500);
94
95 sa.deposit(1000);
96 sa.withdraw(500);
97 sa.addInterest();
98 System.out.println("saving account balance :"+sa.viewBalance());
99
100 ca.deposit(1000);
101 ca.withdraw(2500);
102 ca.overdraft();
103 System.out.println("current account balance :"+ca.viewBalance());
104 }
105}