Browse Source

not completed yet

Carolynn Vansant 6 years ago
parent
commit
0fb653f399

+ 7
- 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
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>4.12</version>
15
+        </dependency>
16
+    </dependencies>
10
 
17
 
11
 
18
 
12
 </project>
19
 </project>

+ 32
- 6
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

8
  * Created by leon on 1/23/18.
8
  * Created by leon on 1/23/18.
9
  */
9
  */
10
 public class PhoneBook {
10
 public class PhoneBook {
11
+
11
     //instance variable
12
     //instance variable
12
     private TreeMap<String, String> treeMap;
13
     private TreeMap<String, String> treeMap;
13
 
14
 
22
     }
23
     }
23
 
24
 
24
     //add a name & number entry
25
     //add a name & number entry
25
-    public void addNameNumberEntry(String name, String number) {
26
+    public void add(String name, String number) {
26
         treeMap.put(name, number);
27
         treeMap.put(name, number);
28
+        String confirm = "";
29
+        if(treeMap.put(name, number)!= null) {
30
+            confirm = name + " and " + number + " are now in the database";
31
+        } else {
32
+            confirm = "Error: " + name + " is already in the database";
33
+        }
27
     }
34
     }
28
 
35
 
29
     //remove a name & number entry
36
     //remove a name & number entry
30
-    public void removeEntry(String name) {
37
+    public String remove(String name) {
31
         treeMap.remove(name);
38
         treeMap.remove(name);
39
+        String response = "";
40
+        if(treeMap.remove(name) != null) {
41
+            response = "Name and number removed";
42
+        }
43
+        if(treeMap.remove(name)== null) {
44
+            response = "Invalid entry";
45
+        }
46
+        return response;
32
     }
47
     }
33
 
48
 
34
     //find phone number lookup by name
49
     //find phone number lookup by name
35
-    public String lookupByName(String name) {
36
-         return treeMap.get(name);
50
+    public String lookup(String name) {
51
+         String lookup = treeMap.get(name);
52
+         String notFound = name + " is not in the directory";
53
+         if(lookup != null) {
54
+             return lookup;
55
+         }
56
+         else {
57
+             return notFound;
58
+         }
37
     }
59
     }
38
 
60
 
39
     //print out all of the entries in PhoneBook
61
     //print out all of the entries in PhoneBook
40
-    public Set<Map.Entry<String,String>> printOutPhoneBook() {
41
-        return treeMap.entrySet();
62
+    //need to work on code for formatting
63
+    public String display() {
64
+        System.out.println(treeMap.entrySet().toString());
65
+        String printOut = treeMap.entrySet().toString();
66
+        return printOut;
42
     }
67
     }
43
 
68
 
44
 
69
 
45
 
70
 
46
 }
71
 }
72
+

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

1
 package com.zipcodewilmington.phonebook;
1
 package com.zipcodewilmington.phonebook;
2
 
2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.TreeMap;
7
+
3
 /**
8
 /**
4
  * Created by leon on 1/23/18.
9
  * Created by leon on 1/23/18.
5
  */
10
  */
6
 public class PhoneBookTest {
11
 public class PhoneBookTest {
7
 
12
 
13
+    public void testDefaultConstructor() {
14
+
15
+        PhoneBook book = new PhoneBook();
16
+
17
+
18
+
19
+    }
20
+
21
+    public void testConstructorWithArgument() {
22
+
23
+    }
24
+
25
+    public void testAdd() {
26
+        PhoneBook book = new PhoneBook();
27
+        book.add("Bob", "302-555-1234");
28
+        Assert.assertNotNull(book);
29
+    }
30
+
31
+    public void testRemove() {
32
+
33
+    }
34
+
35
+    public void testLookup(){
36
+
37
+        //Given
38
+        String expectedNumber = "302-555-1234";
39
+        PhoneBook book = new PhoneBook();
40
+        book.add("Bob", "302-555-1234");
41
+
42
+        //When
43
+        book.lookup("Bob");
44
+
45
+        //Then
46
+        Assert.assertEquals(expectedNumber, "302-555-1234");
47
+    }
48
+
49
+    public void testDisplay() {
50
+
51
+    }
52
+
53
+
8
 }
54
 }