123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
-
- import java.util.TreeMap;
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- private TreeMap<String, String> phoneBook;
- private TreeMap<String, String> reversePhoneBook;
-
- public PhoneBook() {
- phoneBook = new TreeMap<String, String>();
- reversePhoneBook = new TreeMap<String, String>();
- }
-
- public void add(String name, String phoneNumber) {
- phoneBook.put(name, phoneNumber);
- reversePhoneBook.put(phoneNumber, name);
- }
-
- public void remove(String name) {
- String phoneNumber = phoneBook.get(name);
- phoneBook.remove(name);
- reversePhoneBook.remove(phoneNumber);
- }
-
- public String lookup(String name) {
- return phoneBook.get(name) != null ?
- phoneBook.get(name)
- :
- "N/A";
- }
-
- public String reverseLookup(String phoneNumber) {
- return reversePhoneBook.get(phoneNumber) != null ?
- reversePhoneBook.get(phoneNumber)
- :
- "N/A";
- }
-
- public TreeMap getPhoneBook() {
- return phoneBook;
- }
-
- public TreeMap getReversePhoneBook() {
- return reversePhoneBook;
- }
- }
|