-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.java
More file actions
461 lines (355 loc) · 15.8 KB
/
library.java
File metadata and controls
461 lines (355 loc) · 15.8 KB
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
package library;
import java.util.Scanner;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.ChronoUnit;
public class library {
//define an array books to store book objects
private book[] books;
private int noOfBooks; //no of books in the array
private int arrSize;
private int membArrSize;
private member[] members;
private borrowedBooks[] borrowedBooks;
private int noOfMembers;
private int noOfBorrowedBooks;
Scanner scanner = new Scanner(System.in);
//constructor arrsize is the maximum size of the array
public library(int arrSize){
this.arrSize = arrSize;
membArrSize=200; //there can be 200 members in the library
books = new book[arrSize]; //initialise the books array
noOfBooks = 0;
noOfMembers = 0;
noOfBorrowedBooks=0;
borrowedBooks = new borrowedBooks[arrSize];
members = new member[membArrSize];
}
public borrowedBooks[] getBorrowedBooks() {
return borrowedBooks;
}
public int getNoOfBorrowedBooks() {
return noOfBorrowedBooks;
}
public void setNoOfBorrowedBooks(int noOfBorrowedBooks) {
this.noOfBorrowedBooks = noOfBorrowedBooks;
}
public void setBorrowedBooks(borrowedBooks[] borrowedBooks) {
this.borrowedBooks = borrowedBooks;
}
public void setBooks(book[] books){
this.books = books;
}
public book[] getBooks(){
return books;
}
public void setMembers(member[] members){
this.members = members;
}
public member[] getMembers(){
return members;
}
public void setMembArrSize(int membArrSize){
this.membArrSize = membArrSize;
}
public int getMembArrSize(){
return membArrSize;
}
public void registerMembers(){
//store member information and call member function
System.out.println("enter member name:");
String name = scanner.nextLine();
System.out.println("enter member ID:");
int membID = scanner.nextInt();
if(noOfMembers<getMembArrSize()){
members[noOfMembers] = new member("",0);
getMembers()[noOfMembers].setName(name);
getMembers()[noOfMembers].setMembID(membID);
noOfMembers++;
System.out.println("member registered.");
}
}
public void addBooks(){
// scanner.nextLine();
int ID=0;
String title="";
String author="";
// for(int i=1;i<noOfBooks;i++) {
if(noOfBooks<arrSize){
System.out.println("enter book id");
ID = scanner.nextInt();
scanner.nextLine();
System.out.println("enter book title:");
title = scanner.nextLine();
System.out.println("enter author name");
author = scanner.nextLine();
//unless the array size is full
book newBook = new book("",0,"");
books[noOfBooks] = newBook; //book object as the last array item
newBook.setAuthor(author);
newBook.setID(ID);
newBook.setTitle(title);
noOfBooks++;
System.out.println("Number of books in the library: " + noOfBooks);
}
else{
System.out.println("the library is full, can not add more books");
}
}
public void removeBooks(String title) {
boolean bookFound = false;
for (int k = 0; k < noOfBooks; k++) { //enter the books array and access its books
if (books[k].getTitle().equals(title)) { //check whether the titles are equal
//shift elements to the left
for (int j = k; j < noOfBooks - 1; j++) {
books[j] = books[j + 1]; //shift the books' elements by 1 to the left
}
System.out.println("the book is found and removed.");
//set the last element to null
bookFound = true;
noOfBooks--;
break;
}
}
if(bookFound == false){
System.out.println("book not found");
}
}
public void removeMembers(String memberName) {
boolean memberFound =false;
for (int k = 0; k < noOfMembers; k++) {
if (members[k].getName().equals(memberName)) {
//shift elements to the left
for (int j = k; j < noOfMembers - 1; j++) {
members[j] = members[j + 1];
}
System.out.println("the member was removed.");
memberFound = true;
noOfMembers--;
break;
}
}
if(memberFound == false){
System.out.println("member not found");
}
}
public void displayBooks() {
if (books != null && noOfBooks > 0) {
System.out.println("Number of books in the library: " + noOfBooks); // Debug line
for (int j = 0; j < noOfBooks; j++) {
System.out.println("now i am going to display the books");
System.out.println("the name of the ID is:" + getBooks()[j].getID());
System.out.println("the name of the title is:" + getBooks()[j].getTitle());
System.out.println("the name of the author is:" + getBooks()[j].getAuthor());
}
}
else {
System.out.println("There are no books in the library.");
}
}
public void displayMembers() {
if (members != null && noOfMembers > 0) {
System.out.println("Number of Members in the library: " + noOfMembers); // Debug line
for (int j = 0; j < noOfMembers; j++) {
System.out.println("the id of the member is:" + getMembers()[j].getMembID());
System.out.println("the name of the member is:" + getMembers()[j].getName());
}
}
else {
System.out.println("There are no members in the library.");
}
}
public void searchBooks(String title){
boolean bookFound = false;
for(int m=0;m<noOfBooks;m++){
if(getBooks()[m].getTitle().equals(title)){
// System.out.println("book found entered title equals loop");
System.out.println("the book title is:"+getBooks()[m].getTitle());
System.out.println("the book ID is:"+getBooks()[m].getID());
System.out.println("the book author is:"+getBooks()[m].getAuthor());
bookFound=true;
break;
}
}
if(bookFound==false){
System.out.println("No book was found with that name");
}
}
//check if a a person is a
// public boolean isABook(String bookTitle){
//
// }
public boolean isAMember(String personName){
for(int i=0;i<noOfMembers;i++){
if(getMembers()[i].getName().equals(personName)){
return true;
}
}
return false;
}
public boolean hasBorrowed(String title){
for(int i=0;i<noOfBorrowedBooks;i++){
if(borrowedBooks[i].getTitle().equals(title)){
return true;
}
}
return false;
}
public boolean hasBorrowedMember(String membName){
for(int i=0;i<noOfBorrowedBooks;i++){
if(borrowedBooks[i].getBorrowedBy().equals(membName)){
return true;
}
}
return false;
}
public void lendBooks(String membName, String title){
boolean bookFound = false;
// LocalDate currentDate = LocalDate.now();
// System.out.println("debug line:"+currentDate);
//lend the book to the member.check if the person lending is a member or not
//make a isamember function
if(isAMember(membName)){
System.out.println("member found.");
for(int k=0;k<noOfBooks;k++){
if(books[k].getTitle().equals(title)){
// System.out.println("the book is found in store");
if(!hasBorrowed(title)){
System.out.println("the book is available");
if (borrowedBooks == null) {
borrowedBooks = new borrowedBooks[0];
}
borrowedBooks newborrowedBook = new borrowedBooks("","");
borrowedBooks[noOfBorrowedBooks]= newborrowedBook;
newborrowedBook.setBorrowedBy(membName);
newborrowedBook.setTitle(title);
noOfBorrowedBooks++;
System.out.println("no of borrowed books ="+noOfBorrowedBooks);
bookFound = true;
break;
}
System.out.println("sorry,the book has been borrowed. Check again later.");
}
}
if(bookFound==false){
System.out.println("sorry, no such book exist in the library.");
}
}
else {
System.out.println("you must register in the library to borrow a book.");
}
}
void returnBooks(){
boolean flag = false;
System.out.println("enter member name:");
String returningMember = scanner.next();
if(hasBorrowedMember(returningMember)) {
System.out.println("enter book name:");
String returnBook = scanner.next();
for (int i = 0; i < noOfBorrowedBooks; i++) {
if (borrowedBooks[i].getTitle().equals(returnBook)) {
flag=true;
for (int j = i; j < noOfBorrowedBooks - 1; j++) {
borrowedBooks[j] = borrowedBooks[j + 1];
}
noOfBorrowedBooks--;
System.out.println("thank you! The book has been returned!");
}
}
if(flag==false){
System.out.println("invalid book, try again!");
}
}
else {
System.out.println("Sorry, there's no borrowed book under this name");
}
}
public void viewLendingInfo(){
boolean flag = false;
System.out.println("enter name to get lending information of the member:");
String lenderName = scanner.next();
// System.out.println("enter book title to get the lending history of the book");
// String bookName = scanner.nextLine();
if(isAMember(lenderName)){
if (borrowedBooks.length > 0) {
for (int i = 0; i < noOfBorrowedBooks; i++) {
if (borrowedBooks[i].getBorrowedBy().equals(lenderName)) {
System.out.println("book name:"+borrowedBooks[i].getTitle());
System.out.println("the member borrowed:" + borrowedBooks[i].getBorrowedBy());
System.out.println("the date borrowed:" + borrowedBooks[i].getBorrowedDate());
System.out.println("the due date:" + borrowedBooks[i].getDueDate());
flag=true;
}
}
if(flag==false){
System.out.println("the member does not have any borrowed books");
}
}
else{
System.out.println("no books have been borrowed");
}
}
else{
System.out.println("the member not found");
}
}
public void viewOverDueBooks(){
System.out.println("enter to name to see over due books:");
String lenderName = scanner.next();
if(isAMember(lenderName)){
for(int i=0;i<noOfBorrowedBooks;i++){
if(borrowedBooks[i].getBorrowedBy().equals(lenderName)){
if(borrowedBooks[i].isOverDue()){
System.out.println("book title "+borrowedBooks[i].getTitle());
System.out.println("borrowed by:"+borrowedBooks[i].getBorrowedBy());
System.out.println("borrowed Date:"+borrowedBooks[i].getBorrowedDate());
System.out.println("Due date:"+borrowedBooks[i].getDueDate());
}
else{
System.out.println("no overdue books under your name.");
}
}
}
}
else{
System.out.println("you are not a member");
}
}
public void calculateFine(){
double fine = 0.0;
LocalDate currentDate = LocalDate.now();
LocalDate dueDate;
System.out.println("enter name of the member to calculate your fines:");
String lenderName = scanner.next();
for(int i=0; i<noOfBorrowedBooks;i++) {
if (borrowedBooks[i].getBorrowedBy().equals(lenderName)) {
if(borrowedBooks[i].isOverDue()){
System.out.println("debug line entered is over due");
dueDate = borrowedBooks[i].getDueDate();
if(dueDate.plusWeeks(1).isAfter(currentDate)) {
// System.out.println("debug line:current date<due date+7");
long daysInBetween = ChronoUnit.DAYS.between(dueDate,currentDate);
int daysBetween = (int)daysInBetween; //explicit casting
fine = 50.0*daysBetween;
// System.out.println("your fine is:"+fine);
}
else{
// System.out.println("debug line:current date>due date+7");
LocalDate startOfSecondWeek = dueDate.plusWeeks(1);
long secondWeek = ChronoUnit.DAYS.between(startOfSecondWeek, currentDate);
int daysInSecondWeek = (int) secondWeek;
fine = (50.0*7) + daysInSecondWeek*100;
// System.out.println("your fine is");
}
System.out.println("your fine is:"+fine);
}
else{
System.out.println("the book is not overdue");
}
}
else{
System.out.println("you have not borrowed any book");
}
}
}
}