瀏覽代碼

Adding more tests in a second

Brian He 6 年之前
父節點
當前提交
2cb395c3f9

+ 12
- 0
pom.xml 查看文件

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>phonebok</artifactId>
9 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 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 28
- 0
src/main/java/com/zipcodewilmington/phonebook/Person.java 查看文件

@@ -0,0 +1,28 @@
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 查看文件

@@ -1,25 +1,28 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.List;
3 5
 import java.util.Map;
4 6
 import java.util.TreeMap;
5
-import java.util.ArrayList;
6 7
 
7 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 24
     public String lookUp(String name) {
22
-        return phoneBookList.get(name);
25
+        return phoneBookList.get(name).toString();
23 26
     }
24 27
 
25 28
     public String listNames() {
@@ -29,28 +32,29 @@ public class PhoneBook {
29 32
     public String listAll() {
30 33
 
31 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 36
             sb.append(entry.getKey() + entry.getValue());
34 37
             sb.append("\n");
35 38
         }
36 39
         return sb.toString();
37
-
38 40
     }
39 41
 
40 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 45
                 return entry.getKey();
44 46
             }
45 47
         }
46 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 查看文件

@@ -0,0 +1,17 @@
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 查看文件

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