Skip to content

Commit 31f0445

Browse files
committed
Implement Solution
1 parent 0fced0c commit 31f0445

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

Author.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public record Author(String Name) {
2+
3+
}

Book.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public record Book(String title) {
2+
3+
4+
}

BookCollection.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

DuplicateKeyException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class DuplicateKeyException extends Exception {
2+
3+
private static final long serialVersionUID = 1L;
4+
5+
}

Exercise.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1+
import java.util.HashMap;
2+
13
public 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
}

0 commit comments

Comments
 (0)