diff --git a/Author.java b/Author.java new file mode 100644 index 0000000..9694c0e --- /dev/null +++ b/Author.java @@ -0,0 +1 @@ +public record Author(String Name) {} diff --git a/Book.java b/Book.java new file mode 100644 index 0000000..19ad242 --- /dev/null +++ b/Book.java @@ -0,0 +1 @@ +public record Book(String title) {} diff --git a/BookCollection.java b/BookCollection.java new file mode 100644 index 0000000..688804d --- /dev/null +++ b/BookCollection.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map.Entry; + +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 Book getBookByTitle(String title) { + for (List books : collection.values()) { + for (Book b : books) { + if (b.title().equals(title)) { + return b; + } + } + } + return null; + } + + public Author 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 mostDiligentAuthor; + } +} diff --git a/DuplicateKeyException.java b/DuplicateKeyException.java new file mode 100644 index 0000000..f6b572c --- /dev/null +++ b/DuplicateKeyException.java @@ -0,0 +1 @@ +public class DuplicateKeyException extends Exception {} diff --git a/Exercise.java b/Exercise.java index 3c092f9..14125d0 100644 --- a/Exercise.java +++ b/Exercise.java @@ -1,6 +1,27 @@ +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()); } }