-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankingApplication.java
193 lines (191 loc) · 6.53 KB
/
BankingApplication.java
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
package com.company;
import java.util.Scanner;
class BankDetails {
private String AccNo;
private String name;
private long balance;
Scanner sc = new Scanner(System.in);
public void openAccount() {
System.out.print("Enter Account No: ");
try {
AccNo = sc.next();
}
catch(Exception e)
{
System.out.println("Please enter a valid input!!");
}
System.out.print("Enter Name: ");
name = sc.next();
System.out.print("Enter Balance: ");
int flag =1;
while(flag!=0) {
try {
balance = sc.nextLong();
if (balance >= 1000) {
flag =0;
} else {
throw new MinimumBalanceException();
}
} catch (MinimumBalanceException m) {
System.out.println("Please enter a bigger amount for initial opening of account!!");
}
}
}
// display account details
public void showAccount() {
System.out.println("Name of account holder: " + name);
System.out.println("Account no.: " + AccNo);
System.out.println("Balance: " + balance);
}
//deposit money
public void deposit() {
long amt =0;
System.out.println("Enter the amount you want to deposit: ");
try {
amt = sc.nextLong();
balance = balance + amt;
}
catch(Exception e)
{
System.out.println("Please enter a valid input!!");
}
}
// withdraw money
public void withdrawal() {
long amt;
System.out.println("Enter the amount you want to withdraw: ");
try {
amt = sc.nextLong();
if (balance >= amt) {
balance = balance - amt;
System.out.println("Balance after withdrawal: " + balance);
}
else
{
throw new InsufficientBalanceException();
}
}
catch(InsufficientBalanceException e)
{
System.out.println("You have insufficient balance!!!");
}
}
//search an account number
public boolean search(String ac_no) {
if (AccNo.equals(ac_no)) {
showAccount();
return (true);
}
return (false);
}
}
public class BankingApplication{
public static void main(String arg[]) {
Scanner sc = new Scanner(System.in);
//create initial accounts
System.out.print("How many pre- added accounts do you want to have ? ");
int n = sc.nextInt();
BankDetails C[] = new BankDetails[n];
for (int i = 0; i < C.length; i++) {
C[i] = new BankDetails();
C[i].openAccount();
}
// loop runs until number 5 is not pressed to exit
int ch;
do {
System.out.println("\n ***Banking Application***");
System.out.println("1. Check your account \n 2. Deposit the amount \n 3. Withdraw the amount \n 4.Exit ");
System.out.println("Enter your choice: ");
ch = sc.nextInt();
switch (ch) {
case 1:
System.out.print("Enter account no. you want to search: ");
String ac_no = sc.next();
boolean found = false;
for (int i = 0; i < C.length; i++) {
found = C[i].search(ac_no);
if (found) {
break;
}
}
try{
if(!found)
{
throw new AccountNotFoundException();
}
}
catch(AccountNotFoundException a)
{
System.out.println("Account doesn't exist..!!");
break;
}
break;
case 2:
System.out.print("Enter Account no. : ");
ac_no = sc.next();
found = false;
for (int i = 0; i < C.length; i++) {
found = C[i].search(ac_no);
if (found) {
C[i].deposit();
break;
}
}
try{
if(!found)
{
throw new AccountNotFoundException();
}
}
catch(AccountNotFoundException a)
{
System.out.println("Account doesn't exist..!!");
break;
}
break;
case 3:
System.out.print("Enter Account No : ");
ac_no = sc.next();
found = false;
for (int i = 0; i < C.length; i++) {
found = C[i].search(ac_no);
if (found) {
C[i].withdrawal();
break;
}
}
try{
if(!found)
{
throw new AccountNotFoundException();
}
}
catch(AccountNotFoundException a)
{
System.out.println("Account doesn't exist..!!");
break;
}
break;
case 4:
System.out.println("Thank You !!");
break;
}
}
while (ch != 4);
}
}
class MinimumBalanceException extends Exception{
MinimumBalanceException(){
super("Minimum Balance Insufficient");
}
}
class InsufficientBalanceException extends Exception{
InsufficientBalanceException(){
super("Insufficient Balance Please Try With Different Amount");
}
}
class AccountNotFoundException extends Exception{
AccountNotFoundException(){
super("This account does not exist!!");
}
}