Build a simple PhoneBook program.

PhoneBook.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import java.util.TreeMap;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.List;
  6. import java.util.Arrays;
  7. /**
  8. * Created by leon on 1/23/18.
  9. */
  10. public class PhoneBook {
  11. private String name;
  12. private String number;
  13. Map<String,List<String>> phoneBook =new TreeMap<String, List<String>>();
  14. List<String> numberSet = new ArrayList<String>();
  15. public PhoneBook(){
  16. }
  17. public void addToPhoneBook(String name, String number){
  18. ArrayList <String> temp = new ArrayList<>();
  19. temp.add(number);
  20. phoneBook.put(name, temp);
  21. }
  22. public void addToPhoneBookIndividualNumbers(String name, String number){
  23. numberSet.add(number);
  24. phoneBook.put(name, numberSet);
  25. }
  26. public void removeFromPhoneBook(String name){
  27. phoneBook.remove(name);
  28. }
  29. public void removeFromPhoneBookIndividualNumbers(String name, String number){
  30. phoneBook.get(name).remove(number);
  31. }
  32. public String lookupFromPhoneBook(String name){
  33. numberSet = phoneBook.get(name);
  34. return numberSet.toString();
  35. }
  36. public String reverseLookupFromPhoneBook(String number){
  37. String answerKey = null;
  38. for(String key: phoneBook.keySet()){
  39. List<String> test = phoneBook.get(key);
  40. for(String s: test){
  41. if(number.equals(s)){
  42. return key;
  43. }
  44. }
  45. }
  46. return "nothing is here";
  47. }
  48. public String displayFromPhoneBook(){
  49. return phoneBook.entrySet().toString();
  50. }
  51. }