12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
-
-
- /**
- * Created by leon on 1/23/18.
- */
- import java.util.*;
- import java.lang.*;
- public class PhoneBook {
- Map<String, ArrayList<String>> phoneBook = new TreeMap<String, ArrayList<String>>();
-
-
- public PhoneBook(){
-
- }
-
- public ArrayList<String> add(String name, String phoneNumber){
- ArrayList<String> numbers = new ArrayList<>();
-
- if(phoneBook.containsKey(name)) numbers = phoneBook.get(name);
-
- numbers.add(phoneNumber);
- add(name, numbers);
- return phoneBook.get(name);
- // phoneBook.put(name, phoneNumber);
- }
-
- public ArrayList<String> add(String name, ArrayList<String> phoneNumbers){
- phoneBook.put(name, phoneNumbers);
- return phoneBook.get(name);
- }
-
- public ArrayList<String> remove(String name){
- phoneBook.remove(name);
- return phoneBook.get(name);
- }
-
- public ArrayList<String> lookUp(String name){
- ArrayList<String> number = new ArrayList<>();
- for(Map.Entry<String, ArrayList<String>> book: phoneBook.entrySet()){
- if(book.getKey().equals(name)){
- number = book.getValue();
- }
- }
- return number;
- }
-
- public String reverseLookUp(ArrayList<String> phoneNumber){
- String name = null;
- for(Map.Entry<String, ArrayList<String>> book: phoneBook.entrySet()){
- if(book.getValue().equals(phoneNumber)){
- name = book.getKey();
- }
- }
- return name;
- }
-
- public void display(){
- for(Map.Entry<String, ArrayList<String>> book: phoneBook.entrySet()){
- System.out.println(book.getKey());
- for(String num: book.getValue()){
- System.out.println("- "+ num);
- }
- }
- }
-
- public int size(){
- return phoneBook.size();
- }
- }
|