Browse Source

Adding more tests in a second

Brian He 6 years ago
parent
commit
2cb395c3f9

+ 12
- 0
pom.xml View File

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>phonebok</artifactId>
8
     <artifactId>phonebok</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.8</source>
17
+                    <target>1.8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10
     <dependencies>
22
     <dependencies>
11
         <dependency>
23
         <dependency>
12
             <groupId>junit</groupId>
24
             <groupId>junit</groupId>

+ 28
- 0
src/main/java/com/zipcodewilmington/phonebook/Person.java View File

1
+/*package com.zipcodewilmington.phonebook;
2
+
3
+public class Person {
4
+    public final String name;
5
+    public final PhoneNumbers phoneNumbers;
6
+
7
+    public Person(String name, PhoneNumbers phoneNumbers) {
8
+        this.name = name;
9
+        this.phoneNumbers = phoneNumbers;
10
+    }
11
+
12
+    public void add(String phoneNumber) {
13
+        phoneNumbers.addToContacts(phoneNumber);
14
+    }
15
+
16
+    public void remove(String phoneNumber) {
17
+        phoneNumbers.removeFromContacts(phoneNumber);
18
+    }
19
+
20
+    public String getName() {
21
+        return name;
22
+    }
23
+
24
+    public PhoneNumbers getPhoneNumber() {
25
+        return phoneNumbers;
26
+    }
27
+}
28
+*/

+ 23
- 19
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

1
 package com.zipcodewilmington.phonebook;
1
 package com.zipcodewilmington.phonebook;
2
 
2
 
3
+import java.util.ArrayList;
4
+import java.util.List;
3
 import java.util.Map;
5
 import java.util.Map;
4
 import java.util.TreeMap;
6
 import java.util.TreeMap;
5
-import java.util.ArrayList;
6
 
7
 
7
 public class PhoneBook {
8
 public class PhoneBook {
8
 
9
 
9
-    TreeMap<String, String> phoneBookList = new TreeMap<String, String>();
10
+    TreeMap<String, List<String>> phoneBookList = new TreeMap<String, List<String>>();
10
 
11
 
11
-    public void addEntry(String name, String number) {
12
-        phoneBookList.put(name, number);
13
-        //Look to see if person exists
14
-        //Get array list value
12
+    public void addEntry(String name, String ...phoneNumber) {
13
+        List<String> numberList = new ArrayList<String>();
14
+        for(String number : phoneNumber) {
15
+            numberList.add(number);
16
+        }
17
+        phoneBookList.put(name, numberList);
15
     }
18
     }
16
 
19
 
17
-    public void removeEntry(String name, String number) {
18
-        phoneBookList.remove(name, number);
20
+    public void removeEntry(String name) {
21
+        phoneBookList.remove(name);
19
     }
22
     }
20
 
23
 
21
     public String lookUp(String name) {
24
     public String lookUp(String name) {
22
-        return phoneBookList.get(name);
25
+        return phoneBookList.get(name).toString();
23
     }
26
     }
24
 
27
 
25
     public String listNames() {
28
     public String listNames() {
29
     public String listAll() {
32
     public String listAll() {
30
 
33
 
31
         StringBuilder sb = new StringBuilder();
34
         StringBuilder sb = new StringBuilder();
32
-        for(Map.Entry<String, String> entry : phoneBookList.entrySet()) {
35
+        for(Map.Entry<String, List<String>> entry : phoneBookList.entrySet()) {
33
             sb.append(entry.getKey() + entry.getValue());
36
             sb.append(entry.getKey() + entry.getValue());
34
             sb.append("\n");
37
             sb.append("\n");
35
         }
38
         }
36
         return sb.toString();
39
         return sb.toString();
37
-
38
     }
40
     }
39
 
41
 
40
     public String reverseLookUp(String number) {
42
     public String reverseLookUp(String number) {
41
-        for(Map.Entry<String, String>entry : phoneBookList.entrySet()) {
42
-            if(entry.getValue() == number) {
43
+        for(Map.Entry<String, List<String>> entry : phoneBookList.entrySet()) {
44
+            if(entry.getValue().contains(number)) {
43
                 return entry.getKey();
45
                 return entry.getKey();
44
             }
46
             }
45
         }
47
         }
46
         return "That number is not in the phone book";
48
         return "That number is not in the phone book";
47
     }
49
     }
48
 
50
 
49
-    public static void main(String[] args) {
50
-        PhoneBook pb1 = new PhoneBook();
51
-        pb1.addEntry("Brian", "12345678");
52
-        pb1.addEntry("Lawrence", "23458984");
53
-        pb1.addEntry("Anthony", "12381927");
54
-
51
+    public void removeOneNumber(String name, String number) {
52
+        int i = 0;
53
+        for (String phoneNumber : phoneBookList.get(name)) {
54
+            if(phoneNumber == number) {
55
+                phoneBookList.get(name).remove(i);
56
+            }
57
+            i++;
58
+        }
55
     }
59
     }
56
 }
60
 }

+ 17
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneNumbers.java View File

1
+/*package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
6
+public class PhoneNumbers {
7
+    List<String> contactList = new ArrayList<String>();
8
+
9
+    public void addToContacts(String phoneNumber) {
10
+        contactList.add(phoneNumber);
11
+    }
12
+
13
+    public void removeFromContacts(String phoneNumber) {
14
+        contactList.remove(phoneNumber);
15
+    }
16
+}
17
+*/

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

38
     public void removeEntryTest() {
38
     public void removeEntryTest() {
39
         String name = "Person 1";
39
         String name = "Person 1";
40
         String number = "8942418";
40
         String number = "8942418";
41
-        phoneBookTest.removeEntry(name, number);
41
+        phoneBookTest.removeEntry(name);
42
         String expected = null;
42
         String expected = null;
43
         String actual = phoneBookTest.lookUp(name);
43
         String actual = phoneBookTest.lookUp(name);
44
         Assert.assertEquals(expected, actual);
44
         Assert.assertEquals(expected, actual);
49
     public void removeEntryTest2() {
49
     public void removeEntryTest2() {
50
         String name = "Person 2";
50
         String name = "Person 2";
51
         String number = "12387874";
51
         String number = "12387874";
52
-        phoneBookTest.removeEntry(name, number);
52
+        phoneBookTest.removeEntry(name);
53
         String expected = null;
53
         String expected = null;
54
         String actual = phoneBookTest.lookUp(name);
54
         String actual = phoneBookTest.lookUp(name);
55
         Assert.assertEquals(expected, actual);
55
         Assert.assertEquals(expected, actual);
107
         Assert.assertEquals(expected, actual);
107
         Assert.assertEquals(expected, actual);
108
 
108
 
109
     }
109
     }
110
+
111
+    @Test
112
+    public void removeOneNumberTest() {
113
+        String name = "Brian";
114
+
115
+    }
110
 }
116
 }