-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
57 lines (53 loc) · 1.82 KB
/
Library.java
File metadata and controls
57 lines (53 loc) · 1.82 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
import java.util.ArrayList;
import java.util.Iterator;
public class Library {
private ArrayList<Book> books;
public Library() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
this.books.add(book);
}
public Book findBook(String title) {
for (Book book : books) {
if(book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}
public void displayBooks() {
boolean found = false;
for (Book book : books) {
System.out.println("Title: " + book.getTitle() + " by " + book.getAuthor() +
" (Available: " + book.isAvailable() + ")");
found = true;
}
if (!found) {
System.out.println("No available books in the library");
}
}
public void removeBook(String title){
Iterator<Book> iterator = books.iterator();
while(iterator.hasNext()){
Book book = iterator.next();
if(book.getTitle().equalsIgnoreCase(title)) {
iterator.remove();
System.out.println("Book " + title + " has removed successfully");
return;
}
}
System.out.println("Book" + title + " is not available");
}
public void editBooks(String title , String NewTitle, String newAuthor, String newPublication){
Book book = findBook(title);
if(book != null){
book.setTitle(NewTitle);
book.setAuthor(newAuthor);
book.setPublication(newPublication);
System.out.println("Book " + title + " has edited successfully");
}else{
System.out.println("Book " + title + " is not available");
}
}
}