From 31f0445ded9cf3cdd94539643171b2c02e94984e Mon Sep 17 00:00:00 2001 From: SteffenLm <33038091+SteffenLm@users.noreply.github.com> Date: Wed, 23 Aug 2023 17:42:07 +0200 Subject: [PATCH 1/5] Implement Solution --- Author.java | 3 +++ Book.java | 4 ++++ BookCollection.java | 43 ++++++++++++++++++++++++++++++++++++++ DuplicateKeyException.java | 5 +++++ Exercise.java | 25 +++++++++++++++++++++- 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 Author.java create mode 100644 Book.java create mode 100644 BookCollection.java create mode 100644 DuplicateKeyException.java diff --git a/Author.java b/Author.java new file mode 100644 index 0000000..e988398 --- /dev/null +++ b/Author.java @@ -0,0 +1,3 @@ +public record Author(String Name) { + +} \ No newline at end of file diff --git a/Book.java b/Book.java new file mode 100644 index 0000000..f34ef89 --- /dev/null +++ b/Book.java @@ -0,0 +1,4 @@ +public record Book(String title) { + + +} \ No newline at end of file diff --git a/BookCollection.java b/BookCollection.java new file mode 100644 index 0000000..14aa644 --- /dev/null +++ b/BookCollection.java @@ -0,0 +1,43 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.Optional; + +public record BookCollection(HashMap> collection) { + + public void addAuthor(Author author) throws DuplicateKeyException { + if (collection.containsKey(author)) { + throw new DuplicateKeyException(); + } + + collection.put(author, new ArrayList<>()); + } + + public void addBook(Author author, Book book) { + collection.get(author).add(book); + } + + public Optional getBookByTitle(String title) { + for (List books : collection.values()) { + for (Book b : books) { + if (b.title().equals(title)) { + return Optional.of(b); + } + } + } + return Optional.empty(); + } + + public Optional getMostDiligentAuthor() { + Author mostDiligentAuthor = null; + int mostBooks = 0; + for (Entry> entry : collection.entrySet()) { + if (entry.getValue().size() > mostBooks) { + mostDiligentAuthor = entry.getKey(); + mostBooks = entry.getValue().size(); + } + } + return Optional.ofNullable(mostDiligentAuthor); + } +} \ No newline at end of file diff --git a/DuplicateKeyException.java b/DuplicateKeyException.java new file mode 100644 index 0000000..50f898b --- /dev/null +++ b/DuplicateKeyException.java @@ -0,0 +1,5 @@ +public class DuplicateKeyException extends Exception { + + private static final long serialVersionUID = 1L; + +} \ No newline at end of file diff --git a/Exercise.java b/Exercise.java index 3c092f9..4b4c209 100644 --- a/Exercise.java +++ b/Exercise.java @@ -1,6 +1,29 @@ +import java.util.HashMap; + public class Exercise { public static void main(String[] args) { - // implement exercise here + BookCollection collection = new BookCollection(new HashMap<>()); + + try { + collection.addAuthor(new Author("Stephen King")); + collection.addAuthor(new Author("George RR Martin")); + } catch (DuplicateKeyException e) { + e.printStackTrace(); + } + + collection.addBook(new Author("Stephen King"), new Book("Es")); + collection.addBook(new Author("Stephen King"), new Book("Sie")); + collection.addBook(new Author("George RR Martin"), new Book("Das Lied von Eis und Feuer 1")); + collection.addBook(new Author("George RR Martin"), new Book("Das Lied von Eis und Feuer 2")); + collection.addBook(new Author("George RR Martin"), new Book("Das Lied von Eis und Feuer 3")); + collection.addBook(new Author("George RR Martin"), new Book("Das Lied von Eis und Feuer 4")); + collection.addBook(new Author("George RR Martin"), new Book("Das Lied von Eis und Feuer 5")); + collection.addBook(new Author("George RR Martin"), new Book("Das Lied von Eis und Feuer 6")); + + System.out.println(collection.getBookByTitle("Das Lied von Eis und Feuer 5")); + System.out.println(collection.getMostDiligentAuthor()); + } + } From f625ac75cac689c3d4b5fa04a51addc979dd7a2f Mon Sep 17 00:00:00 2001 From: github-actions <> Date: Wed, 23 Aug 2023 15:42:55 +0000 Subject: [PATCH 2/5] Google Java Format --- Author.java | 4 +-- Book.java | 5 +--- BookCollection.java | 56 +++++++++++++++++++------------------- DuplicateKeyException.java | 5 ++-- Exercise.java | 2 -- 5 files changed, 32 insertions(+), 40 deletions(-) diff --git a/Author.java b/Author.java index e988398..9694c0e 100644 --- a/Author.java +++ b/Author.java @@ -1,3 +1 @@ -public record Author(String Name) { - -} \ No newline at end of file +public record Author(String Name) {} diff --git a/Book.java b/Book.java index f34ef89..19ad242 100644 --- a/Book.java +++ b/Book.java @@ -1,4 +1 @@ -public record Book(String title) { - - -} \ No newline at end of file +public record Book(String title) {} diff --git a/BookCollection.java b/BookCollection.java index 14aa644..b7a725d 100644 --- a/BookCollection.java +++ b/BookCollection.java @@ -6,38 +6,38 @@ public record BookCollection(HashMap> collection) { - public void addAuthor(Author author) throws DuplicateKeyException { - if (collection.containsKey(author)) { - throw new DuplicateKeyException(); - } - - collection.put(author, new ArrayList<>()); + public void addAuthor(Author author) throws DuplicateKeyException { + if (collection.containsKey(author)) { + throw new DuplicateKeyException(); } - public void addBook(Author author, Book book) { - collection.get(author).add(book); - } + collection.put(author, new ArrayList<>()); + } - public Optional getBookByTitle(String title) { - for (List books : collection.values()) { - for (Book b : books) { - if (b.title().equals(title)) { - return Optional.of(b); - } - } + public void addBook(Author author, Book book) { + collection.get(author).add(book); + } + + public Optional getBookByTitle(String title) { + for (List books : collection.values()) { + for (Book b : books) { + if (b.title().equals(title)) { + return Optional.of(b); } - return Optional.empty(); + } } + return Optional.empty(); + } - public Optional getMostDiligentAuthor() { - Author mostDiligentAuthor = null; - int mostBooks = 0; - for (Entry> entry : collection.entrySet()) { - if (entry.getValue().size() > mostBooks) { - mostDiligentAuthor = entry.getKey(); - mostBooks = entry.getValue().size(); - } - } - return Optional.ofNullable(mostDiligentAuthor); + public Optional getMostDiligentAuthor() { + Author mostDiligentAuthor = null; + int mostBooks = 0; + for (Entry> entry : collection.entrySet()) { + if (entry.getValue().size() > mostBooks) { + mostDiligentAuthor = entry.getKey(); + mostBooks = entry.getValue().size(); + } } -} \ No newline at end of file + return Optional.ofNullable(mostDiligentAuthor); + } +} diff --git a/DuplicateKeyException.java b/DuplicateKeyException.java index 50f898b..0917539 100644 --- a/DuplicateKeyException.java +++ b/DuplicateKeyException.java @@ -1,5 +1,4 @@ public class DuplicateKeyException extends Exception { - private static final long serialVersionUID = 1L; - -} \ No newline at end of file + private static final long serialVersionUID = 1L; +} diff --git a/Exercise.java b/Exercise.java index 4b4c209..14125d0 100644 --- a/Exercise.java +++ b/Exercise.java @@ -23,7 +23,5 @@ public static void main(String[] args) { System.out.println(collection.getBookByTitle("Das Lied von Eis und Feuer 5")); System.out.println(collection.getMostDiligentAuthor()); - } - } From 4a624fd915ac1b0fe1d2b3d88e215409cdd6351a Mon Sep 17 00:00:00 2001 From: SteffenLm <33038091+SteffenLm@users.noreply.github.com> Date: Wed, 5 Jun 2024 21:59:19 +0200 Subject: [PATCH 3/5] fix solution according to description and diagramm --- BookCollection.java | 10 +++++----- DuplicateKeyException.java | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/BookCollection.java b/BookCollection.java index b7a725d..01cb601 100644 --- a/BookCollection.java +++ b/BookCollection.java @@ -18,18 +18,18 @@ public void addBook(Author author, Book book) { collection.get(author).add(book); } - public Optional getBookByTitle(String title) { + public Book getBookByTitle(String title) { for (List books : collection.values()) { for (Book b : books) { if (b.title().equals(title)) { - return Optional.of(b); + return b; } } } - return Optional.empty(); + return null; } - public Optional getMostDiligentAuthor() { + public Author getMostDiligentAuthor() { Author mostDiligentAuthor = null; int mostBooks = 0; for (Entry> entry : collection.entrySet()) { @@ -38,6 +38,6 @@ public Optional getMostDiligentAuthor() { mostBooks = entry.getValue().size(); } } - return Optional.ofNullable(mostDiligentAuthor); + return mostDiligentAuthor; } } diff --git a/DuplicateKeyException.java b/DuplicateKeyException.java index 0917539..6213685 100644 --- a/DuplicateKeyException.java +++ b/DuplicateKeyException.java @@ -1,4 +1,2 @@ public class DuplicateKeyException extends Exception { - - private static final long serialVersionUID = 1L; } From dded37187ac2265991084bcdf0ca3651c4c4fa3c Mon Sep 17 00:00:00 2001 From: SteffenLm <33038091+SteffenLm@users.noreply.github.com> Date: Wed, 5 Jun 2024 21:59:33 +0200 Subject: [PATCH 4/5] fix imports --- BookCollection.java | 1 - 1 file changed, 1 deletion(-) diff --git a/BookCollection.java b/BookCollection.java index 01cb601..688804d 100644 --- a/BookCollection.java +++ b/BookCollection.java @@ -2,7 +2,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map.Entry; -import java.util.Optional; public record BookCollection(HashMap> collection) { From 049ac015d6ad36ddb8abcd46034cca933b434697 Mon Sep 17 00:00:00 2001 From: github-actions <> Date: Wed, 5 Jun 2024 19:59:51 +0000 Subject: [PATCH 5/5] Google Java Format --- DuplicateKeyException.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DuplicateKeyException.java b/DuplicateKeyException.java index 6213685..f6b572c 100644 --- a/DuplicateKeyException.java +++ b/DuplicateKeyException.java @@ -1,2 +1 @@ -public class DuplicateKeyException extends Exception { -} +public class DuplicateKeyException extends Exception {}