12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import java.util.Map;
- import java.util.TreeMap;
-
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- private Map<String, String> phoneBook = new TreeMap<String, String>();
-
- public String add(String name, String phonenumber){
- phoneBook.put(name, phonenumber);
- return (name + " " + phoneBook.get(name));
-
- }
-
- public String remove(String name){
- phoneBook.remove(name);
- return name;
- }
-
- public String reverseLookup(String number){
- String str = "";
- for(Map.Entry<String, String>entry : phoneBook.entrySet()){
- if(entry.getValue().equals(number)){
- str = (entry.getKey());
- }
- }
- return str;
- }
-
- public String lookUp(String name){
- return phoneBook.get(name);
- }
-
- public String display(){
- String result = "";
-
- for(Map.Entry<String, String> entry : phoneBook.entrySet()){
- result += (entry.getKey()) + " " + (entry.getValue() + "\n");
- }
-
- return result;
-
-
- }
-
-
- }
|