12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.TreeMap;
-
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- String name;
- String number = "";
- Map<String, String> yellowPages = new TreeMap<String, String>();
-
- public PhoneBook(){
- }
- public void add(String name, String phoneNumber){
- yellowPages.put(name, phoneNumber);
- }
-
- public void remove(String name) {
- yellowPages.remove(name);
- }
-
- public String lookUp(String name) {
- String number = yellowPages.get(name);
- return number;
- }
-
- public int getSize(){
-
- return yellowPages.size();
- }
-
- public String reverseLookUp(String value) {
- String key = "";
-
- for(Map.Entry entry : yellowPages.entrySet())
- if(value.equals(entry.getValue())){
- key = entry.getKey().toString();
-
- }
-
- return key;
- }
-
- public String display() {
- Set set = yellowPages.entrySet();
- Iterator it = set.iterator();
-
- StringBuilder answerBuild = new StringBuilder();
-
- while(it.hasNext()) {
- Map.Entry x = (Map.Entry)it.next();
- answerBuild.append(x.getKey() + " : " + x.getValue());
-
- }
-
- String answer = answerBuild.toString();
- return answer;
- }
- }
|