123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import java.util.*;
-
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- String name;
- String phoneNumber;
- SortedMap<String, String> phoneBook = new TreeMap<String, String>();
-
- public void add(String name, String phoneNumber){
- phoneBook.put(name, phoneNumber);
- }
-
- public void remove(String name){
- phoneBook.remove(name);
- }
-
- public String lookup(String name){
- return phoneBook.get(name);
- }
-
- public String reverseLookup(String phoneNumber){
- for(Map.Entry<String, String> e : phoneBook.entrySet()){
- if(e.getValue().equals(phoneNumber)){
- return e.getKey();
- }
- }
- return null;
- }
-
- public void display(){
- System.out.printf("%-20s%-12s\n", "Name", "Phone Number");
- for(Map.Entry<String, String> e : phoneBook.entrySet()){
- String printName = e.getKey();
- String printNumber = e.getValue();
- System.out.printf(" %-20s %-12s\n", printName, printNumber);
- }
- System.out.printf("-----End of the phone book-----\n");
- }
- }
|