-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.java
481 lines (469 loc) · 11.7 KB
/
library.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
/*This program implements the concept of Inheritance.
Library is an abstract class.
Classes Admin and Student inherit the class library.
They override the viewAvailableBooks() function.
There are exception handlers to handle the RuntimeExceptions such as InputMismatchException and NoSuchElementException.
LocalDate class and ArrayList is also used.
*/
import java.io.*;
import java.util.*;
import java.time.*;
class Book
{
int book_id,count;
String book_name,subject,author;
Book()
{
book_id=0;
count=0;
book_name="";
subject="";
author="";
}
Book(int id,String name,String sub,String auth,int c)
{
book_id=id;
count=c;
book_name=name;
subject=sub;
author=auth;
}
public String toString()
{
return book_id+"\t"+book_name+"\t"+subject+"\t"+author+"\t"+count;
}
}
abstract class Library // Abstract class used
{
static ArrayList <Book> m= new ArrayList<>();
static ArrayList <Student> students= new ArrayList<>();
String names[]={"Riya","Ashok","Priya","Shreya","Siddharth","Sahil","Yash","Riddhi","Anjali","Pankaj"};
public void initialize()
{
m.add(new Book(101,"Higher Engineering Mathematics","maths","B. S. Garewal",10));
m.add(new Book(102,"Advanced Engineering Mathematics","maths","Erwin Kreyszig",12));
m.add(new Book(103,"Mathematics for Engineers","maths","R. K. Jain",15));
m.add(new Book(104,"Advanced Probability Theory","maths","Janos Galambos",16));
m.add(new Book(105,"Discrete Mathematics and its Applications","maths","Kenneth H. Rosen",14));
m.add(new Book(201,"Higher Engineering Physics","physics","H. K. Malik",10));
m.add(new Book(202,"Engineering Physics","physics","D. K. Bhattacharya",12));
m.add(new Book(203,"Modern Engineering Physics","physics","A S Vasudeva",15));
m.add(new Book(204,"Concepts of Physics","physics","H. C. Verma",13));
m.add(new Book(205,"Principles Of Physics","physics","P. V. Naik",18));
m.add(new Book(301,"Fundamentals of Software Engineering","computer science","Rajib Mall",10));
m.add(new Book(302,"Let Us C","computer science","Ashutosh Pandey",12));
m.add(new Book(303,"Computer Networks","computer science","Andrew S. Tanenbaum",15));
m.add(new Book(304,"Data structures and algorithms in C++","computer science","Adam Drozdek",13));
m.add(new Book(305,"Java The Complete Reference","computer science","Herbert Schildt",18));
m.add(new Book(401,"Cambridge English for Engineering","english","Mark Ibbotson",10));
m.add(new Book(402,"Professional English in Use","english","Mark Ibbotson",12));
m.add(new Book(403,"English Conversation Practice","english","Grant Taylor",15));
m.add(new Book(404,"Wren & Martin","english","P. C. Wren",16));
m.add(new Book(405,"Oxford Modern English Grammar","english","Bas Aarts",14));
for(int i=0;i<10;i++)
{
students.add(new Student(11800+(i+1),names[i]));
}
}
public Book get_details()
{
int c=0;
Scanner sc= new Scanner(System.in);
System.out.println("\nEnter Book id:");
int book_id=sc.nextInt();
sc.nextLine();
for (Book b:m)
{
if(b.book_id==book_id)
c=1;
}
if(c==1)
{
throw new RuntimeException("this book id already exists!");
}
System.out.println("\nEnter Book Name:");
String book_name=sc.nextLine();
System.out.println("\nEnter subject:");
String subject=sc.nextLine();
System.out.println("\nEnter author Name:");
String author=sc.nextLine();
System.out.println("\nEnter the number of copies:");
int count=sc.nextInt();
Book bk=new Book(book_id,book_name,subject,author,count);
return bk;
}
public abstract void viewAvailableBooks();//abstract function
}
class Admin extends Library
{
public void addBooks()
{
try
{
int f=0;
Book bk=get_details();
super.m.add(bk);
System.out.println("\nBook Successfully added....!!!\n");
}
catch(InputMismatchException ex)
{
System.out.println("\nIncorrect input!");
}
catch(RuntimeException t)
{
System.out.println(t);
}
}
public void viewAvailableBooks()// overriding the abstract function
{
try
{
Scanner sc= new Scanner(System.in);
System.out.println("\nEnter your choice:\n1-view subject wise\n2-view all");
int ch=sc.nextInt();
sc.nextLine();
if(ch==1)
{
System.out.println("\nEnter subject:");
String s=sc.nextLine();
s=s.trim();
int f=0;
System.out.println("book id\tbook name\tauthor\tcopies");
for (Book b:super.m)
{
if(b.subject.equalsIgnoreCase(s))
{
System.out.println(b.book_id+"\t"+b.book_name+"\t"+b.author+"\t"+b.count);
f=1;
}
}
if(f==0)
{
throw new NoSuchElementException("Subject not found...!!!");
}
}
else if(ch==2)
{
for (Book b:super.m)
{
System.out.println(b);
}
}
else
{
System.out.println("\nInvalid input!");
}
}
catch(InputMismatchException ex)
{
System.out.println("\nIncorrect input!");
}
catch(NoSuchElementException u)
{
System.out.println(u);
}
}
public void deleteBook()
{
Scanner sc= new Scanner(System.in);
System.out.println("\nEnter subject for which you want to delete the record:");
String s=sc.nextLine();
System.out.println();
for (Book b:super.m)
{
if(b.subject.equalsIgnoreCase(s))
System.out.println(b);
}
try
{
System.out.println("\nEnter the record id which you want to delete:");
int rec = sc.nextInt();
int i=0;
for (Book b:super.m)
{
if(b.book_id==rec)
i=m.indexOf(b);
else
i=-1;
}
if(i!=-1)
{
m.remove(i);
System.out.println("\nRecord successfully deleted...!!!");
}
else
{
throw new NoSuchElementException("No such record found...!!!");
}
}
catch(InputMismatchException ex)
{
System.out.println("\nIncorrect input!");
}
catch(NoSuchElementException u)
{
System.out.println(u);
}
}
}
class Student extends Library
{
String name,i_book;
int reg_no,flag=0;
LocalDate i_date,r_date;
Scanner sc= new Scanner(System.in);
Student()
{
name="";
reg_no=0;
}
Student(int reg,String n)
{
name=n;
reg_no=reg;
}
public void viewAvailableBooks()// Overriding the abstract function
{
try
{
System.out.println("\nEnter your choice:\n1-Search by subject\n2-Search by book name");
int ch=sc.nextInt();
sc.nextLine();
if(ch==1)
{
System.out.println("\nEnter subject:");
String s=sc.nextLine();
s=s.trim();
int f=0;
System.out.println("book id\t\tbook name\t\tauthor\t\tcopies");
for (Book b:super.m)
{
if(b.subject.equalsIgnoreCase(s))
{
System.out.println("\n"+b.book_id+"\t"+b.book_name+"\t"+b.author+"\t"+b.count);
f=1;
}
}
if(f==0)
{
throw new NoSuchElementException("Subject not found...!!!");
}
}
else if(ch==2)
{
System.out.println("\nEnter book name:");
String s=sc.nextLine();
s=s.trim();
int f=0;
for (Book b:super.m)
{
if(b.book_name.equalsIgnoreCase(s))
{
System.out.println("\n"+b.book_id+"\t"+b.book_name+"\t"+b.author+"\t"+b.count);
f=1;
}
}
if(f==0)
{
throw new NoSuchElementException("Book not found...!!!");
}
}
}
catch(InputMismatchException ex)
{
System.out.println("\nIncorrect input!");
}
catch(NoSuchElementException u)
{
System.out.println(u);
}
}
public void issue_book()
{
if(flag==1)
{
System.out.println("\nYou have already issued a book! Please return the book if you want to issue another book!");
}
else
{
try
{
int f=0;
System.out.println("Enter Book id:");
int id=sc.nextInt();
Book bk=new Book();
for(Book b:super.m)
{
if(b.book_id==id)
{
bk=b;
f=1;
}
}
if(f==0)
{
throw new NoSuchElementException("Invalid book id...!!!");
}
else
{
if(bk.count==0)
System.out.println("\nThis book is out of stock!");
else
{
i_book=bk.book_name;
bk.count-=1;
this.flag=1;
i_date=LocalDate.now();
r_date=i_date.plusWeeks(1);
System.out.println("\nSuccessfully issued!"+
"\nissue date: "+i_date+"\treturn date: "+r_date+
"\nPlease collect your book!");
}
}
}
catch(InputMismatchException ex)
{
System.out.println("\nIncorrect input!");
}
catch(NoSuchElementException u)
{
System.out.println(u);
}
}
}
public void return_book()
{
if(flag==1)
{
Book bk=new Book();
for (Book b:super.m)
{
if(b.book_name.equalsIgnoreCase(i_book))
{
bk=b;
}
}
bk.count+=1;
System.out.println("\nSuccessfully returned the book "+i_book+"! \nPlease keep the book at the counter!");
this.flag=0;
}
else
{
System.out.println("\nYou have not issued any book!");
}
}
}
class execute
{
public static void main(String args[])
{
int ch=0;
do
{
try
{
Scanner sc= new Scanner(System.in);
System.out.println("\n1-Admin login\n2-user login\n3-exit");
ch=sc.nextInt();
sc.nextLine();
if(ch==1)
{
System.out.println("\nEnter User Name:");
String u_name=sc.nextLine();
if(u_name.equals("admin"))
{
System.out.println("\nEnter Password:");
String password=sc.nextLine();
if (password.equals("admin@123"))
{
System.out.println("\nlogin successful....!!!");
System.out.println("\nHii Admin! What operation would you like to perform?");
int choice;
Admin obj=new Admin();
obj.initialize();
do
{
System.out.println("\nEnter:\n1- add books\n2-view books\n3-remove books\n4-log out");
choice=sc.nextInt();
switch(choice)
{
case 1:obj.addBooks();
System.out.println();
break;
case 2:obj.viewAvailableBooks();
break;
case 3: obj.deleteBook();
break;
case 4: break;
default:System.out.println("Invalid choice!");
}
}while(choice==1 || choice==2 || choice==3);
}
else
{
System.out.println("\nIncorrect password!");
}
}
else
{
System.out.println("\nInvalid username!");
}
}
else if (ch==2)
{
System.out.println("\nHello Student ! Enter your name:");
String nam=sc.nextLine();
System.out.println("\nEnter your registration number:");
int f=0;
Student obj=new Student();
obj.initialize();
int num=sc.nextInt();
for(Student s:Library.students)
{
if(s.name.equalsIgnoreCase(nam))
{
f=1;
if(s.reg_no==num)
{
f=2;
obj=s;
}
}
}
if(f==0)
System.out.println("\nInvalid name!");
else if(f==1)
System.out.println("\nInvalid registration number!");
else
{
System.out.println("\nlogin successful....!!!");
int choice;
do
{
System.out.println("\nEnter:\n1-search\n2-issue book\n3-return book\n4-log out");
choice=sc.nextInt();
switch(choice)
{
case 1:obj.viewAvailableBooks();
break;
case 2: obj.issue_book();
//System.out.println("Student flag: "+obj.flag);
break;
case 3: obj.return_book();
//System.out.println("Student flag: "+obj.flag);
break;
case 4: break;
default:System.out.println("Invalid choice!");
}
}while(choice==1||choice==2||choice==3);
}
}
}
catch(RuntimeException e)
{
ch=1;
System.out.println("\nIncorrect input!");
}
}while(ch==1|| ch==2);
}
}