Build a simple PhoneBook program.

PhoneBook.java 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import java.util.TreeMap;
  2. import java.util.Map;
  3. import java.util.ArrayList;
  4. import java.util.Map.Entry;
  5. /**
  6. * Created by leon on 1/23/18.
  7. */
  8. public class PhoneBook {
  9. Map<String, ArrayList> phoneBook = new TreeMap<String, ArrayList>();
  10. public String add(String name, String phoneNumber){
  11. ArrayList<String> numbers = new ArrayList<String>();
  12. numbers.add(phoneNumber);
  13. phoneBook.put(name, numbers);
  14. return name + " has been added.";
  15. }
  16. public int getSize(){
  17. return phoneBook.size();
  18. }
  19. public boolean keyExists(String name){
  20. return phoneBook.containsKey(name);
  21. }
  22. public String remove(String name){
  23. phoneBook.remove(name);
  24. return name + " has been deleted.";
  25. }
  26. public String lookup(String name){
  27. return String.join(", ", phoneBook.get(name));
  28. }
  29. public String reverseLookup(String phoneNumber){
  30. for(Map.Entry<String,ArrayList> entry : phoneBook.entrySet()) {
  31. String key = entry.getKey();
  32. ArrayList value = entry.getValue();
  33. if (value.equals(phoneNumber)) {
  34. }
  35. return entry.getKey();
  36. }
  37. return "";
  38. }
  39. public void display(){
  40. for(Map.Entry<String, ArrayList> entry : phoneBook.entrySet()) {
  41. String key = entry.getKey();
  42. ArrayList value = entry.getValue();
  43. System.out.println(key + " => " + value);
  44. }
  45. }
  46. }