File tree Expand file tree Collapse file tree 5 files changed +79
-1
lines changed
Expand file tree Collapse file tree 5 files changed +79
-1
lines changed Original file line number Diff line number Diff line change 1+ public record Author (String Name ) {
2+
3+ }
Original file line number Diff line number Diff line change 1+ public record Book (String title ) {
2+
3+
4+ }
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .HashMap ;
3+ import java .util .List ;
4+ import java .util .Map .Entry ;
5+ import java .util .Optional ;
6+
7+ public record BookCollection (HashMap <Author , List <Book >> collection ) {
8+
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 <>());
15+ }
16+
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 );
26+ }
27+ }
28+ }
29+ return Optional .empty ();
30+ }
31+
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 );
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ public class DuplicateKeyException extends Exception {
2+
3+ private static final long serialVersionUID = 1L ;
4+
5+ }
Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+
13public class Exercise {
24
35 public static void main (String [] args ) {
4- // implement exercise here
6+ BookCollection collection = new BookCollection (new HashMap <>());
7+
8+ try {
9+ collection .addAuthor (new Author ("Stephen King" ));
10+ collection .addAuthor (new Author ("George RR Martin" ));
11+ } catch (DuplicateKeyException e ) {
12+ e .printStackTrace ();
13+ }
14+
15+ collection .addBook (new Author ("Stephen King" ), new Book ("Es" ));
16+ collection .addBook (new Author ("Stephen King" ), new Book ("Sie" ));
17+ collection .addBook (new Author ("George RR Martin" ), new Book ("Das Lied von Eis und Feuer 1" ));
18+ collection .addBook (new Author ("George RR Martin" ), new Book ("Das Lied von Eis und Feuer 2" ));
19+ collection .addBook (new Author ("George RR Martin" ), new Book ("Das Lied von Eis und Feuer 3" ));
20+ collection .addBook (new Author ("George RR Martin" ), new Book ("Das Lied von Eis und Feuer 4" ));
21+ collection .addBook (new Author ("George RR Martin" ), new Book ("Das Lied von Eis und Feuer 5" ));
22+ collection .addBook (new Author ("George RR Martin" ), new Book ("Das Lied von Eis und Feuer 6" ));
23+
24+ System .out .println (collection .getBookByTitle ("Das Lied von Eis und Feuer 5" ));
25+ System .out .println (collection .getMostDiligentAuthor ());
26+
527 }
28+
629}
You can’t perform that action at this time.
0 commit comments