-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathLibrary.java
More file actions
152 lines (123 loc) · 4.12 KB
/
Library.java
File metadata and controls
152 lines (123 loc) · 4.12 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
package LibraryBookTracker;
import java.util.ArrayList;
public class Library {
// Using ArrayList because the number of books can grow as users add more
private ArrayList<Book> books;
// This keeps track of the next ID to assign to a new book
private int nextBookId;
// Constructor creates the book list and starts IDs at 1
public Library() {
books = new ArrayList<>();
nextBookId = 1;
}
// Creates and adds a new book using the next available ID
public void addBook(String title, String author) {
Book newBook = new Book(nextBookId, title, author);
books.add(newBook);
nextBookId++;
System.out.println("Book added successfully.");
System.out.println(newBook);
}
// Displays every book in the system
public void displayAllBooks() {
if (books.isEmpty()) {
System.out.println("No books in the library.");
return;
}
System.out.println("\n--- All Books ---");
for (Book book : books) {
System.out.println(book);
}
}
// Displays only books that are currently available
public void displayAvailableBooks() {
boolean foundAvailable = false;
System.out.println("\n--- Available Books ---");
for (Book book : books) {
if (book.isAvailable()) {
System.out.println(book);
foundAvailable = true;
}
}
if (!foundAvailable) {
System.out.println("No available books right now.");
}
}
// Displays only books that are currently borrowed
public void displayBorrowedBooks() {
boolean foundBorrowed = false;
System.out.println("\n--- Borrowed Books ---");
for (Book book : books) {
if (!book.isAvailable()) {
System.out.println(book);
foundBorrowed = true;
}
}
if (!foundBorrowed) {
System.out.println("No borrowed books right now.");
}
}
// Lets the user borrow a book using its unique ID
public void borrowBookById(int id) {
Book book = findBookById(id);
if (book == null) {
System.out.println("Book not found.");
return;
}
if (!book.isAvailable()) {
System.out.println("That book is already borrowed.");
return;
}
book.setAvailable(false);
System.out.println("You borrowed: " + book.getTitle());
}
// Lets the user return a book using its unique ID
public void returnBookById(int id) {
Book book = findBookById(id);
if (book == null) {
System.out.println("Book not found.");
return;
}
if (book.isAvailable()) {
System.out.println("That book is already available.");
return;
}
book.setAvailable(true);
System.out.println("You returned: " + book.getTitle());
}
// Searches for a book by ID
public void searchBookById(int id) {
Book book = findBookById(id);
if (book == null) {
System.out.println("Book not found.");
} else {
System.out.println("Book found:");
System.out.println(book);
}
}
// Searches for books by title
// Using contains() makes searching more flexible than exact matching
public void searchBookByTitle(String title) {
boolean found = false;
System.out.println("\n--- Search Results ---");
for (Book book : books) {
if (book.getTitle().toLowerCase().contains(title.toLowerCase())) {
System.out.println(book);
found = true;
}
}
if (!found) {
System.out.println("No books matched that title.");
}
}
// Helper method used to find one book by its unique ID
// Keeping this logic in one place avoids repeated code
private Book findBookById(int id) {
for (Book book : books) {
if (book.getId() == id) {
return book;
}
}
return null;
}
}