Repository navigation
Expand file tree
/
Copy pathLibrary.java
More file actions
99 lines (85 loc) · 3.33 KB
/
Copy pathLibrary.java
File metadata and controls
99 lines (85 loc) · 3.33 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
import java.util.ArrayList;
import java.util.List;
public class Library {
// The core data structure to hold all Book objects
private List<Book> books;
// Constructor initializes the ArrayList
public Library() {
this.books = new ArrayList<>();
}
// --- Core Feature 1: Adding a Book ---
public void addBook(Book book) {
// Simple check to ensure ISBN is unique before adding
for (Book existingBook : books) {
if (existingBook.getIsbn().equals(book.getIsbn())) {
System.out.println("Error: Book with ISBN " + book.getIsbn() + " already exists.");
return;
}
}
books.add(book);
System.out.println("Book added successfully: " + book.getTitle());
}
// --- Core Feature 2: Displaying All Books ---
public void displayAllBooks() {
if (books.isEmpty()) {
System.out.println("\nThe library catalog is empty. Please add some books first.");
return;
}
System.out.println("\n--- Library Catalog (" + books.size() + " Books) ---");
for (Book book : books) {
// The custom toString() method from the Book class is automatically used here
System.out.println(book);
}
System.out.println("------------------------------------");
}
// --- Core Feature 3: Searching for a Book ---
/**
* Searches the library for books matching the query in title or ISBN.
* @param query The search term (case-insensitive).
* @return A list of matching Book objects.
*/
public List<Book> searchBook(String query) {
List<Book> foundBooks = new ArrayList<>();
String lowerCaseQuery = query.toLowerCase();
for (Book book : books) {
// Check if title or ISBN contains the query
if (book.getTitle().toLowerCase().contains(lowerCaseQuery) ||
book.getIsbn().contains(lowerCaseQuery)) {
foundBooks.add(book);
}
}
return foundBooks;
}
// --- Core Feature 4: Issuing a Book ---
public boolean issueBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
if (!book.isIssued()) {
book.setIsIssued(true); // Update status via the setter
return true; // Success
} else {
System.out.println("Error: The book " + book.getTitle() + " is already issued.");
return false;
}
}
}
System.out.println("Error: Book with ISBN " + isbn + " not found.");
return false; // Book not found
}
// --- Core Feature 5: Returning a Book ---
public boolean returnBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
if (book.isIssued()) {
book.setIsIssued(false); // Update status via the setter
return true; // Success
} else {
System.out.println("Error: The book " + book.getTitle() + " was not issued.");
return false;
}
}
}
System.out.println("Error: Book with ISBN " + isbn + " not found.");
return false; // Book not found
}
}