-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookManager.java
More file actions
73 lines (64 loc) · 2.58 KB
/
Copy pathBookManager.java
File metadata and controls
73 lines (64 loc) · 2.58 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
import java.util.ArrayList;
//Manager class for handling library books
public class BookManager {
// A list to hold all the books in the library
private ArrayList<LibraryBook> books;
private int nextBookId;
public BookManager() {
this.books = new ArrayList<>();
this.nextBookId = 1;
}
// Method to add a new book to the library
public void addBook(String title, String author, String section) {
LibraryBook newBook = new LibraryBook(nextBookId, title, author, section);
books.add(newBook);
nextBookId++; // Increment the ID for the next book
System.out.println("Successfully added: " + newBook.getBookTitle());
}
// Method to show all books in the library
public void showAllBooks() {
System.out.println("\n--- Library Inventory ---");
if (books.isEmpty()) {
System.out.println("The library is currently empty.");
} else {
for (LibraryBook book : books) {
System.out.println(book.toString());
}
}
System.out.println("-------------------------\n");
}
private LibraryBook findBookByTitle(String title) {
for (LibraryBook book : books) {
if (book.getBookTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null; // Return null if no book with that title is found
}
public void borrowBook(String title) {
LibraryBook book = findBookByTitle(title);
if (book != null) { // Check if the book exists
if (!book.isBorrowed()) { // Check if it's available
book.setBorrowed(true);
System.out.println("You have successfully borrowed '" + book.getBookTitle() + "'.");
} else {
System.out.println("Sorry, '" + book.getBookTitle() + "' is already borrowed.");
}
} else {
System.out.println("Sorry, no book with the title '" + title + "' was found.");
}
}
public void returnBook(String title) {
LibraryBook book = findBookByTitle(title);
if (book != null) { // Check if the book exists
if (book.isBorrowed()) { // Check if it was actually borrowed
book.setBorrowed(false);
System.out.println("Thank you for returning '" + book.getBookTitle() + "'.");
} else {
System.out.println("Error: This book was not borrowed.");
}
} else {
System.out.println("Sorry, no book with the title '" + title + "' was found.");
}
}
}