1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 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 phoneNumber = "";
- Map<String, String> phoneBook = new TreeMap<String, String>();
-
- public PhoneBook() {
- }
-
- public void add(String name, String phoneNumber) {
- phoneBook.put(name, phoneNumber);
- }
-
- public void remove(String name) {
- phoneBook.remove(name);
- }
-
- public String lookup(String name) {
- String phoneNumber = phoneBook.get(name);
- return phoneNumber;
- }
-
- public String reverseLookup(String value){
- String key = "";
-
- for(Map.Entry entry : phoneBook.entrySet())
- if (value.equals(entry.getValue())) {
- key = entry.getKey().toString();
- }
- return key;
- }
-
- public String display() {
- Set set = phoneBook.entrySet();
- Iterator iter = set.iterator();
-
- StringBuilder answer = new StringBuilder();
-
- while(iter.hasNext()) {
- Map.Entry i = (Map.Entry)iter.next();
- answer.append(i.getKey() + " : " + i.getValue());
- }
-
- String result = answer.toString();
- return result;
- }
-
- public int getSize() {
- return phoneBook.size();
- }
- }
-
|