Build a simple PhoneBook program.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import java.util.*;
  2. /**
  3. * Created by leon on 1/23/18.
  4. */
  5. public class PhoneBook {
  6. String name;
  7. String phoneNumber;
  8. // create treemap(hashmap), has key String, value arrayList of Strings
  9. TreeMap<String, ArrayList<String>> phoneBook = new TreeMap<>();
  10. // construct an instance of phoneBook
  11. public PhoneBook(TreeMap<String, ArrayList<String>> phoneBook) {
  12. this.phoneBook = phoneBook;
  13. }
  14. // default constructor
  15. public PhoneBook(){}
  16. public void add(String name, String phoneNumber){
  17. // if phonebook does NOT contain key(name), put name on list
  18. if (!phoneBook.containsKey(name)) {
  19. ArrayList<String> list = new ArrayList<>();
  20. list.add(phoneNumber);
  21. phoneBook.put(name, list);
  22. // or else, create tempoary arraylist and add phonenumber
  23. } else {
  24. ArrayList<String> temp = phoneBook.get(name);
  25. temp.add(phoneNumber);
  26. //(key,old key,new key)
  27. phoneBook.replace(name, phoneBook.get(name), temp);
  28. }
  29. }
  30. public void remove(String name, String phoneNumber){
  31. ArrayList<String> list = phoneBook.get(name);
  32. // if only one number, remove name
  33. if(list.size() == 1) {
  34. phoneBook.remove(name);
  35. // or else create temp arraylist and remove phonenumber/replace
  36. // in list with parameters (name, list, temp)
  37. } else {
  38. ArrayList<String> temp = phoneBook.get(name);
  39. temp.remove(phoneNumber);
  40. phoneBook.replace(name,list,temp);
  41. }
  42. }
  43. public void removeRecord(String name) {
  44. phoneBook.remove(name);
  45. }
  46. // lookup returns list of phonenumbers for given name
  47. public ArrayList<String> lookup(String name){
  48. return phoneBook.get(name);
  49. }
  50. public String reverseLookup(String phoneNumber) {
  51. // for each entry in phonebook, lookup array of name/number
  52. for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
  53. // gets name
  54. String name = entry.getKey();
  55. // gets list of numbers
  56. ArrayList<String> list = entry.getValue();
  57. // if number equals the number we are looking for, return name
  58. for (String number: list) {
  59. if (number.equals(phoneNumber)) {
  60. return name;
  61. }
  62. }
  63. }
  64. return "Not found";
  65. }
  66. public String display() {
  67. // build a string to display
  68. StringBuilder sb = new StringBuilder();
  69. // For (string, phone number) entry, iterate thru phoneBook.entrySet
  70. for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()){
  71. String name = entry.getKey();
  72. ArrayList<String> numbers = entry.getValue();
  73. // when key got, add name to space " "
  74. sb.append(name).append(" ");
  75. // for each number in element, append to name
  76. for (int i = 0; i < numbers.size(); i++) {
  77. sb.append(numbers.get(i));
  78. // append a space if i is not the last number in list
  79. if (i != numbers.size()-1) {
  80. sb.append(" ");
  81. }
  82. }
  83. sb.append("\n");
  84. }
  85. return sb.toString();
  86. }
  87. }