File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .javabooks ;
2+
3+ import java .util .*;
4+ import java .util .stream .Collectors ;
5+
6+ public class Library implements LibraryService {
7+
8+ private List <Book > books = new ArrayList <>();
9+
10+ @ Override
11+ public void addBook (Book book ) {
12+ books .add (book );
13+ }
14+
15+ @ Override
16+ public void removeBook (String isbn ) {
17+ books .removeIf (b -> b .getIsbn ().equals (isbn ));
18+ }
19+
20+ @ Override
21+ public List <Book > listBooks () {
22+ return new ArrayList <>(books );
23+ }
24+
25+ @ Override
26+ public List <Book > searchByTitle (String title ) {
27+ return books .stream ()
28+ .filter (b -> b .getTitle ().toLowerCase ().contains (title .toLowerCase ()))
29+ .collect (Collectors .toList ());
30+ }
31+
32+ @ Override
33+ public List <Book > searchByAuthor (String author ) {
34+ return books .stream ()
35+ .filter (b -> b .getAuthor ().toLowerCase ().contains (author .toLowerCase ()))
36+ .collect (Collectors .toList ());
37+ }
38+
39+ @ Override
40+ public Book findByIsbn (String isbn ) {
41+ return books .stream ()
42+ .filter (b -> b .getIsbn ().equals (isbn ))
43+ .findFirst ().orElse (null );
44+ }
45+
46+ @ Override
47+ public void borrowBook (String isbn ) {
48+ Book b = findByIsbn (isbn );
49+ if (b != null && !b .isBorrowed ()) {
50+ b .borrow ();
51+ }
52+ }
53+
54+ @ Override
55+ public void returnBook (String isbn ) {
56+ Book b = findByIsbn (isbn );
57+ if (b != null && b .isBorrowed ()) {
58+ b .returnBook ();
59+ }
60+ }
61+ }
You can’t perform that action at this time.
0 commit comments