-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexp10.java
More file actions
218 lines (194 loc) · 4.99 KB
/
exp10.java
File metadata and controls
218 lines (194 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//mini-application for a banking system in Java. In this program, we will add some basic functionalities of a bank account like a deposit of amount, withdrawal of amount, etc.Initially, the program accepts the number of customers (Array of Objects)we need to add and adds the customer and account details accordingly. Further, it displays the series of menus to operate over the accounts.
//1.Display all account details
//2.Search by account number
//3.Deposit the amount
//4.Withdraw the amount
//5.Exit
//Create Custom Exception and perform the following action:
//Customers are not allowed to deposit amount <= 0 ( In this case throw InvalidAmountException).
//Customers are not allowed to withdraw amount <= 0 (throw InvalidAmountException).
//Customers are also not allowed to withdraw an amount greater than (>) the available amount (throw InsufficientFundsException)
import java.util.*;
import java.util.Arrays;
class DepositException extends Exception {
String message;
public DepositException(String msg)
{
message=msg;
}
public String getMessage()
{
return message;
}
}
class WithdrawException extends Exception{
String message;
public WithdrawException(String msg)
{
message=msg;
}
public String getMessage()
{
return message;
}
}
// WithdrawException and DepositException are defined as its own file(class) in the same package
class Bank{
private String acc_holder,acc_number,acc_type;
private long acc_balance;
Scanner sc=new Scanner(System.in);
//To Open Account
void OpenAccount()
{
System.out.println("Name of Account holder: ");
acc_holder=sc.next();
System.out.println("Type of account : ");
acc_type=sc.next();
System.out.println("Account Number : ");
acc_number=sc.next();
System.out.println("Account Balance : ");
acc_balance=sc.nextLong();
}
//Show Account
void ShowAccount()
{
System.out.println("Name of Account holder: "+acc_holder);
System.out.println("Type of account : "+acc_type);
System.out.println("Account Number : "+acc_number);
System.out.println("Account Balance : "+acc_balance);
}
//Search Account
public boolean SearchAcc(String acc_no)
{
if(acc_number.equals(acc_no))
{
return (true);
}
else
return (false);
}
//To Deposit Funds
void Deposit()
{
long a_deposit;
System.out.println("Enter the amount to be deposited :");
a_deposit=sc.nextLong();
try {
if(a_deposit<=0)
{
throw new DepositException("Enter valid amount");
}
}
catch(DepositException e)
{
System.out.println(e.getMessage());
}
acc_balance+=a_deposit;
System.out.println("Current balance: "+acc_balance);
}
//To Withdraw Funds
void Withdraw()
{
long a_withdraw;
System.out.println("Enter the amount to be withdrawn :");
a_withdraw=sc.nextLong();
try {
if(a_withdraw<=0)
{
throw new WithdrawException("Enter valid amount");
}
}
catch(WithdrawException e)
{
System.out.println(e.getMessage());
}
if(a_withdraw>acc_balance)
{
System.out.println("Insufficient funds!!");
}
else
{
acc_balance-=a_withdraw;
System.out.println("Current balnce: "+acc_balance);
}
}
}
//MAIN METHOD
public class exp10 {
public static void main(String[] args) {
Bank B=new Bank();
Scanner sc=new Scanner(System.in);
int choice;
boolean customer=false;
B.OpenAccount();
System.out.println("1.Display all account details");
System.out.println("2.Search by account ");
System.out.println("3.Deposit");
System.out.println("4.Withdraw");
System.out.println("5.Exit");
do
{
System.out.println("Please enter your choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
{
B.ShowAccount();
break;
}
case 2:
{
{
System.out.println("Enter Account Number: ");
String acc_no=sc.next();
if(B.SearchAcc(acc_no))
{
B.ShowAccount();
}
else
{
System.out.println("ACCOUNT NOT FOUND!!");
}
}
break;
}
case 3:
{
{
System.out.println("Enter Account Number to deposit funds: ");
String acc_no=sc.next();
if(B.SearchAcc(acc_no))
{
B.Deposit();
}
else
{
System.out.println("ACCOUNT NOT FOUND!!");
}
}
break;
}
case 4:
{
{
System.out.println("Enter Account Number to withdraw funds: ");
String acc_no=sc.next();
if(B.SearchAcc(acc_no))
{
B.Withdraw();
}
}
break;
}
case 5:
{
System.out.println("--THANK YOU--");
break;
}
default :
System.out.println("Couldnt find entered choice");
}
}while(choice!=5);
}
}