Build a simple PhoneBook program.

PhoneBook.java 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.TreeMap;
  4. import java.util.Set;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Collections;
  8. /**
  9. * Created by leon on 1/23/18.
  10. */
  11. public class PhoneBook {
  12. TreeMap<String,String> phoneMap = new TreeMap<String, String>();
  13. String name;
  14. String phoneNumber;
  15. public PhoneBook(String name, String phoneNumber){
  16. //Constructor
  17. this.name = name;
  18. this.phoneNumber = phoneNumber;
  19. }
  20. public void add(String name, String phoneNumber){
  21. phoneMap.put(name, phoneNumber);
  22. }
  23. public void remove(String name){
  24. phoneMap.remove(name);
  25. }
  26. public String lookup(String name){
  27. return phoneMap.get(name);
  28. }
  29. public String reverseLookup(String phoneNumber){
  30. return phoneMap.get(phoneNumber);
  31. }
  32. public String display(){
  33. String resultString = "";
  34. for(String e : phoneMap.keySet()){
  35. String v = phoneMap.get(e);
  36. resultString = resultString + e + " -> " + v + "\n";
  37. }
  38. return resultString;
  39. }
  40. }