123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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<String,ArrayList<String>> directory = new TreeMap<String,ArrayList<String>>();
- HashMap<ArrayList<String>,String> reverseDirectory = new HashMap<ArrayList<String>,String>();
- public PhoneBook(){
-
- }
-
- public TreeMap getDirectory(){
- return directory;
- }
-
- public HashMap<ArrayList<String>, String> invertMap() {
- for(String k: directory.keySet()){
- reverseDirectory.put(directory.get(k), k);
- }
- return reverseDirectory;
- }
-
- public TreeMap add(String name, ArrayList<String> phoneNumber){
- directory.put(name, phoneNumber);
- return directory;
- }
-
- public TreeMap removeRecord(String name){
- directory.remove(name);
- return directory;
- }
-
- public ArrayList<String> lookUp(String name){
- return directory.get(name);
- }
-
- public String reverseLookUp(String phoneNumber){
- this.invertMap();
- String answer = "";
- for(ArrayList<String> 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<String> array){
- String result = String.join(", ", array);
-
- return result;
- }
-
- public boolean arrayContains(ArrayList<String> 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;
- }
- }
|