Build a simple PhoneBook program.

PhoneBook.java 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import java.util.*;
  2. /**
  3. * Created by leon on 1/23/18.
  4. */
  5. public class PhoneBook {
  6. Map<String, ArrayList<String> > entries = new HashMap<String, ArrayList<String>>();
  7. public void add (String name, String phoneNumber){
  8. ArrayList<String> pn = new ArrayList<String>();
  9. pn.add(phoneNumber);
  10. this.entries.put(name, pn);
  11. }
  12. public void addNumber(String name, String phoneNumber){
  13. for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
  14. if(name.equals(entry.getKey())){
  15. entry.getValue().add(phoneNumber);
  16. break;
  17. }
  18. }
  19. }
  20. public void remove(String name, String phoneNumber){
  21. }
  22. public void removeRecord(String name){
  23. this.entries.remove(name);
  24. }
  25. public void lookUp(String name){
  26. System.out.print(this.entries.get(name));
  27. }
  28. public String reverseLoopup(String phoneNumber){
  29. String name = "No Records...";
  30. for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
  31. for(String pn : entry.getValue()){
  32. if(pn.equals(phoneNumber)){
  33. name = entry.getKey();
  34. }
  35. }
  36. }
  37. return name;
  38. }
  39. public void display() {
  40. StringBuilder sb = new StringBuilder();
  41. for(Map.Entry<String, ArrayList<String>> entry :this.entries.entrySet()){
  42. sb.append(entry.getKey());
  43. sb.append(" - ");
  44. sb.append(entry.getValue());
  45. sb.append("\n");
  46. }
  47. System.out.print(sb.toString());
  48. }
  49. }