-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary.java
More file actions
61 lines (50 loc) · 1.47 KB
/
Copy pathlibrary.java
File metadata and controls
61 lines (50 loc) · 1.47 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
package com.javabooks;
import java.util.*;
import java.util.stream.Collectors;
public class Library implements LibraryService {
private List<Book> books = new ArrayList<>();
@Override
public void addBook(Book book) {
books.add(book);
}
@Override
public void removeBook(String isbn) {
books.removeIf(b -> b.getIsbn().equals(isbn));
}
@Override
public List<Book> listBooks() {
return new ArrayList<>(books);
}
@Override
public List<Book> searchByTitle(String title) {
return books.stream()
.filter(b -> b.getTitle().toLowerCase().contains(title.toLowerCase()))
.collect(Collectors.toList());
}
@Override
public List<Book> searchByAuthor(String author) {
return books.stream()
.filter(b -> b.getAuthor().toLowerCase().contains(author.toLowerCase()))
.collect(Collectors.toList());
}
@Override
public Book findByIsbn(String isbn) {
return books.stream()
.filter(b -> b.getIsbn().equals(isbn))
.findFirst().orElse(null);
}
@Override
public void borrowBook(String isbn) {
Book b = findByIsbn(isbn);
if (b != null && !b.isBorrowed()) {
b.borrow();
}
}
@Override
public void returnBook(String isbn) {
Book b = findByIsbn(isbn);
if (b != null && b.isBorrowed()) {
b.returnBook();
}
}
}