Build a simple PhoneBook program.

PhoneBook.java 1011B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import java.util.*;
  2. /**
  3. * Created by leon on 1/23/18.
  4. */
  5. public class PhoneBook {
  6. TreeMap<String, String> phoneListName = new TreeMap();
  7. TreeMap<String, String> phoneListNumber = new TreeMap();
  8. public void add(String name, String number){
  9. phoneListName.put(name, number);
  10. phoneListNumber.put(number, name);
  11. }
  12. public void remove(String name){
  13. String number = lookUp(name);
  14. phoneListName.remove(name);
  15. phoneListNumber.remove(number);
  16. }
  17. public String lookUp(String name){
  18. return phoneListName.get(name);
  19. }
  20. public String reverseLookUp(String number){
  21. return phoneListNumber.get(number);
  22. }
  23. public void display(){
  24. Set set = phoneListName.entrySet();
  25. Iterator i = set.iterator();
  26. while(i.hasNext()){
  27. Map.Entry me = (Map.Entry)i.next();
  28. System.out.println(me.getKey() + ": " + me.getValue());
  29. }
  30. }
  31. }