Skip to content

Solution for Maps 01 #59

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

Open
wants to merge 2 commits into
base: exercises/maps/01
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
14 changes: 13 additions & 1 deletion Exercise.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
public class Exercise {

public static void main(String[] args) {
// implement exercise here
TelephoneBook telephoneBook = new TelephoneBook();

telephoneBook.addEntry(
new TelephoneBook.Person("Hans Maier"),
new TelephoneBook.TelephoneNumber("+49 67234 887261"));
telephoneBook.addEntry(
new TelephoneBook.Person("Peter Mueller"),
new TelephoneBook.TelephoneNumber("+49 7529 87612"));
telephoneBook.addEntry(
new TelephoneBook.Person("Lisa Schmidt"),
new TelephoneBook.TelephoneNumber("+49 8762 52081"));

System.out.println(telephoneBook.getTelephoneNumberByName("Hans Maier"));
}
}
24 changes: 24 additions & 0 deletions TelephoneBook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.HashMap;
import java.util.Map.Entry;

public class TelephoneBook {

private HashMap<Person, TelephoneNumber> entries = new HashMap<>();

public void addEntry(Person person, TelephoneNumber telephoneNumber) {
entries.put(person, telephoneNumber);
}

public TelephoneNumber getTelephoneNumberByName(String name) {
for (Entry<Person, TelephoneNumber> entry : entries.entrySet()) {
if (entry.getKey().name().equals(name)) {
return entry.getValue();
}
}
return null;
}

public record Person(String name) {}

public record TelephoneNumber(String value) {}
}