Browse Source

soooo close

Daniel Horowitz 6 years ago
parent
commit
2355aefa13

+ 36
- 27
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,9 +1,6 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-import java.util.Set;
4
-import java.util.TreeMap;
5
-import java.util.TreeSet;
6
-import java.util.Map;
3
+import java.util.*;
7 4
 
8 5
 /**
9 6
  * Created by leon on 1/23/18.
@@ -13,7 +10,7 @@ public class PhoneBook {
13 10
     private String name;
14 11
     private String number;
15 12
 
16
-    TreeMap<String, String> myTree = new TreeMap<String, String>();
13
+    TreeMap<String, ArrayList<String>> myTree = new TreeMap<String, ArrayList<String>>();
17 14
 
18 15
 
19 16
     public PhoneBook(){
@@ -21,30 +18,40 @@ public class PhoneBook {
21 18
     }
22 19
 
23 20
 
24
-    public void addEntry(String name, String number) {
25
-        myTree.put(name, number);
26
-
21
+    public void addEntry(String name, String... number) {
22
+        ArrayList<String> listOfPhoneNumbers = new ArrayList<String>(Arrays.asList(number));
23
+        myTree.put(name, listOfPhoneNumbers);
27 24
 
28 25
     }
29 26
 
27
+    public void additionalPhoneNumberToKey(String name, String number) {
28
+        myTree.get(name).add(number);
29
+    }
30 30
 
31
-    public void remove(String name) {
32
-        myTree.remove(name);
33 31
 
32
+    public void removeNumberFromName(String name, String number) {
33
+        myTree.get(name).remove(number);
34 34
     }
35 35
 
36
-    public String stringLookup(String name) {
37 36
 
38
-       return myTree.get(name);
37
+    public void removeEntry(String name) {
38
+        myTree.remove(name);
39 39
 
40
+    }
41
+
42
+    public String lookupNumber(String name) {
43
+        ArrayList<String> matchingNumbers = myTree.get(name);
44
+        String listedNumbers = "";
45
+        for (String phoneNumbers : matchingNumbers) {
46
+            listedNumbers +=phoneNumbers + "\n";
47
+        }
48
+       return listedNumbers.trim();
40 49
 
41 50
 
42 51
     }
43 52
 
44 53
     public String listNames() {
45 54
 
46
-
47
-
48 55
         Set<String> contacts = myTree.keySet();
49 56
 
50 57
         String allNames = "";
@@ -52,18 +59,19 @@ public class PhoneBook {
52 59
             allNames += contact + "\n";
53 60
 
54 61
         }
55
-        return allNames;
62
+        return allNames.trim();
56 63
     }
57 64
 
58 65
     public String listPhoneBook() {
59 66
 
67
+        Set<String> contacts = myTree.keySet();
60 68
         StringBuilder printBook = new StringBuilder();
61 69
 
62
-        for (Map.Entry<String, String> entry : myTree.entrySet()) {
63
-            String key = entry.getKey();
64
-            String value = entry.getValue();
65
-
66
-            printBook.append(key).append(" = ").append(value).append("\n");
70
+        for (String name : contacts) {
71
+            printBook.append(name).append(": ");
72
+            for (int x = 0; x<myTree.get(name).size(); x++) {
73
+                printBook.append(x).append("  ");
74
+            } printBook.append("\n");
67 75
 
68 76
         }
69 77
         String result = printBook.toString();
@@ -73,13 +81,14 @@ public class PhoneBook {
73 81
 
74 82
     public String reverseLookup(String number) {
75 83
 
76
-        String getName = "";
77
-        for (Map.Entry<String, String> entry : myTree.entrySet()) {
78
-            if (entry.getValue() == number) {
79
-                getName += entry.getKey();
80
-                return getName;
81
-            }
84
+        Set<String> contacts = myTree.keySet();
82 85
 
86
+        for (String name : contacts) {
87
+            for (int y = 0; y < myTree.get(name).size(); y++) {
88
+                if (myTree.get(name).get(y).equals(number)) {
89
+                    return name;
90
+                }
91
+            }
83 92
         }
84 93
 
85 94
 
@@ -105,7 +114,7 @@ public class PhoneBook {
105 114
 
106 115
 
107 116
         phonebookEntry.listPhoneBook();
108
-        System.out.println(phonebookEntry.reverseLookup("4899994190"));
117
+        System.out.println(phonebookEntry.listPhoneBook());
109 118
 
110 119
     }
111 120
 

+ 8
- 8
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -16,7 +16,7 @@ public class PhoneBookTest {
16 16
     public void addTest() {
17 17
         PhoneBook testBook = new PhoneBook();
18 18
         testBook.addEntry("Luke", "6109998787");
19
-        String testNumber = testBook.stringLookup("Luke");
19
+        String testNumber = testBook.lookupNumber("Luke");
20 20
         Assert.assertTrue(testNumber.equals("6109998787"));
21 21
 
22 22
     }
@@ -25,7 +25,7 @@ public class PhoneBookTest {
25 25
     public void addTest1() {
26 26
         PhoneBook testBook = new PhoneBook();
27 27
         testBook.addEntry("Kate", "4846569090");
28
-        String testNumber = testBook.stringLookup("Kate");
28
+        String testNumber = testBook.lookupNumber("Kate");
29 29
         Assert.assertTrue(testNumber.equals("4846569090"));
30 30
 
31 31
     }
@@ -34,8 +34,8 @@ public class PhoneBookTest {
34 34
     public void removeTest1() {
35 35
         PhoneBook testBook = new PhoneBook();
36 36
         testBook.addEntry("Kate", "4846569090");
37
-        testBook.remove("Kate");
38
-        String testNumber = testBook.stringLookup("Kate");
37
+        testBook.removeEntry("Kate");
38
+        String testNumber = testBook.lookupNumber("Kate");
39 39
         Assert.assertEquals(null, testNumber);
40 40
 
41 41
 
@@ -45,7 +45,7 @@ public class PhoneBookTest {
45 45
     public void stringLookupTest() {
46 46
         PhoneBook testBook = new PhoneBook();
47 47
         testBook.addEntry("Kate", "4846569090");
48
-        String testNumber = testBook.stringLookup("Kate");
48
+        String testNumber = testBook.lookupNumber("Kate");
49 49
         Assert.assertFalse(testNumber.equals("4846587091"));
50 50
 
51 51
     }
@@ -54,7 +54,7 @@ public class PhoneBookTest {
54 54
     public void stringLookupTest1() {
55 55
         PhoneBook testBook = new PhoneBook();
56 56
         testBook.addEntry("Kate", "4846569090");
57
-        String testNumber = testBook.stringLookup("Kate");
57
+        String testNumber = testBook.lookupNumber("Kate");
58 58
         Assert.assertTrue(testNumber.equals("4846569090"));
59 59
 
60 60
     }
@@ -79,8 +79,8 @@ public class PhoneBookTest {
79 79
         testBook.addEntry("Adam", "4864569090");
80 80
         testBook.addEntry("Zeke", "4787869090");
81 81
         String actual = testBook.listPhoneBook();
82
-        String expected = "Adam = 4864569090\nKate = 4846569090\nZeke = 4787869090\n";
83
-        Assert.assertEquals(actual, expected);
82
+        String expected = "Adam: 4864569090\nKate: 4846569090\nZeke: 4787869090\n";
83
+        Assert.assertEquals(expected, actual);
84 84
 
85 85
 
86 86
     }