瀏覽代碼

Merge dfc60d5595ebcf7ca501c3b60a3b928aa2b31064 into 74a9673e36c7d25c28315cb54247a91792f8fbc0

wulawrence 6 年之前
父節點
當前提交
22c06e7a2e
沒有帳戶連結到提交者的電子郵件

+ 19
- 0
pom.xml 查看文件

@@ -7,6 +7,25 @@
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>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>RELEASE</version>
27
+        </dependency>
28
+    </dependencies>
10 29
 
11 30
 
12 31
 </project>

+ 31
- 0
src/main/java/com/zipcodewilmington/phonebook/ContactEntry.java 查看文件

@@ -0,0 +1,31 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+
4
+import java.util.ArrayList;
5
+
6
+public class ContactEntry {
7
+
8
+    private String name;
9
+    private ArrayList<String> phoneNumbers = new ArrayList<String>();
10
+
11
+    public String getName(){
12
+
13
+        return name;
14
+    }
15
+
16
+    public void setName(String name){
17
+        this.name = name;
18
+    }
19
+
20
+    public void addNumber(String phoneNumber){
21
+        addNumberToEntry(phoneNumber);
22
+    }
23
+
24
+    public void addNumberToEntry(String phoneNumber){
25
+        this.phoneNumbers.add(phoneNumber);
26
+    }
27
+
28
+    public ContactEntry(String name){
29
+        this.name = name;
30
+    }
31
+}

+ 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
+    private String name;
5
+    private String number;
6
+
7
+
8
+    public Person(){
9
+        setName("");
10
+        setNumber("");
11
+    }
12
+
13
+    public String getName() {
14
+        return name;
15
+    }
16
+
17
+    public void setName(String name) {
18
+        this.name = name;
19
+    }
20
+
21
+    public String getNumber() {
22
+        return number;
23
+    }
24
+
25
+    public void setNumber(String number) {
26
+        this.number = number;
27
+    }
28
+}

+ 95
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java 查看文件

@@ -1,7 +1,102 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import com.sun.xml.internal.bind.v2.util.QNameMap;
4
+
5
+import java.util.*;
6
+
3 7
 /**
4 8
  * Created by leon on 1/23/18.
5 9
  */
6 10
 public class PhoneBook {
11
+
12
+private String nameEntry;
13
+private String numberEntry;
14
+
15
+
16
+
17
+   private TreeMap<String, ArrayList<String>> phoneBook = new TreeMap<String, ArrayList<String>>();
18
+
19
+     public void addEntry(String nameEntry, String...numberEntry){
20
+        ArrayList<String> phoneList = new ArrayList<String>();
21
+        for (String number : numberEntry){
22
+            phoneList.add(number);
23
+        }
24
+         phoneBook.put(nameEntry, phoneList);
25
+
26
+    }
27
+
28
+//    public String addNewNumbertoEntry(String nameEntry, String numberEntry) {
29
+//         ContactEntry entry = new ContactEntry(nameEntry);
30
+//         entry.addNumber((entryLookup(nameEntry)));
31
+//         entry.addNumber(numberEntry);
32
+//         addEntry(nameEntry, numberEntry);
33
+//         return entryLookup(nameEntry);
34
+//    }
35
+
36
+     public void removeEntry(String nameEntry , String numberEntry){
37
+        phoneBook.remove(nameEntry, numberEntry);
38
+
39
+     }
40
+
41
+     protected String entryLookup(String nameEntry){
42
+        return phoneBook.get(nameEntry).toString();
43
+
44
+     }
45
+
46
+    public String entryListNames() {
47
+
48
+        return phoneBook.keySet().toString();
49
+
50
+        }
51
+
52
+//    public String entryListNumbers() {
53
+//        String fullListNumbers = "";
54
+//        Set<String> keys = phoneBook.keySet();
55
+//        for (String key: keys){
56
+//            fullListNumbers += phoneBook.get(key) + "\n";
57
+//
58
+//        }
59
+//        return fullListNumbers;
60
+//    }
61
+
62
+     public String entryListAll() {
63
+        String fullListAll = "";
64
+         Set<String> keys = phoneBook.keySet();
65
+        for (String key: keys){
66
+            fullListAll += key + " : " + phoneBook.get(key) + "\n";
67
+
68
+        }
69
+        return fullListAll;
70
+     }
71
+
72
+     public String reverseLookup(String numberEntry){
73
+         Set<String> keys = phoneBook.keySet();
74
+         for (String key: keys){
75
+             if(phoneBook.get(key).contains(numberEntry)) {
76
+                 return key;
77
+             }
78
+         }
79
+         return "That number does not exist";
80
+     }
81
+
82
+     public void removeOneNumberfromEntry(String nameEntry, String numberEntry){
83
+         int counter = 0;
84
+         for (String phoneNum : phoneBook.get(nameEntry)){
85
+             if (phoneNum.equals(numberEntry)){
86
+                 phoneBook.get(nameEntry).remove(counter);
87
+             }
88
+         } counter++;
89
+     }
90
+
91
+
92
+    public static void main(String[] args) {
93
+       PhoneBook nameNumber = new PhoneBook();
94
+       nameNumber.addEntry("Albert", "111111111");
95
+       nameNumber.addEntry("Bobby", "222222222");
96
+       nameNumber.addEntry("Albert", "333333333", "444444444");
97
+      //  System.out.println(nameNumber.entryLookup("Albert"));
98
+        System.out.println(nameNumber.entryListAll());
99
+        System.out.println(nameNumber.reverseLookup("333333333"));
100
+    }
101
+
7 102
 }

+ 129
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java 查看文件

@@ -1,7 +1,136 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
3 7
 /**
4 8
  * Created by leon on 1/23/18.
5 9
  */
6 10
 public class PhoneBookTest {
11
+
12
+   private PhoneBook testPhoneBook;
13
+
14
+   @Before
15
+   public void createPhone(){
16
+      this.testPhoneBook = new PhoneBook();
17
+   }
18
+
19
+   @Test
20
+   public void testAddEntry(){
21
+      String number = "111111111";
22
+      String name = "Albert";
23
+      testPhoneBook.addEntry(name, number);
24
+      String actual = testPhoneBook.entryLookup(name);
25
+      String expected = "[111111111]";
26
+      Assert.assertEquals(expected, actual);
27
+
28
+
29
+   }
30
+
31
+   @Test
32
+   public void testAddEntry1(){
33
+      String expected = null;
34
+      String name = "Albert";
35
+      testPhoneBook.addEntry(name, expected);
36
+      String actual = testPhoneBook.entryLookup(name);
37
+      Assert.assertFalse("You did not enter a number", false);
38
+   }
39
+
40
+
41
+   @Test (expected =  NullPointerException.class)
42
+   public void testRemoveEntry(){
43
+      String name = "Albert";
44
+      String number = "111111111";
45
+      testPhoneBook.removeEntry(name, number);
46
+      String actual = testPhoneBook.entryLookup(name);
47
+
48
+
49
+   }
50
+
51
+   @Test (expected = NullPointerException.class)
52
+   public void testRemoveEntry1(){
53
+      String expected = "Albert";
54
+      String name = "Albert";
55
+      String number = "111111111";
56
+      testPhoneBook.removeEntry(name, number);
57
+      String actual = testPhoneBook.entryLookup(name);
58
+   }
59
+
60
+   @Test
61
+   public void testEntryLookup(){
62
+      String name = "Albert";
63
+      String number = "111111111";
64
+      testPhoneBook.addEntry("Albert", "111111111");
65
+      testPhoneBook.entryLookup(name);
66
+      String expected = "[111111111]";
67
+      String actual = testPhoneBook.entryLookup(name);
68
+      Assert.assertEquals(expected, actual);
69
+
70
+   }
71
+
72
+
73
+   @Test
74
+   public void testEntryListNames(){
75
+      String name = "Albert";
76
+      String number = "111111111";
77
+      testPhoneBook.addEntry(name, number);
78
+      String name1 = "Bobby";
79
+      String number1 = "222222222";
80
+      testPhoneBook.addEntry(name1, number1);
81
+      String name2 = "Catherine";
82
+      String number2 = "333333333";
83
+      testPhoneBook.addEntry(name2, number2);
84
+
85
+      String expected = "[Albert, Bobby, Catherine]";
86
+      String actual = testPhoneBook.entryListNames();
87
+      Assert.assertEquals(expected, actual);
88
+
89
+   }
90
+
91
+//   @Test
92
+//   public void testEntryListNumbers(){
93
+//
94
+//   }
95
+
96
+   @Test
97
+   public void testEntryListAll(){
98
+      String name = "Albert";
99
+      String number = "111111111";
100
+      testPhoneBook.addEntry(name, number);
101
+      String name1 = "Bobby";
102
+      String number1 = "222222222";
103
+      testPhoneBook.addEntry(name1, number1);
104
+      String name2 = "Catherine";
105
+      String number2 = "333333333";
106
+      testPhoneBook.addEntry(name2, number2);
107
+
108
+      String expected = "Albert : [111111111]\nBobby : [222222222]\nCatherine : [333333333]\n";
109
+      String actual = testPhoneBook.entryListAll();
110
+      Assert.assertEquals(expected, actual);
111
+   }
112
+
113
+   @Test
114
+   public void testReverseEntry(){
115
+      String name = "Albert";
116
+      String number = "111111111";
117
+      testPhoneBook.addEntry(name, number);
118
+      String expected = "Albert";
119
+      String actual = testPhoneBook.reverseLookup(number);
120
+      Assert.assertEquals(expected, actual);
121
+   }
122
+
123
+   @Test
124
+   public void testremoveOnePhoneNum(){
125
+      String name = "Albert";
126
+      String number = "111111111";
127
+      String number1 = "222222222";
128
+      testPhoneBook.addEntry(name, number, number1);
129
+      testPhoneBook.removeOneNumberfromEntry("Albert", "111111111");
130
+      String expected = "[222222222]";
131
+      String actual = testPhoneBook.entryLookup("Albert");
132
+      Assert.assertEquals(expected, actual);
133
+
134
+   }
135
+
7 136
 }