forked from prashantkalokhe/Hactoberfest-2022-New
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank.java
268 lines (239 loc) · 7.47 KB
/
bank.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import java.util.*;
import java.io.*;
class node{
int acc_no;
String name;
String address;
Double balance;
node next;
}
class bank{
Scanner sc = new Scanner(System.in);
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
node head;
bank(){
head=null;
}
void create() throws IOException {
int a;
String b,c;
Double d;
node ptr;
System.out.println("Enter the new Account Number to be Inserted:");
a= sc.nextInt();
System.out.println("Enter the new Name to be Inserted:");
b= br.readLine();
System.out.println("Enter the new Address to be Inserted:");
c=br.readLine();
System.out.println("Enter the new Balance to be Inserted:");
d=sc.nextDouble();
ptr=new node();
ptr.acc_no=a;
ptr.name=b;
ptr.address=c;
ptr.balance=d;
ptr.next=null;
if (head==null){
head=ptr;
System.out.println("\n\tCreated first Node");
}
else{
ptr.next=head;
head=ptr;
System.out.println("\n\tInserted");
}
}
void insert() throws IOException {
int e;
String f,g;
Double h;
node prev,ptr,cur;
System.out.println("Enter the new Account Number to be Inserted:");
e= sc.nextInt();
System.out.println("Enter the new Name to be Inserted:");
f= br.readLine();
System.out.println("Enter the new Address to be Inserted:");
g=br.readLine();
System.out.println("Enter the new Balance to be Inserted:");
h=sc.nextDouble();
ptr=new node();
ptr.acc_no=e;
ptr.name=f;
ptr.address=g;
ptr.balance=h;
ptr.next=null;
prev=null;
cur=head;
while (cur!=null && cur.acc_no<e){
prev=cur;
cur=cur.next;
}
if (cur!=null && cur.acc_no==e){
System.out.println("\n\tNo Duplication of elements");
}
else {
/*Insertion is possible*/
ptr=new node();
ptr.acc_no=e;
ptr.name=f;
ptr.address=g;
ptr.balance=h;
ptr.next=null;
/*now insertion*/
if (prev==null){
// insertion at head
ptr.next=head;
head=ptr;
System.out.println("\n\tInserted at head");
}
else if (prev!=null && cur==null) {
// Insertion at the End
prev.next=ptr;
System.out.println("\n\tInserted at the end");
}
else{
// Insertion in between
prev.next=ptr;
ptr.next=cur;
System.out.println("\n\tInserted in Between");
}
}
}
void delete(){
node prev,cur;
if (head==null){
System.out.println("List is empty!");
}
else {
System.out.println("Enter the account number you want to delete: ");
int v=sc.nextInt();
prev=null;
cur=head;
while (cur!=null && cur.acc_no<v){
prev=cur;
cur=cur.next;
}
if (cur==null ||(cur!=null && cur.acc_no!=v)){
System.out.println("No such Account Number Exist!");
}
else{
/*cur is node to be deleted*/
if(prev==null){
head=cur.next;
cur.next=null;
cur=null;
System.out.println("Account number Deleted");
}
else {
prev.next=cur.next;
cur.next=null;
cur=null;
System.out.println("Account number Deleted");
}
}
}
}
void display(){
node cur;
if (head==null){
System.out.println("\n\tList is Empty");
}
else {
cur=head;
while(cur!=null){
System.out.println("\n\tAccount Number: "+cur.acc_no+"\n\tName of the Customer:"+cur.name+"\n\tAddress of the Customer"+cur.address+"\n\tBalance amount of the account"+cur.balance);
cur=cur.next;
}
}
}
void deposit(){
node cur;
int acc_tosearch;
Double amount_todeposit;
System.out.println("Enter the Account Number you want to Deposit:");
acc_tosearch= sc.nextInt();
System.out.println("Enter the amount to be deposited:");
amount_todeposit= sc.nextDouble();
int flag=0;
cur=head;
while (cur!=null) {
if (cur.acc_no == acc_tosearch) {
flag = 1;
break;
}
cur = cur.next;
}
cur.balance= (cur.balance+amount_todeposit);
if (flag==1) {
System.out.println("Account Number Found and amount deposited\n\t" + "Available Balance:"+cur.balance);
}
else{
System.out.println("Account Number not found");
}
}
void withdraw(){
node cur;
int acc_tosearch;
Double amount_towithdraw;
System.out.println("Enter the Account Number you want to withdraw:");
acc_tosearch= sc.nextInt();
System.out.println("Enter the amount to be Withdrawn:");
amount_towithdraw= sc.nextDouble();
int flag=0;
cur=head;
while (cur!=null) {
if (cur.acc_no == acc_tosearch) {
flag = 1;
break;
}
cur = cur.next;
}
if (cur.balance>1000) {
cur.balance = (cur.balance - amount_towithdraw);
if (flag == 1) {
System.out.println("Account Number Found and amount withdrawn\n\t" + "Available Balance:" + cur.balance);
}
else{
System.out.println("Account Number not found");
}
}
else {
System.out.println("Amount Not Sufficient to be Withdrawn!");
}
}
public static void main(String args[]) throws IOException {
Scanner sc1=new Scanner(System.in);
bank b1=new bank();
int ch;
do {
System.out.println("********WELCOME TO MAHARASHTRA BANK*********");
System.out.println("\n\t1]Create first node\n\t2]Insertion of Account Number:\n\t3]Deletion of Account Number\n\t4]Deposit Amount\n\t5]Withdraw Amount\n\t6]Display the Linked List\n\t7]Exit");
System.out.println("\nEnter your Choice:");
ch=sc1.nextInt();
switch (ch){
case 1:
b1.create();
break;
case 2:
b1.insert();
break;
case 3:
b1.delete();
break;
case 4:
b1.deposit();
break;
case 5:
b1.withdraw();
break;
case 6:
b1.display();
break;
case 7:
System.out.println("********Thank you! visit again********");
break;
}
}while (ch!=7);
}
}