Build a simple PhoneBook program.

PhoneBook.java 1.1KB

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