1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
-
- import java.util.*;
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- private TreeMap<String, ArrayList<String>> phoneBook;
- public PhoneBook() {
- phoneBook = new TreeMap<String, ArrayList<String>>();
- }
-
- public void add(String name, String phoneNumber) {
- ArrayList<String> input = phoneBook.containsKey(name) ?
- phoneBook.get(name)
- :
- new ArrayList<String>();
- input.add(phoneNumber);
- phoneBook.put(name, input);
- }
-
- public void removeEntry(String name) {
- phoneBook.remove(name);
- }
-
- public String lookup(String name) {
- return phoneBook.get(name)
- .toString()
- .replaceAll("[\\[\\]]", "");
- }
-
- public String reverseLookup(String phoneNumber) {
- String output = "";
- Set<String> keys = phoneBook.keySet();
- for (String key : keys) {
- if (phoneBook.get(key).contains(phoneNumber)) {
- output = key;
- }
- }
- return output.equals("") ? "No associated name." : output;
- }
-
- public String display() {
- return phoneBook.toString().replaceAll("[{}=,]", "")
- .replaceAll("\\[", " - ")
- .replaceAll("\\]", "\n");
-
- }
-
- public TreeMap getPhoneBook() {
- return phoneBook;
- }
- }
|