-
Notifications
You must be signed in to change notification settings - Fork 6
/
Fs.java
385 lines (362 loc) · 8.82 KB
/
Fs.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package fs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author Rutuja Rajesh
*/
class records{
static String name,fee,spec,phone,keyf;
}
class node{
int [] a=new int[5];
int size;
node[] next=new node[4];
node parent;
node(){
for(int i = 0; i < 4; i++)
next[i] = null;
parent = null;
size = 0;
}
}
class btree
{
node root=new node();
public node findLeaf(int key, int level)
{
node ptr = root;
node prevptr = null;
level = 0;
int i;
while (ptr != null)
{
i = 0;
level++;
while (i < ptr.size-1 && key > ptr.a[i])
{
i++;
}
prevptr = ptr;
ptr = ptr.next[i];
}
return prevptr;
}
public void updateKey(node parent, node child, int newkey)
{
if (parent == null)
{
return;
}
if (parent.size == 0)
{
return;
}
int oldkey = child.a[child.size-2];
for (int i = 0; i < parent.size;i++)
{
if (parent.a[i] == oldkey)
{
parent.a[i] = newkey;
parent.next[i] = child;
}
}
}
public void search(int key)
{
if (root == null)
{
System.out.print("The tree Does not exist");
System.out.print("\n");
return;
}
int level=0;
node leaf = findLeaf(key,level);
int flg = 0;
for (int i = 0; i < leaf.size; i++)
{
if (leaf.a[i] == key)
{
flg = 1;
System.out.print("The DOCTOR ID ");
System.out.print(key);
System.out.print(" Exists in the B-Tree at the level ");
System.out.print(level);
System.out.print("\n");
}
}
if (flg == 0)
{
System.out.print("The ID Searched for was not found");
System.out.print("\n");
}
}
public void insert(int key)
{
if (root == null)
{
root = new node();
root.a[root.size] = key;
root.size++;
return;
}
int level=0;
node leaf = findLeaf(key,level);
int i;
for (i = 0; i < leaf.size; i++)
{
if (leaf.a[i] == key)
{
System.out.print("The Record to be inserted already exists");
System.out.print("\n");
return;
}
}
promote(leaf,key,null);
System.out.print("---------------\n");
traverse(root);
System.out.print("----------------\n");
}
void insertIntoNode(node n,int key,node address){
int i;
if( n == null)
return;
for(i = 0; i < n.size; i++)
if(n.a[i] == key)
return;
i = n.size-1;
while(i >= 0 && n.a[i] > key)
{
n.a[i+1] = n.a[i];
n.next[i+1] = n.next[i];
i--;
}
i++;
n.a[i] = key;
n.next[i] = address;
n.size++;
if( i == n.size-1)
updateKey(n.parent,n,key);
}
void promote(node n,int key,node address){
if( n == null)
return;
if(n.size < 4)
{
insertIntoNode(n,key,address);
return;
}
if( n == root)
{
root = new node();
n.parent = root;
}
node newptr = split(n);
node t;
if(key < n.a[0])
t = newptr;
else
t = n;
insertIntoNode(t,key,address);
promote(n.parent,n.a[n.size-1],n);
promote(newptr.parent,newptr.a[newptr.size-1],newptr);
}
node split(node n){
int midpoint = (n.size+1)/2;
int newsize = n.size - midpoint;
node newptr = new node();
node child;
newptr.parent = n.parent;
int i;
for(i = 0; i < midpoint; i++)
{
newptr.a[i] = n.a[i];
newptr.next[i] = n.next[i];
n.a[i] = n.a[i+midpoint];
n.next[i] = n.next[i+midpoint];
}
n.size = midpoint;
newptr.size = newsize;
for( i = 0; i < n.size; i++)
{
child = n.next[i];
if(child!= null)
child.parent = n;
}
for( i = 0; i < newptr.size; i++)
{
child = newptr.next[i];
if(child!= null)
child.parent = newptr;
}
return newptr;
}
void traverse(node ptr){
if(ptr == null)
return;
for(int i = 0; i < ptr.size; i++)
System.out.print(ptr.a[i]+" ");
System.out.println();
for(int i = 0; i < ptr.size;i++)
traverse(ptr.next[i]);
}
btree(){
root = null;
}
}
/**
*
* @author Rajesh
*/
public class Fs {
static int countno() throws FileNotFoundException, IOException{
int no1;
no1=0;
try {
File fle = new File("C:\\Users\\Rajesh\\Documents\\NetBeansProjects\\fs\\bpluss.txt");
if (fle.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
FileWriter fw = new FileWriter(fle,true);
} catch(IOException ioe){
System.out.println("Exception occurred:");
}
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Rajesh\\Documents\\NetBeansProjects\\fs\\bpluss.txt"));
String line;
while((line=reader.readLine())!=null)
{
no1++;
}
no1=no1-1;
reader.close();
return no1;
}
static String name,fee,spec,phone,count;
static records [] recs=new records[100];
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
new options().setVisible(true);
// TODO code application logic here
/**
String[] nodes=new String[100];
btree b = new btree();
int choice=1,key=1,no,rkey;
no=countno();
System.out.println("No. of records: "+no+"\n");
for(int i=0;i<no;i++)
recs[i]=new records();
BufferedReader bbr = new BufferedReader(new FileReader("C:\\Users\\Rajesh\\Documents\\NetBeansProjects\\fs\\bpluss.txt"));
for(int i=0;i<no;i++)
{
String lineRead=bbr.readLine();
String [] st = lineRead.split("\\|");
recs[i].keyf=st[0];
recs[i].name=st[1];
recs[i].fee=st[2];
recs[i].spec=st[3];
recs[i].phone=st[4];
rkey=Integer.valueOf(recs[i].keyf);
b.insert(rkey);
}
bbr.close();
while(true)
{
System.out.print("Welcome to DOCTOR RECORDS \n");
System.out.print("1.Insert a record\n");
System.out.print("2.Search for record details\n");
System.out.print("3.traverse the ID values\n");
System.out.print("4. exit \n");
Scanner sc=new Scanner(System.in);
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter The DOCTOR ID: ");
key=sc.nextInt();
b.insert(key);
System.out.print("Enter the DOCTOR's name: ");
name=sc.next();
System.out.print("Enter the fees: ");
fee=sc.next();
System.out.print("Enter the spec: ");
spec=sc.next();
System.out.print("Enter phone: ");
phone=sc.next();
//TRY TO FIND HOW TO WRITE OBJECTS INTO A FILE
try {
FileWriter fw=new FileWriter("C:\\Users\\Rajesh\\Documents\\NetBeansProjects\\fs\\bpluss.txt",true);
fw.write(""+key);
fw.write("|");
fw.write(name);
fw.write("|");
fw.write(fee);
fw.write("|");
fw.write(spec);
fw.write("|");
fw.write(phone);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.close();
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
no++;
break;
case 2: System.out.print("Enter The doctor ID to be searched: ");
key=sc.nextInt();
b.search(key);
bbr=new BufferedReader(new FileReader("C:\\Users\\Rajesh\\Documents\\NetBeansProjects\\fs\\bpluss.txt"));
for(int i=0;i<no;i++)
{
String lineRead=bbr.readLine();
String [] t=lineRead.split("\\|");
recs[i].keyf=t[0];
recs[i].name=t[1];
recs[i].fee=t[2];
recs[i].spec=t[3];
recs[i].phone=t[4];
rkey=Integer.valueOf(recs[i].keyf);
if(key==rkey)
{
System.out.println("DOCTOR's Name: "+recs[i].name);
System.out.println("FEES: "+recs[i].fee);
System.out.println("SPEC: "+recs[i].spec);
System.out.println("phone no: "+recs[i].phone+"\n\n");
}
}
bbr.close();
break;
case 3: System.out.println("DOCTOR ID\tDOCTOR Name\tFEES\tSPECIALISATION\tPHONE\n");
bbr=new BufferedReader(new FileReader("C:\\Users\\Rajesh\\Documents\\NetBeansProjects\\fs\\bpluss.txt"));
for(int i=0;i<=no;i++)
{
String lineRead=bbr.readLine();
String [] t=lineRead.split("\\|");
recs[i].keyf=t[0];
recs[i].name=t[1];
recs[i].fee=t[2];
recs[i].spec=t[3];
recs[i].phone=t[4];
System.out.println(recs[i].keyf+"\t\t"+recs[i].name+"\t\t"+recs[i].fee+"\t\t"+recs[i].spec+"\t\t"+recs[i].phone+"\t\t\t");
}
bbr.close();
break;
case 4:return;
}
}
*/
}
}