-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
103 lines (87 loc) · 2.75 KB
/
Copy pathLibrary.java
File metadata and controls
103 lines (87 loc) · 2.75 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
public class Library {
private Book[] books;
private int count;
public Library(int size){
books = new Book[size];
count = 0;
}
public void addBook(Book book){
if(count>= books.length) {
System.out.println("There is no more books in the library");
return;
}
books[count] = book;
count++;
System.out.println("Book" + book.getId() +" Added Successfullly");
}
public void showBook(){
System.out.println("==== Books ====");
for(int i = 0; i<count; i++){
System.out.println("ID: " + books[i].getId() );
System.out.println("Title: " + books[i].getTitle() );
System.out.println("Author: " + books[i].getAuthor() );
System.out.println("Available: " + (books[i].isAvailable() ? "Yes" : "No") );
System.out.println("------------");
}
}
public Book searchBook(int id){
for(int i = 0; i<count; i++){
if(books[i].getId() == id) return books[i];
}
return null;
}
public Book searchBook(String title){
for(int i = 0; i<count; i++){
if(books[i].getTitle().equals(title)) return books[i];
}
return null;
}
public boolean borrowBook(int id){
Book book = searchBook(id);
if(book == null) {
System.out.println("Book not Found");
return false;
}
else if (book.isAvailable()) {
book.setAvailable(false);
System.out.println("Book Borrowed Successfully");
return true;
}
else {
System.out.println("Book is already borrowed");
return false;
}
}
public boolean returnBook(int id){
Book book = searchBook(id);
if(book == null) {
System.out.println("Book not found");
return false;
}
else if(book.isAvailable()) {
System.out.println("Book never borrowed");
return false;
}
else {
book.setAvailable(true);
System.out.println("Book Returned Successfully");
return true;
}
}
public boolean removeBook(int id){
Book book = searchBook(id);
if(book == null) {
System.out.println("Book not found");
return false;
}
else {
count = count - 1;
book.setId(0);
book.setAvailable(null);
book.setAuthor(null);
book.setTitle(null);
System.out.println("Book removed Successfully");
return true;
}
}
}