Build a simple PhoneBook program.

PhoneBook.java 803B

12345678910111213141516171819202122232425262728293031
  1. import java.util.*;
  2. import java.util.Map.Entry;
  3. public class PhoneBook {
  4. Map<String, List<String>> phoneMap = new TreeMap<String, List<String>>();
  5. List<String> phoneNumbers = new ArrayList<String>();
  6. public void add(String name, String phoneNumber){
  7. phoneNumbers.add(phoneNumber);
  8. phoneMap.put(name, phoneNumbers);
  9. }
  10. public void remove(String name){
  11. phoneMap.remove(name);
  12. }
  13. public String lookup(String name){
  14. return phoneNumbers.iterator(phoneMap.get(name));
  15. }
  16. public String reverseLookup(String phoneNumber){
  17. for(Entry<String, String> entry : phoneMap.entrySet()){
  18. if(entry.getValue().equals(phoneNumber)){
  19. return entry.getKey();
  20. }
  21. }
  22. return null;
  23. }
  24. }