Build a simple PhoneBook program.

PhoneBook.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import java.util.Iterator;
  2. import java.util.Map;
  3. import java.util.Set;
  4. import java.util.SortedMap;
  5. import java.util.TreeMap;
  6. import java.util.ArrayList;
  7. public class PhoneBook {
  8. public TreeMap<String, ArrayList<String>> bookEntries;
  9. public PhoneBook (){
  10. this.bookEntries = new TreeMap<String, ArrayList<String>>();
  11. }
  12. public void add(String name, String phoneNumber) {
  13. ArrayList<String> addMe = new ArrayList<String>();
  14. if (bookEntries.containsKey(name)) {
  15. addMe = bookEntries.get(name);
  16. }
  17. addMe.add(phoneNumber);
  18. bookEntries.put(name, addMe);
  19. }
  20. public void remove(String name, String phoneNumber) {
  21. ArrayList listOfNumbers = new ArrayList();
  22. if (bookEntries.containsKey(name)) {
  23. listOfNumbers = bookEntries.get(name);
  24. listOfNumbers.remove(phoneNumber);
  25. }
  26. if (listOfNumbers.size() == 0) {
  27. bookEntries.remove(name);
  28. }
  29. }
  30. public void removeRecord(String name) {
  31. if (bookEntries.containsKey(name)) {
  32. bookEntries.remove(name);
  33. }
  34. }
  35. public String lookup(String name) {
  36. String phoneNumber = "This entry doesn't exist. Please enter valid entry.";
  37. if (bookEntries.containsKey(name)) {
  38. phoneNumber = bookEntries.get(name).toString().replace("[", "").replace("]", "");
  39. }
  40. return phoneNumber;
  41. }
  42. public String reverseLookup(String phoneNumber) {
  43. String name = "This entry doesn't exist. Please enter valid entry.";
  44. Set<String> keys = bookEntries.keySet();
  45. for(String key: keys){
  46. if(bookEntries.get(key).contains(phoneNumber) == true) {
  47. name = key;
  48. }
  49. }
  50. return name;
  51. }
  52. public void display() {
  53. Set<String> keys = bookEntries.keySet();
  54. for(String key: keys){
  55. System.out.println(key + ": " + bookEntries.get(key).toString().replace("[", "").replace("]", ""));
  56. }
  57. }
  58. public void main(String[] args) {
  59. PhoneBook myBook = new PhoneBook();
  60. myBook.add("Tomato", "267-945-5231");
  61. myBook.add("Asparagus", "305-504-1159");
  62. myBook.add("Eggplant", "123-345-6789");
  63. myBook.add("Beet", "233-464-1289");
  64. myBook.add("Asparagus", "454-345-4566");
  65. myBook.add("Leeks", "194-345-3458");
  66. myBook.add("Leeks", "345-209-1095");
  67. myBook.display();
  68. System.out.println("***");
  69. myBook.remove("Asparagus", "305-504-1159");
  70. myBook.removeRecord("Beet");
  71. System.out.println("Phonebook after removing Asparagus' number and Beets' record");
  72. myBook.display();
  73. System.out.println("***");
  74. System.out.println("Looking for Leeks' phone number");
  75. System.out.println(myBook.lookup("Leeks"));
  76. System.out.println("***");
  77. System.out.println("Looking for the owner of number 454-345-4566");
  78. System.out.println(myBook.reverseLookup("454-345-4566"));
  79. System.out.println("***");
  80. System.out.println("Looking for Banana's phone number");
  81. System.out.println(myBook.lookup("Banana"));
  82. }
  83. }