12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /**
- * Created by leon on 1/23/18.
- */
- import java.util.*;
-
- public class PhoneBook {
- private TreeMap<String, ArrayList<String>> book = new TreeMap<String, ArrayList<String>>();
-
- public ArrayList<String> add(String name, String phoneNumber) {
- ArrayList<String> converted = new ArrayList<String>();
-
- if (book.containsKey(name)) converted = book.get(name);
-
- converted.add(phoneNumber);
- add(name, converted);
- return book.get(name);
- }
-
- public ArrayList<String> add(String name, ArrayList<String> phoneNumbers) {
- book.put(name, phoneNumbers);
- return book.get(name);
- }
-
- public TreeMap<String, ArrayList<String>> remove(String name) {
- book.remove(name);
- return book;
- }
-
- public ArrayList<String> remove(String name, String number) {
- ArrayList<String> numbers = book.get(name);
- numbers.remove(numbers.indexOf(number));
- book.put(name, numbers);
- return book.get(name);
- }
-
- public ArrayList<String> lookup(String name) {
- return book.get(name);
- }
-
- public String reverseLookup(String phoneNumber) {
- String result = "none";
- for(Map.Entry<String, ArrayList<String>> entry : book.entrySet()) {
- ArrayList<String> numbers = entry.getValue();
- for (String number : numbers) {
- if (number.equals(phoneNumber)) result = entry.getKey();
- }
- }
- return result;
- }
-
- public String display() {
- StringBuilder result = new StringBuilder();
- for(Map.Entry<String, ArrayList<String>> entry : book.entrySet()) {
- result.append(entry.getKey() + " ");
-
- ArrayList<String> numbers = entry.getValue();
- for (String number : numbers) {
- result.append(number + " ");
- }
-
- result.append("\n");
- }
- return result.toString();
- }
-
- public static void main(String[] args) {
- PhoneBook phoneBook = new PhoneBook();
- };
- }
|