import java.util.*; import java.util.List; import java.util.Arrays; import java.util.Iterator; /** * Created by leon on 1/23/18. */ public class PhoneBook { String name; String phoneNumber; TreeMap> directory = new TreeMap>(); HashMap,String> reverseDirectory = new HashMap,String>(); public PhoneBook(){ } public TreeMap getDirectory(){ return directory; } public HashMap, String> invertMap() { for(String k: directory.keySet()){ reverseDirectory.put(directory.get(k), k); } return reverseDirectory; } public TreeMap add(String name, ArrayList phoneNumber){ directory.put(name, phoneNumber); return directory; } public TreeMap removeRecord(String name){ directory.remove(name); return directory; } public ArrayList lookUp(String name){ return directory.get(name); } public String reverseLookUp(String phoneNumber){ this.invertMap(); String answer = ""; for(ArrayList element: reverseDirectory.keySet()){ if(arrayContains(element, phoneNumber) == true){ answer = reverseDirectory.get(element); } } return answer; } public String display(){ StringBuilder print = new StringBuilder(""); for(String key : directory.keySet()){ print.append(key + " " + printArray(directory.get(key)) + "\n"); } return print.toString(); } public String printArray(ArrayList array){ String result = String.join(", ", array); return result; } public boolean arrayContains(ArrayList list, String phoneNumber){ boolean correct = false; for(String element : list){ if(phoneNumber.equals(element)){ correct = true; break; } } return correct; } public TreeMap remove(String name, String phoneNumber){ Iterator search = directory.get(name).iterator(); while(search.hasNext()){ if(search.next().equals(phoneNumber)){ search.remove(); break; } } return directory; } }