66
77public record BookCollection (HashMap <Author , List <Book >> collection ) {
88
9- public void addAuthor (Author author ) throws DuplicateKeyException {
10- if (collection .containsKey (author )) {
11- throw new DuplicateKeyException ();
12- }
13-
14- collection .put (author , new ArrayList <>());
9+ public void addAuthor (Author author ) throws DuplicateKeyException {
10+ if (collection .containsKey (author )) {
11+ throw new DuplicateKeyException ();
1512 }
1613
17- public void addBook (Author author , Book book ) {
18- collection .get (author ).add (book );
19- }
14+ collection .put (author , new ArrayList <>());
15+ }
2016
21- public Optional <Book > getBookByTitle (String title ) {
22- for (List <Book > books : collection .values ()) {
23- for (Book b : books ) {
24- if (b .title ().equals (title )) {
25- return Optional .of (b );
26- }
27- }
17+ public void addBook (Author author , Book book ) {
18+ collection .get (author ).add (book );
19+ }
20+
21+ public Optional <Book > getBookByTitle (String title ) {
22+ for (List <Book > books : collection .values ()) {
23+ for (Book b : books ) {
24+ if (b .title ().equals (title )) {
25+ return Optional .of (b );
2826 }
29- return Optional . empty ();
27+ }
3028 }
29+ return Optional .empty ();
30+ }
3131
32- public Optional <Author > getMostDiligentAuthor () {
33- Author mostDiligentAuthor = null ;
34- int mostBooks = 0 ;
35- for (Entry <Author , List <Book >> entry : collection .entrySet ()) {
36- if (entry .getValue ().size () > mostBooks ) {
37- mostDiligentAuthor = entry .getKey ();
38- mostBooks = entry .getValue ().size ();
39- }
40- }
41- return Optional .ofNullable (mostDiligentAuthor );
32+ public Optional <Author > getMostDiligentAuthor () {
33+ Author mostDiligentAuthor = null ;
34+ int mostBooks = 0 ;
35+ for (Entry <Author , List <Book >> entry : collection .entrySet ()) {
36+ if (entry .getValue ().size () > mostBooks ) {
37+ mostDiligentAuthor = entry .getKey ();
38+ mostBooks = entry .getValue ().size ();
39+ }
4240 }
43- }
41+ return Optional .ofNullable (mostDiligentAuthor );
42+ }
43+ }
0 commit comments