12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import java.util.*;
- import java.util.TreeMap;
- import java.util.Set;
- import java.util.Map;
- import java.util.Iterator;
-
- public class PhoneBook {
- String phoneNumber;
- Map<String, String> pb = new TreeMap <String, String>();
-
- public PhoneBook(){
- }
-
- public void add(String name, String phoneNumber){
-
- pb.put(name, phoneNumber);
-
- }
-
- public void remove(String name){
- pb.remove(name);
- }
-
- public String display(){
- Set set = pb.entrySet();
- Iterator it = set.iterator();
- StringBuilder display = new StringBuilder();
-
- while(it.hasNext()) {
- Map.Entry me = (Map.Entry)it.next();
- display.append(me.getKey() + " : " + me.getValue() + "\n");
-
- }
- return display.toString();
- }
-
- public String lookup(String name){
- String phone = pb.get(name);
- return phone;
- }
-
- public String reverseLookup(String phoneNumber){
- String key= null;
- for(Map.Entry entry: pb.entrySet()){
- if(phoneNumber.equals(entry.getValue())){
- key = entry.getKey().toString();
- break;
- }
- }
- return key;
-
- }
-
- public int size() {
- return pb.size();
- }
- }
|