-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.Java
514 lines (462 loc) · 16.8 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
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
//library management system.Each book should contain following details ->Title, Author, ISBN, Price, Publication, Date of release
//To add books,to delete books,to update the list of books
//To display all the books
//display new arrivals
//To display removed books
//To give user option to request new books
//To display details of a particular book
//Give switch option to end user to perform above activities form n number of times
//Create The application should be compatible with 4 users and Thread safe
//Each user is allowed to use application only for 4 minutes.
////Library should hold maximum 15 books , if count exceeds user should get a message
//"Racks are full" with customized class
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
// the LibraryManagement Interface
interface LibraryManagement {
void requestBook(int bookNumber);
void completeDetails(int bookNo);
void startLibrary() throws InterruptedException;
void addNewData();
void addToData();
void removeBook(int bookNo);
void update(String book);
void newDisplayList();
void displayList();
void removedBooks();
}
//Custom exceptions for alert
class FullRackException extends Exception {
public FullRackException() {
}
// getMessage alert for library overloading for books
public String getMessage() {
return "Racks are full: Cannot add new Books!";
}
}
class Book {
String title;
String author;
String ISBN;
int price;
String publication;
String dateOfRelease;
Book() { }
Book(String title, String author, String ISBN, int price, String publication, String dateOfRelease) {
this.title = title;
this.author = author;
this.ISBN = ISBN;
this.price = price;
this.publication = publication;
this.dateOfRelease = dateOfRelease;
}
}
// library threads using vector
public class Library extends Thread implements LibraryManagement {
static Vector<Book> data = new Vector<>();
static Vector<String> removed_data = new Vector<>();
// adding some new data to the system
public void addNewData() {
Scanner sc = new Scanner(System.in);
System.out.println("#===========================");
data.add(new Book());
System.out.println("Enter the title of the book: ");
data.get(data.size() - 1).title = sc.nextLine();
System.out.println("Enter the Author of " + data.get(data.size() - 1).title + " book name: ");
data.get(data.size() - 1).author = sc.nextLine();
System.out.println("Enter the ISBN of " + data.get(data.size() - 1).title + " book name: ");
data.get(data.size() - 1).ISBN = sc.next();
System.out.println("Enter the price of " + data.get(data.size() - 1).title + " book name: ");
data.get(data.size() - 1).price = sc.nextInt();
System.out.println("Enter the name of publication of " + data.get(data.size() - 1).title + " book name: ");
sc.nextLine();
data.get(data.size() - 1).publication = sc.nextLine();
System.out.println("Enter the date of release of " + data.get(data.size() - 1).title + " book name: ");
data.get(data.size() - 1).dateOfRelease = sc.next();
}
// new data
public void addToData() {
data.add(new Book("Harry Potter", "J.k. Rowling", "2-567891923-913-2", 100, "Bloomsbury","10-05-2000"));
data.add(new Book("East of Eden", "John Steinback", "1-456243483632-2", 180, "Steinback", "02-01-2001"));
data.add(new Book("The Sun Also Rises", "Ernest Hemingway"," 56789-01-2", 940, "Hemingway", "10-12-1986"));
data.add(new Book("The House of Mirth", "Edith Wharton", "10009-1-2-1567", 190, "P publications","4-02-2010"));
data.add(new Book("Their Eyes Were Watching God", "Zora Neale Hurston", "111009-2-456", 100, "K portal ","19-08-1986"));
data.add(new Book("Engineering psychology and human performance", "Christopher D Wickens", "12345-90-1-3", 220,"Filledman", "09-7-2011"));
data.add(new Book("Harry Potter and the Philosopher's Stone", "J.K. Rowling", "123456-0-989", 1050, "", ""));
}
// remove all the books one by one
public void removeBook(int bookNo) {
removed_data.add(data.get(bookNo - 1).title);
data.remove(bookNo - 1);
}
// update the existing system
public void update(String book) {
Scanner sc = new Scanner(System.in);
int index = -1;
for (int i = 0; i <= data.size() - 1; i++) {
if (data.get(i).title.equals(book)) {
index = i;
break;
}
}
System.out.println("#===================================");
if (index == -1) {
System.out.println("Books does not exist");
return;
}
System.out.println("Enter 1 to update title");
System.out.println("Enter 2 to update Author");
System.out.println("Enter 3 to update ISBN");
System.out.println("Enter 4 to update price");
System.out.println("Enter 5 to update name of the publication");
System.out.println("Enter 6 to update the date of release");
System.out.println("Enter exit to exit");
System.out.println("#======================");
String update = sc.next();
switch (update) {
case "1":
System.out.println("Enter the new book title: ");
sc.nextLine();
String title = sc.nextLine();
System.out.println(title);
data.get(index).title = title;
System.out.println("Book title is updated");
break;
case "2":
sc.nextLine();
System.out.println("Enter the new Author: ");
String author = sc.nextLine();
data.get(index).author = author;
System.out.print("Book author is updated");
break;
case "3":
System.out.println("Enter the new ISBN: ");
String ISBN = sc.next();
data.get(index).ISBN = ISBN;
break;
case "4":
System.out.println("Enter the new price: ");
int price = sc.nextInt();
data.get(index).price = price;
System.out.print("Book price is updated");
break;
case "5":
sc.nextLine();
System.out.println("Enter the new publication: ");
String publication = sc.nextLine();
data.get(index).publication = publication;
System.out.print("Book publication is updated");
break;
case "6":
System.out.println("Enter the new date: ");
String date = sc.next();
data.get(index).dateOfRelease = date;
System.out.print("Book date of release is updated");
break;
default:System.out.println("Exited!");
break;
}
}
public void newDisplayList() {
for (int i = data.size() - 1; i >= 0 && i >= data.size() - 1 - 4; i--) {
System.out.println(data.get(i).title);
}
}
public void displayList() {
for (int i = 0; i <= data.size() - 1; i++) {
System.out.println((i + 1) + "-" + data.get(i).title);
}
}
public void removedBooks() {
if (removed_data.size() == 0) {
System.out.println("No books removed yet..");
return;
}
for (int i = 0; i <= removed_data.size() - 1; i++) {
System.out.println(removed_data.get(i));
}
}
public void requestBook(int bookNumber) {
System.out.println("#=====================================");
System.out.println(data.get(bookNumber).title + " is issued to User");
data.remove(bookNumber);
System.out.println("#=====================================");
}
public void completeDetails(int bookNo) {
System.out.println("------------------------------------------------");
System.out.println("Complete details: ");
System.out.println("------------------------------------------------");
System.out.println("Title: " + data.get(bookNo).title);
System.out.println("Author: " + data.get(bookNo).author);
System.out.println("ISBN: " + data.get(bookNo).ISBN);
System.out.println("Price: Rs " + data.get(bookNo).price);
System.out.println("Publication: " + data.get(bookNo).publication);
System.out.println("Date Of Release: " + data.get(bookNo).dateOfRelease);
}
// starting the library function using multi threads
public void startLibrary() throws InterruptedException {
Scanner sc = new Scanner(System.in);
String ch = "";
do {
Thread.sleep(300);
System.out.println("#=====================================");
Thread.sleep(300);
System.out.println("Enter 1 to add a book. ");
Thread.sleep(300);
System.out.println("Enter 2 to remove a book. ");
Thread.sleep(300);
System.out.println("Enter 3 to update a book. ");
Thread.sleep(300);
System.out.println("Enter 4 to display all the books. ");
Thread.sleep(300);
System.out.println("Enter 5 to display new arrivals. ");
Thread.sleep(300);
System.out.println("Enter 6 to display removed books. ");
Thread.sleep(300);
System.out.println("Enter 7 to request a new book. ");
Thread.sleep(300);
System.out.println("Enter 8 to display details of particular book. ");
Thread.sleep(300);
System.out.println("Enter exit to logout or end the program. ");
Thread.sleep(300);
System.out.println("------------------------------------------------");
ch = sc.next();
switch (ch) {
case "1":
try {
if (data.size() == 15) {
FullRackException e = new FullRackException();
throw e;
}
addNewData();
System.out.println("BOOK added!");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("------------------------------------------------");
break;
case "2":
System.out.println("------------------------------------------------");
System.out.println("Enter the book number to remove ");
System.out.println("------------------------------------------------");
displayList();
System.out.println("------------------------------------------------");
int bookNos = sc.nextInt();
removeBook(bookNos);
System.out.println("BOOK removed!");
System.out.println("------------------------------------------------");
break;
case "3":
System.out.println("------------------------------------------------");
System.out.println("Enter the book number to update: ");
System.out.println("------------------------------------------------");
displayList();
System.out.println("------------------------------------------------");
int book = sc.nextInt();
update(data.get(book - 1).title);
System.out.println("------------------------------------------------");
break;
case "4":
System.out.println("------------------------------------------------");
System.out.println("List of available books: ");
System.out.println("------------------------------------------------");
displayList();
System.out.println("------------------------------------------------");
break;
case "5":
System.out.println("------------------------------------------------");
System.out.println("Newly arrived books: ");
System.out.println("------------------------------------------------");
newDisplayList();
System.out.println("------------------------------------------------");
break;
case "6":
System.out.println("------------------------------------------------");
System.out.println("Removed books: ");
System.out.println("------------------------------------------------");
removedBooks();
System.out.println("------------------------------------------------");
break;
case "7":
System.out.println("------------------------------------------------");
System.out.println("Select the book number from below to request : ");
System.out.println("------------------------------------------------");
displayList();
int bookNumber = sc.nextInt();
bookNumber--;
requestBook(bookNumber);
break;
case "8":
System.out.println("------------------------------------------------");
System.out.println("Enter the book number for complete details: ");
System.out.println("------------------------------------------------");
displayList();
int bookNo = sc.nextInt();
bookNo--;
completeDetails(bookNo);
System.out.println("------------------------------------------------");
break;
}
}
while (!ch.equals("exit"));
}
public static void main(String[] args) throws InterruptedException, FullRackException
{
Library l = new Library();
l.addToData();
Scanner sc = new Scanner(System.in);
System.out.println("--------LIBRARY MANAGEMENT SYSTEM-----------");
Thread2 t = new Thread2();
System.out.println("Enter the number of Users (upto 4): ");
int user = sc.nextInt();
if (user == 1) {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
catch (Exception e) {
System.out.println("Task done.");
}
finally {
System.out.println("USER LOGGED OUT");
System.exit(0);
}
executor.shutdownNow();
}
else if (user == 2) {
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
catch (Exception e) {
System.out.println("Task done.");
} finally
{
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
System.out.println("Next user logged in..Enter start to start program");
executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
catch (Exception e) {
System.out.println("Task done.");
}
finally {
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
} else if (user == 3) {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Task done.");
} finally {
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
System.out.println("Next user logged in..Enter start to start program");
executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Task done.");
}
finally {
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
System.out.println("Next user logged in..Enter start to start program");
executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
catch (Exception e) {
System.out.println("Task done.");
}
finally {
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
}
else if (user == 4) {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Task done.");
} finally {
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
System.out.println("Next user logged in..Enter start to start program");
executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Task done.");
} finally {
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
System.out.println("Next user logged in..Enter start to start program");
executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Task done.");
} finally {
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
System.out.println("Next user logged in..Enter start to start program");
executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Thread(t));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("Task done.");
} finally {
System.out.println("USER LOGGED OUT");
}
executor.shutdownNow();
} else {
System.out.println("Users cannot be more than 4");
}
}
}
class Thread2 extends Thread {
public synchronized void run() {
Library l= new Library();
try {
l.startLibrary();
}
catch (InterruptedException e) {
}
System.out.println("Thanks for using our library :)");
}
}