forked from Sahil19191/ATM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search_Tree.java
More file actions
212 lines (176 loc) · 4.69 KB
/
Binary_Search_Tree.java
File metadata and controls
212 lines (176 loc) · 4.69 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
package BST;
import java.util.*;
public class Binary_Search_Tree
{
Scanner scan = new Scanner(System.in);
// Creating A node of tree using inner class
private class Data
{
// Data Field
private String Name;
private int Acc_no;
private int Pin;
private double balance;
// Linking Field
Data L,R;
// Creating A node
Data(String N,int A,int P,double B)
{
Name=N;
Acc_no = A;
Pin = P;
balance = B;
L=R=null;
}
}
private Data root; // Creating reference of the root of BST
public Binary_Search_Tree()
{
root=null;
}
// Inserting a node using insert method
public void insert(String N,int A,int P,double B)
{
root=Insert_node(root, N, A, P, B);
}
private Data Insert_node(Data root,String N,int A,int P,double B)
{
if(root!=null){
if(A<root.Acc_no)
root.L=Insert_node(root.L,N,A,P,B);
else if(A>root.Acc_no)
root.R=Insert_node(root.R,N,A,P,B);
else{
System.out.println("Duplicates not allowed");
}
return root;
}
else
{
Data New = new Data(N,A,P,B);
return New;
}
}
// Inserting Finished
//Printing the data of all the users
public void Print()
{
Inorder(root);
}
private void Inorder(Data root)
{
if(root!=null)
{
Inorder(root.L);
Details(root);
Inorder(root.R);
}
}
// Printing Finished
// Basic Operations done by ATM
private void Deposit(Data root)
{
double a;
System.out.println("Enter Ammount to be deposited");
a=scan.nextDouble();
if(a<=0) System.out.println("Invalid ammount");
else
{
root.balance+=a;
}
}
private void Withdrawl(Data root)
{
double a;
System.out.println("Enter Ammount");
a=scan.nextDouble();
if(a>root.balance) System.out.println("Not enough balance");
else
{
root.balance-=a;
System.out.println("Collect your money from Cash Dispenser");
}
}
private void Change_Pin(Data root)
{
int p;
System.out.println("Enter old Pin");
p=scan.nextInt();
if(p==root.Pin)
{
System.out.println("Enter new Pin");
p=scan.nextInt();
root.Pin=p;
}
}
private void Details(Data root)
{
System.out.println("\n*******/ Details \\*******");
System.out.println(" Name : "+root.Name);
System.out.println("Account no: "+root.Acc_no);
System.out.println(" Pin : ****");
System.out.println(" Balance : "+root.balance+"\n");
}
private void Operations(Data root,boolean[] F)
{
int c;
int p;
F[0] = true;
// asking for PIN
System.out.println("Enter Pin");
p=scan.nextInt();
if(p==root.Pin){
for(int i=0;i<25;i++) System.out.print("*");
System.out.println("\nName : "+root.Name);
do{
System.out.print("\n\t1 - Deposit\n\t2 - Withdrawl\n\t3 - Change Pin\n\t4 - Details\n\t0 - Exit\n\tEnter: ");
c=scan.nextInt();
switch(c)
{
case 1:
Deposit(root);
break;
case 2:
Withdrawl(root);
break;
case 3:
Change_Pin(root);
break;
case 4:
Details(root);
break;
case 0:
break;
default:
System.out.println("Invalid Input");
}
}while(c!=0);
for(int i=0;i<25;i++) System.out.print("*");
System.out.println();
} // end of if
else
{
System.out.println("\n Wrong Pin");
}
}
// End of Operations
// Searching an Account to perform the Operations
public void search_Tree(int A,boolean[] F)
{
search(root, A,F);
}
private void search(Data root,int A,boolean[] F)
{
if(root!=null){
if(A > root.Acc_no)
search(root.R, A,F);
else if( A < root.Acc_no)
search(root.L, A,F);
else
{
Operations(root,F);
}
}
}
// End of Searching
} // End of class