Build a simple PhoneBook program.

PhoneBook.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import java.util.ArrayList;
  2. import java.util.Map;
  3. import java.util.TreeMap;
  4. /**
  5. * Created by leon on 1/23/18.
  6. */
  7. public class PhoneBook {
  8. public static void main (String[] args){
  9. }
  10. private Map<String, ArrayList<String>> directory;
  11. public PhoneBook (){
  12. directory= new TreeMap< String, ArrayList<String>>();
  13. }
  14. public void setValues(String name, ArrayList<String>phonenumber){
  15. this.directory.put(name, phonenumber);
  16. }
  17. public boolean hasEntry(String name) {
  18. return directory.containsKey(name);
  19. }
  20. public void add(String name, String number) {
  21. ArrayList<String> contact = new ArrayList<>();
  22. contact.add(number);
  23. directory.put(name,contact);
  24. }
  25. public ArrayList<String> getContact(String name) {
  26. return directory.get(name);
  27. }
  28. public String reverseLookUp(String number){
  29. String repply= "Not here";
  30. for (String name:directory.keySet()){
  31. ArrayList<String> reverse= directory.get(name);
  32. for (String looking: reverse){
  33. if (looking.equals(number)){
  34. repply= name;
  35. }
  36. }
  37. }
  38. return repply;
  39. }
  40. public void remove (String name){
  41. if (directory.containsKey(name)){
  42. directory.remove(name);
  43. }
  44. }
  45. public String listNameAndNumbers() {
  46. StringBuilder builder = new StringBuilder();
  47. for (String name:directory.keySet()){
  48. for (int i= 0; i< directory.get(name).size(); i++){
  49. builder.append(name + " " + directory.get(name).get(i) + "\n");
  50. }
  51. }
  52. return builder.toString();
  53. }
  54. public Map<String, ArrayList<String>> getDirectory() {
  55. return directory;
  56. }
  57. public Map<String, ArrayList<String>> getContact() {
  58. return directory;
  59. }
  60. public void setDirectory(Map<String, ArrayList<String>> directory) {
  61. this.directory = directory;
  62. }
  63. }