Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for Maps 02 #66

Open
wants to merge 5 commits into
base: exercises/maps/02
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public record Author(String Name) {}
1 change: 1 addition & 0 deletions Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public record Book(String title) {}
42 changes: 42 additions & 0 deletions BookCollection.java
Original file line number Diff line number Diff line change
@@ -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<Author, List<Book>> 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<Book> 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<Author, List<Book>> entry : collection.entrySet()) {
if (entry.getValue().size() > mostBooks) {
mostDiligentAuthor = entry.getKey();
mostBooks = entry.getValue().size();
}
}
return mostDiligentAuthor;
}
}
1 change: 1 addition & 0 deletions DuplicateKeyException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public class DuplicateKeyException extends Exception {}
23 changes: 22 additions & 1 deletion Exercise.java
Original file line number Diff line number Diff line change
@@ -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());
}
}