forked from Daoud-Hussain/University-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Library.java
418 lines (346 loc) · 14.5 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
//Importing all required packages
import java.util.Scanner;
import java.util.Random;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;
import java.io.*;
import library.*;
public class Library {
//Initializing and declaring required variables and arrays
static Scanner input = new Scanner(System.in);
static String[] borrowedbooks;
static int[] borrowedBooksIndexes;
static String[] books = new String[20];
static int totalBooks=0;
static int userChoice;
static int dateformater;
static String borrowedDate;
static String returnedDate;
public static void main(String regNumber) {
Arrays.fill(books,"");
boolean flag = true;
while(flag){
//Displaying main menu
System.out.println("******************************************");
System.out.println("Welcome to Library Managament System!");
System.out.println("Enter 1 to search books ");
System.out.println("Enter 2 to read membership criteria ");
System.out.println("Enter 3 to borrow books ");
System.out.println("Enter 4 to return books ");
System.out.println("Enter 5 to previous menu");
System.out.println("Enter 6 to exit");
System.out.println("******************************************");
//Taking user choices
try{
System.out.print("Enter your choice: ");
userChoice = input.nextInt();
if (userChoice == 3){
library.TermsAndConditions.displayTerms();
System.out.print("Do you accept the terms Press 1 to accept and 2 to reject: ");
int option = input.nextInt();
if(option==1){
borrowBook(regNumber);
}
else if(option==2){
System.out.println("You must have to accept terms and condition, otherwise you can't buy any book!.");
}
else{
System.out.print("Invalid Input! \n");
input.nextLine();
}
}
else if (userChoice == 4){
System.out.print("");
returnBook(regNumber);
}
else if(userChoice == 2){
System.out.print("");
library.TermsAndConditions.displayTerms();
}
else if (userChoice == 1){
searchBook();
}
else if(userChoice == 5){
LibraryManagement.manage(regNumber);
}
else if(userChoice == 6){
System.exit(0);
}
else{
System.out.println("Invalid Input! ");
}
}
catch(Exception e){
System.out.println("Invalid Input Try again!\n");
input.nextLine();
}
}
}
//Book borrowing module
public static void borrowBook(String regNumber) {
System.out.println("*************************************************");
System.out.println("Available books are: ");
try{
File file = new File("Library/AvailableBooks.txt");
Scanner sc = new Scanner(file);
int j=1;
while(sc.hasNextLine()){
String line = sc.nextLine();
if(line!=""){
books[j-1] = line;
System.out.println(j+". "+line);
// j++;
totalBooks++;
}
j++;
}
sc.close();
System.out.println("*************************************************");
}
catch(Exception e){
System.out.println("Error occurred!");
}
if(totalBooks==0){
System.out.println("!!!!!!! No Record Found !!!!!!!\n");
}
else{
boolean flag = false;
while(true){
try{
//TAking number of books purchased from user
Scanner input = new Scanner(System.in);
System.out.print("How many books you want to borrow: ");
int numberOfPurchasedBooks = input.nextInt();
borrowedbooks = new String[numberOfPurchasedBooks];
borrowedBooksIndexes = new int[numberOfPurchasedBooks + 1];
//Validating the input
if(numberOfPurchasedBooks <= totalBooks && numberOfPurchasedBooks > 0){
for(int i=0; i<numberOfPurchasedBooks; i++) {
System.out.println("*************************************");
System.out.printf("Record Book %d \n", i+1);
int previousBook = 0;
System.out.print("Which book you want to borrow, Enter its number: ");
int bookNumber = input.nextInt();
try{
if(bookNumber<=20 && bookNumber > 0){
//Displaying available books
File ff = new File("Library/AvailableBooks.txt");
Scanner sc = new Scanner(ff);
int index = 1;
while(sc.hasNextLine()){
String line = sc.nextLine();
if(index==bookNumber){
borrowedbooks[i] = line;
if(bookNumber==previousBook){
System.out.println("This book is booked. Try Any other");
}
previousBook= bookNumber;
break;
}
index++;
}
sc.close();
}
else{
System.out.println("Invalid Book Number");
i-=1;
}
}
catch(Exception ex){
System.out.println("Invalid Input! Try Again");
i-=1;
}
}
System.out.println("Successfully Borrowed " + numberOfPurchasedBooks+ " books");
flag = true;
}
else{
System.out.print("Invalid Input! Only books "+totalBooks+ " are available.\n");
}
}
catch(Exception e){
System.out.print("Invalid input! Try Again\n");
}
if(flag){
break;
}
}
//Calcualating date of borrowing
try{
SimpleDateFormat dateAndTimeFormatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
borrowedDate = dateAndTimeFormatter.format(date);
File myFile = new File("Library/BorrowedBooks/"+regNumber+".txt");
boolean value = myFile.createNewFile();
FileWriter fileWriter = new FileWriter("Library/BorrowedBooks/"+regNumber+".txt",true);
fileWriter.write("Borrowed at: " + dateAndTimeFormatter.format(date) + "\n");
for(String book : borrowedbooks){
fileWriter.write(book+"\n");
}
fileWriter.close();
}
catch(Exception e){
System.out.print(e+"\n");
}
for(int n=0;n<books.length;n++){
for(int m=0;m<borrowedbooks.length;m++){
if(borrowedbooks[m].compareTo(books[n])==0){
books[n] = "";
break;
}
}
}
//Writing available file as empty
try{
File myFF = new File("Library/AvailableBooks.txt");
FileWriter fileWriter = new FileWriter(myFF);
fileWriter.write("");
fileWriter.close();
}
catch(Exception e){
System.out.print(e+"\n");
}
//Creating new file with same name to store available books after borrowing
try{
File myFF = new File("Library/AvailableBooks.txt");
boolean value = myFF.createNewFile();
FileWriter fileWriter = new FileWriter(myFF,true);
for(String bk:books){
if(bk!=""){
fileWriter.write(bk+"\n");
}
}
fileWriter.close();
}
catch(Exception e){
System.out.print(e+"\n");
}
Arrays.fill(borrowedbooks,"");
Arrays.fill(books,"");
totalBooks=0;
}
}
// Book returning module
public static void returnBook(String regNumber){
//Checking date to return book
SimpleDateFormat formatter2 = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
returnedDate = formatter2.format(date);
int choice=0;
try{
//Displaying borrowed books
File fileObj = new File("Library/BorrowedBooks/"+regNumber+".txt");
if(fileObj.exists()){
try{
System.out.print("\nYou have borrowed following books : \n");
File file = new File("Library/BorrowedBooks/"+regNumber+".txt");
Scanner data = new Scanner(file);
int i=0;
while(data.hasNextLine()){
String line = data.nextLine();
System.out.print(line+"\n");
books[i] = line;
i++;
}
data.close();
}
catch(Exception e){
}
// choice Menu
System.out.print("\n************************************\n");
System.out.print("\n1. Return books\n");
System.out.print("2. Previous menu\n");
boolean flag = false;
while(true){
try{
System.out.print("Enter your choice : ");
choice = input.nextInt();
if(choice==1){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = sdf.parse(borrowedDate);
Date date2 = sdf.parse(returnedDate);
System.out.println("Borrowed date : " + sdf.format(date1));
System.out.println("Returned date : " + sdf.format(date2));
int result = date1.compareTo(date2);
if (result == 0) {
System.out.println("No fine is declared to " + regNumber);
} else if (result > 30) { //Declaring fine for late submission
int fine = 50;
System.out.println("You have to pay 50 rupees fine per day!");
} else {
System.out.println("Correct your calendar");
}
flag = true;
FileWriter writer = new FileWriter("Library/AvailableBooks.txt",true);
for(int a=0;a<books.length;a++){
writer.write(books[a]+"\n");
}
writer.close();
File myObj = new File("Library/BorrowedBooks/"+regNumber+".txt");
if(myObj.delete()){
System.out.println("Books has been returned Successfully!");
}
else{
System.out.println("Some error occured in deleting your data!\n");
}
}
else if(choice==2){
flag = true;
System.out.println("Return to previous menu..");
}
else{
System.out.println("Invalid choice... Try again!\n");
}
}
catch(Exception e){
System.out.println("Invalid choice... Try again!"+"\n");
input.nextLine();
}
if(flag){
break;
}
}
}
else{
System.out.println("You have not borrowed any book!\n");
}
}
catch(Exception e){
System.out.println("Error Occurred!");
}
}
//Book searching module
public static String searchBook(){
boolean process = false;
Scanner input = new Scanner(System.in);
//Taking book name from user
System.out.print("Enter book name you want to search: ");
String searchBookName = input.nextLine().toLowerCase();
String line= " ";
try{
//Specifying file to search book
File file = new File("Library/AvailableBooks.txt");
Scanner console = new Scanner(file);
//Searching book name from available files
int i=0;
while(console.hasNextLine()){
line = console.nextLine().toLowerCase();
if(line.contains(searchBookName)){
System.out.println(i+1 +". " + line);
process = true;
}
i++;
}
}
catch(Exception e){
System.out.println("Any Error while reading the Available file");
input.nextLine();
}
//No matched input case
if(!process){
System.out.printf("No book available with name \"%s\" Try Any other! \n", searchBookName);
}
return line;
}
}