浏览代码

just wait on it

Jessica Campbell 6 年前
父节点
当前提交
faab5e5a98

+ 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.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 82
- 15
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java 查看文件

@@ -1,34 +1,101 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-import apple.laf.JRSUIUtils;
4
-import com.sun.tools.corba.se.idl.StringGen;
5
-
3
+import java.util.Map;
4
+import java.util.Set;
6 5
 import java.util.TreeMap;
6
+import java.util.TreeSet;
7 7
 
8 8
 /**
9 9
  * Created by leon on 1/23/18.
10 10
  */
11 11
 public class PhoneBook {
12 12
 
13
-    private TreeMap<String, String> tmap;
13
+    private TreeMap<String String> contacts;
14
+
15
+    public PhoneBook() {
16
+
17
+        contacts = new TreeMap<>();
18
+    }
19
+    public void add (String name, String number){
20
+        contacts.put(name, number);
21
+    }
22
+    public String lookup (String name){
23
+        return contacts.get(name);
24
+    }
25
+    public void remove (String name){
26
+        contacts.remove(name);
27
+    }
28
+    public String display (){
29
+        StringBuilder sb = new StringBuilder();
30
+        for (Map.Entry<String, String>entry : contacts.entrySet()){
31
+            String name = entry.getKey();
32
+            String number = entry.getValue();
33
+            sb.append(name + "     " + number + "\n");
34
+        }
35
+        return sb.toString();
36
+    }
37
+    public String reverseLookup (String number){
38
+        for (Map.Entry <String, String>entry: contacts.entrySet()){
39
+            if (entry.getValue().equals(number){
40
+            }
41
+            return null;
42
+        }
43
+    }
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
14 54
 
15
-   PhoneBook(){
16 55
 
17
-      this.tmap =new TreeMap<String, String>();
18 56
 
19
-   }
20 57
 
21
-   public void addAContact(String name, String number) {
22
-       this.tmap.put(name, number);
23
-   }
24 58
 
25
-   public void removeAContact(String name, String number){
26
-       this.tmap.remove(name, number);
27
-   }
28
-    public void lookupContact (String name, String number){
29 59
 
60
+
61
+
62
+
63
+
64
+
65
+
66
+    public void addAContact(String name, String number) {
67
+        tmap.put(name, number);
68
+    }
69
+
70
+    public String listOfAllNames(){
71
+        StringBuilder result = new StringBuilder();
72
+        for (Map.Entry<String, String> entry: tmap.entrySet()){
73
+            result.append(String.format("%-20s %8s\n", entry.getKey(), entry.getValue()));
74
+            //formats the way a string will print out on console^^^^^^
75
+        }
76
+        return String.valueOf(result);
77
+    }
78
+
79
+    public void removeAContact(String name) {
80
+        tmap.remove(name);
81
+    }
82
+
83
+    public String lookupContactName(String name) {
84
+        if (tmap.containsKey(name)) {
85
+            return tmap.get(name);
86
+        } else {
87
+            return "This Name Does Not Exist";
88
+        }
30 89
     }
31 90
 
32 91
 
33
-       }
92
+
93
+    public reverseLookUpNumber (String number){
94
+
95
+        }
96
+
97
+
98
+}
99
+
100
+
34 101
 

+ 59
- 3
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java 查看文件

@@ -1,21 +1,77 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Before;
3 5
 import org.junit.Test;
4 6
 
7
+import java.util.TreeMap;
8
+
5 9
 /**
6 10
  * Created by leon on 1/23/18.
7 11
  */
8 12
 public class PhoneBookTest {
13
+    // constructor
14
+    String name = "Jess";
15
+    String number = "1234567";
16
+    String nameA = "Alice";
17
+    String numberA = "1111111";
18
+    String nameB = "Sean";
19
+    String numberB = "2222222";
9 20
 
10
-    @Test
11
-    public void firstContactName() {
21
+    private PhoneBook phoneBook = new PhoneBook();
22
+    // instance
12 23
 
13
-    }
14 24
 
25
+   @Before
26
+   public void setUp(){
27
+       phoneBook = new PhoneBook();
28
+   }
29
+
30
+   @Test
31
+   public boolean testAddAContact() {
32
+    //instance of new phone book
33
+//    phoneBook.addAContact(name, number);
34
+//    Assert.assertTrue(phoneBook.addAContact(name, number).equals(name, number));
35
+       TreeMap.containsKey(String);
36
+       Assert.assertTrue(phoneBook.addAContact(name));
15 37
 
38
+   }
39
+@Test
40
+    public void removeAContact(String name){
41
+       //Given
42
+        String expected = "Null";
16 43
 
44
+        //When
45
+        phoneBook.addAContact(expected);
46
+        tmap.add(name);
47
+        //first must add a name to map
48
+
49
+        phoneBook.lookupContactName(expected);
50
+        String actual = phoneBook.removeAContact();
51
+        tmap.remove(name);
52
+        //Then
53
+        Assert.assertTrue(expected, actual);
54
+    }
55
+    @Test
56
+    public void testLookupContactName (){
57
+       //Given
58
+       phoneBook.lookupContactName("Jess");
59
+       String expected = "1234567";
17 60
 
61
+       //When
62
+       String actual = phoneBook.lookupContactName("Jess");
18 63
 
64
+       //Then
65
+       Assert.assertEquals(expected, actual);
66
+    }
19 67
 
68
+    @Test
69
+    public void printOut(){
70
+        System.out.println(phoneBook.listOfAllNames());
20 71
 
72
+    }
21 73
 }
74
+
75
+
76
+
77
+