Skip to content

Commit ab560a7

Browse files
committed
Exercise Optionals 01
0 parents  commit ab560a7

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed

.github/workflows/format.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: format and commit all java files
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- 'exercises/**'
8+
- 'solutions/**'
9+
10+
jobs:
11+
formatting:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-java@v3
16+
with:
17+
java-version: '17'
18+
distribution: 'temurin'
19+
- uses: axel-op/googlejavaformat-action@v3

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*

Author.java

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

Book.java

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

BookCollection.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map.Entry;
5+
6+
public record BookCollection(HashMap<Author, List<Book>> collection) {
7+
8+
public void addAuthor(Author author) throws DuplicateKeyException {
9+
if (collection.containsKey(author)) {
10+
throw new DuplicateKeyException();
11+
}
12+
13+
collection.put(author, new ArrayList<>());
14+
}
15+
16+
public void addBook(Author author, Book book) {
17+
collection.get(author).add(book);
18+
}
19+
20+
public Book getBookByTitle(String title) {
21+
for (List<Book> books : collection.values()) {
22+
for (Book b : books) {
23+
if (b.title().equals(title)) {
24+
return b;
25+
}
26+
}
27+
}
28+
return null;
29+
}
30+
31+
public Author getMostDiligentAuthor() {
32+
Author mostDiligentAuthor = null;
33+
int mostBooks = 0;
34+
for (Entry<Author, List<Book>> entry : collection.entrySet()) {
35+
if (entry.getValue().size() > mostBooks) {
36+
mostDiligentAuthor = entry.getKey();
37+
mostBooks = entry.getValue().size();
38+
}
39+
}
40+
return mostDiligentAuthor;
41+
}
42+
}

DuplicateKeyException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class DuplicateKeyException extends Exception {}

Exercise.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.HashMap;
2+
3+
public class Exercise {
4+
5+
public static void main(String[] args) {
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+
}
27+
}

0 commit comments

Comments
 (0)