-
Notifications
You must be signed in to change notification settings - Fork 1
/
Library.java
66 lines (63 loc) · 1.84 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
public class Library {
private Book books[] = new Book[150];
private Student student[] = new Student[100];
public Library(Book[] books, Student[] student) {
this.books = books;
this.student = student;
}
public void displayBooks(){
for(int i=0;i<books.length;i++){
if(books[i].title!=null) {
books[i].display();
}
}
}
private Book findBook(String title) {
for (int i = 0; i < books.length; i++) {
if (books[i].getTitle()== title) {
return books[i];
}
}
return null;
}
public void borrowBook(String title, Student student) {
Book book = findBook(title);
if (book != null && book.numCopies !=0 ) {
book.borrow();
student.borrowBook(book);
} else {
System.out.println("Book not found.");
}
}
public void addStudent(Student newStudent){
for(int i=0;i<student.length;i++){
if(student[i]==null) {
student[i]= newStudent;
}
}
}
public void addBook(Book newBook){
Book book = findBook(newBook.title);
if(book != null) {
book.numCopies++;
}
else{
for(int i=0;i<books.length;i++){
if(books[i].title==null) {
books[i]= newBook;
break;
}
}
}
}
public void returnBook(String title , Student student){
Book book = findBook(title);
if (book != null) {
book.returnBook();
student.returnBook(book);
}
else {
System.out.println("Library Error : the Book have not been issued \n\n");
}
}
}