-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlibrary_management.java
620 lines (504 loc) · 15.5 KB
/
library_management.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import java.util.Date;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
class Student
{
String name;
int id_no;
String Stream;
String book1,book2;
int book_no,issuedbook;
Student(String name,int id_no,String Stream)
{
this.name=name;
this.id_no=id_no;
this.Stream=Stream;
}
}
public class library_management
{
static void Selectionsort( Student arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j].id_no < arr[min_idx].id_no)//Sort according to ID number of student
min_idx = j;
String temp1 = arr[min_idx].name;
arr[min_idx].name = arr[i].name;
arr[i].name = temp1;
String temp2 = arr[min_idx].Stream;
arr[min_idx].Stream = arr[i].Stream;
arr[i].Stream = temp2;
}
}
static void display(Student arr[])
{
for(int i=0;i<arr.length;i++)
{
System.out.println("\nName of Student:" + arr[i].name);
System.out.println("\nId of Student:" + arr[i].id_no);
System.out.println("\nStream of Student:" + arr[i].Stream);
}
}
class Node
{
String key;
Node left, right;
public Node(String item)
{
key = item;
left = null;
right=null;
}
}
Node root;
private static Scanner input;
finaldsa()
{
root = null;
}
//Insert Book
void insert(String key)
{
root = insertRec(root, key);
}
Node insertRec(Node root, String key)
{
if(root == null)
{
root = new Node(key);
return root;
}
if (key.compareTo(root.key)<0) //If book name < root then place it as left child
root.left = insertRec(root.left, key);
else if (key.compareTo(root.key)>0) //If book name > root then place it as Right child
root.right = insertRec(root.right, key);
else
System.out.println("error.");
return root;
}
void update(String key,String key1)
{
deleteKey(key);
insert(key1);
}
//Search Book
public boolean containsNode(String value)
{
return containsNodeRecursive(root, value);
}
private boolean containsNodeRecursive(Node current, String key)
{
if (current == null)
{
return false;
}
//If book name < root then place it as left child
if (key.equalsIgnoreCase(current.key))
{
return true;
}
//If book name < root then search at left side of root else right side
return key.compareTo(current.key)<0
? containsNodeRecursive(current.left, key)
: containsNodeRecursive(current.right, key);
}
//print tree in 2D
void printTree()
{
root = printTreeRec(root, 0);
}
Node printTreeRec(Node t , int space)
{
if(t == null)
return root;
space += 5;
printTreeRec(t.right ,space);
System.out.println();
for(int i = 5 ; i < space ; i++)
System.out.print( " ");
System.out.print("[" +t.key+ "]");
return printTreeRec(t.left ,space);
}
void deleteKey(String key)
{
root = deleteRec(root, key);
}
Node deleteRec(Node root, String key)
{
if (root == null) return root;
//If book name < root then search it at left side and delete
if (key.compareTo(root.key)<0)
root.left = deleteRec(root.left, key);
//If book name > root then search it at right side and delete
else if (key.compareTo(root.key)<0)
root.right = deleteRec(root.right, key);
else
{
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
root.key = minValue(root.right);
root.right = deleteRec(root.right, root.key);
}
return root;
}
String minValue(Node root)
{
String minv = root.key;
while (root.left != null)
{
minv = root.left.key;
root = root.left;
}
return minv;
}
//Print Books Inorder
void printInorder(Node node)
{
if (node == null)
return;
printInorder(node.left);
System.out.print(node.key + " ");
printInorder(node.right);
}
void printInorder()
{
printInorder(root);
}
void inorder()
{
inorderRec(root);
}
void inorderRec(Node root)
{
if (root != null)
{
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
public static void main(String[] args) throws Exception
{
input = new Scanner(System.in);
finaldsa tree = new finaldsa();
HashMap<String, Integer> hashmapping = new HashMap<>();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance();
Student[] array =new Student[3];
//Add student Details
array[0]=new Student("Rajvi",1741078,"B.Tech-ICT");
array[1]=new Student("Krushna",1741086,"B.Tech-ICT");
array[2]=new Student("Kalagee",1741052,"B.Tech-ICT");
int [][] arr=new int[100][2];
//Create file to store data of students.
FileWriter fr = new FileWriter("append.txt", true);
BufferedWriter br = new BufferedWriter(fr);
FileWriter fr1 = new FileWriter("append.txt", true);
BufferedWriter br1 = new BufferedWriter(fr1);
FileWriter fr2 = new FileWriter("append.txt", true);
BufferedWriter br2 = new BufferedWriter(fr2);
FileWriter fr3 = new FileWriter("append.txt", true);
BufferedWriter br3 = new BufferedWriter(fr3);
FileReader file = new FileReader("x.txt");
BufferedReader reader = new BufferedReader(file);
FileReader file2= new FileReader("y.txt");
BufferedReader reader2 = new BufferedReader(file2);
FileReader file3= new FileReader("z.txt");
BufferedReader reader3 = new BufferedReader(file3);
Date Rday1 = null,Rday2 = null,Cday=null;
boolean e1=false;
int i=0;
while(e1==false)
{
System.out.println("\n....................................." );
System.out.println("1. Librarian Login. ");
System.out.println("2. User Login. ");
System.out.println("3. Exit. ");
System.out.println("\n....................................." );
System.out.println("\nEnter Your choice:");
int ch1 = input.nextInt();
switch(ch1)
{
case 1: //For Librarian login
String pwd1="abc123";
String id1="dsa@1";
System.out.println("\nEnter UserId:" );
String id2 = input.next();
System.out.println("\nEnter Password:" );
String pwd2=input.next();
if(!id1.equals(id2))
System.out.println("Invalid Userid.");
else if(!pwd1.equals(pwd2))
System.out.println("Invalid Password.");
else
{
System.out.println("Login succesfully.");
boolean e2=false;
while(e2==false)
{
System.out.println("\n....................................." );
System.out.println("1. Add book. ");
System.out.println("2. Delete book. ");
System.out.println("3. Update book. ");
System.out.println("4. Print Books Details. ");
System.out.println("5. Print Books in-order. ");
System.out.println("6. Print tree ");
System.out.println("7. Exit");
System.out.println("\n....................................." );
System.out.println("\nEnter Your choice:");
int ch2 = input.nextInt();
switch(ch2)
{
case 1: //To add a book
String line;
while((line = reader.readLine()) != null)
{
tree.insert(line);
hashmapping.put(line, i);
i++;
}
int j=i;
int o = 0;
String number;
while((number = reader2.readLine()) != null)
{
int result = Integer.parseInt(number);
if(j!=o)
arr[o][0] = result;
o++;
}
int pq=0;
String number1;
while((number1 = reader3.readLine()) != null)
{
int result1 = Integer.parseInt(number1);
if(j!=pq)
arr[pq][1] = result1;
pq++;
}
System.out.println("\nEnter name of book:");
String name = input.next();
boolean z1=tree.containsNode(name);
if(z1==true)
{
System.out.println("\nIt is already exists:");
}
else
{
System.out.println("\nEnter quantity of book:");
int quantity = input.nextInt();
br1.write(name);
br2.write(quantity);
br3.write(quantity);
tree.insert(name);
hashmapping.put(name, i);
hashmapping.get(name);
arr[i][0]+=quantity;//Total quantity of books
arr[i][1]+=quantity;//Available quantity of books
i++;
}
break;
case 2: //To delete a book
System.out.println("\nEnter name of book:");
String b1 = input.next();
boolean x=tree.containsNode(b1);
if(x==true)
{
tree.deleteKey(b1);
hashmapping.remove(b1);
}
break;
case 3: //To update any book
System.out.println("\nEnter name of book:");
String b2 = input.next();
boolean z=tree.containsNode(b2);
if(z==true)
{
int a=hashmapping.get(b2);
System.out.println("\nEnter quantity of book to add more:");
int q = input.nextInt();
arr[a][0]+=q;
}
break;
case 4: //Print Books Details
Set<Entry<String, Integer>> setOfEntries = hashmapping.entrySet();
for(Entry<String, Integer> entry : setOfEntries)
{
int r=entry.getValue();
System.out.println("Name of book is: " + entry.getKey());
System.out.println("Total Quantity of book is: " + arr[r][0]);
System.out.println("Available Quantity of book is: " + arr[r][1]);
System.out.println();
}
break;
case 5: //To Print Books in-order
tree.printInorder();
break;
case 6://To print binary search tree
tree.printTree();
break;
case 7:
e2=true;
break;
}
}
}
break;
case 2: //For User Login
boolean e3=false;
while(e3==false)
{
System.out.println("\n....................................." );
System.out.println("1. Issue book. ");
System.out.println("2. Return book. ");
System.out.println("3. Exit");
System.out.println("\n....................................." );
System.out.println("\nEnter Your choice:");
int ch3 = input.nextInt();
switch(ch3)
{
case 1://To issue a book
int index = -1;
System.out.println("\nEnter your id:");
int id = input.nextInt();
//display(array);
for(int k=0;k<3;k++)
{
if(array[k].id_no==id)
index=k;
}
if(index!=-1)
{
if(array[index].book_no==2)
{
System.out.println("\nYou can't issue more than two books.");
}
else
{
System.out.println("\nEnter name of book:");
String book = input.next();
boolean x=tree.containsNode(book);
if(x==true)
{
int a=hashmapping.get(book);
if(arr[a][1]>0)
{
if(array[index].book1==null)
array[index].book1=book;
else
array[index].book2=book;
System.out.println("Book issued successfully.");
arr[a][1]--;
Cday=cal.getTime();
System.out.println("Currunt Date Time : " + formatter.format(cal.getTime()));
cal.add(Calendar.SECOND, 5);
Rday1 = cal.getTime();
System.out.println("Due Date Time: " + formatter.format(Rday1));
array[index].book_no++;
br.write("\nStudent name: " + array[index].name);
br.write("\nStudent ID : " + array[index].id_no);
br.write("\nIssued Book : " + book);
br.write("\nIssued date : " + formatter.format(Cday));
br.write("\nReturn date : " + formatter.format(Rday1));
}
else
System.out.println("You can not issue book now.\nTry again after some days");
}
else
System.out.println("Book is not available.");
}
}
else
System.out.println("Invalid ID");
break;
case 2://to return a book
try
{
int ind = -1;
System.out.println("\nEnter your id:");
int s_id = input.nextInt();
System.out.println("\nEnter name of book:");
String Rbook = input.next();
for(int k=0;k<3;k++)
{
if(array[k].id_no==s_id && (array[k].book1.equalsIgnoreCase(Rbook)==true || array[k].book2.equalsIgnoreCase(Rbook)==true))
ind=k;
}
if(ind!=-1)
{
boolean y=tree.containsNode(Rbook);
if(y==true)
{
if(array[ind].book1.equalsIgnoreCase(Rbook)==true)
array[ind].book1=null;
else
array[ind].book2=null;
cal = Calendar.getInstance();
Rday2=cal.getTime();
//System.out.println(Rday2 + "&"+ Rday1);
if(Rday2.after(Rday1))
{
System.out.println("Book is overdue.");
long diff=Rday2.getTime()-Rday1.getTime();
int noofdays=(int)(diff/(2000/**24*60*60*/));
System.out.println("Due Date Time: " + formatter.format(Rday2));
System.out.println("book is delayed by " + noofdays + "seconds." + diff);
double charge =noofdays*5;
System.out.println("Your charge is: " + charge + "Rs." );
}
else
{
System.out.println("Book is returned successfully.");
}
int a=hashmapping.get(Rbook);
arr[a][1]++;
array[ind].book_no--;
}
}
else
System.out.println("Invalid ID");
}
catch(Exception e)
{
System.out.println("Something is going to be wrong.");
}
break;
case 3:
e3=true;
break;
}
}
break;
case 3:
e1=true;
break;
}
}
br.close();
fr.close();
br1.close();
fr1.close();
br2.close();
fr2.close();
br3.close();
fr3.close();
reader.close();
reader2.close();
reader3.close();
}
}