ソースを参照

better but still not finished

Carolynn Vansant 6 年 前
コミット
a09917099d
共有2 個のファイルを変更した24 個の追加27 個の削除を含む
  1. 16
    27
      src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java
  2. 8
    0
      src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java

+ 16
- 27
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java ファイルの表示

@@ -24,45 +24,34 @@ public class PhoneBook {
24 24
 
25 25
     //add a name & number entry
26 26
     public void add(String name, String 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";
27
+        if(!treeMap.containsKey(name)) {
28
+            treeMap.put(name, number);
33 29
         }
34 30
     }
35 31
 
36 32
     //remove a name & number entry
37
-    public String remove(String name) {
38
-        treeMap.remove(name);
39
-        String response = "";
40
-        if(treeMap.remove(name) != null) {
41
-            response = "Name and number removed";
33
+    public void remove(String name) {
34
+        if(treeMap.containsKey(name)) {
35
+            treeMap.remove(name);
42 36
         }
43
-        if(treeMap.remove(name)== null) {
44
-            response = "Invalid entry";
45
-        }
46
-        return response;
47 37
     }
48 38
 
49 39
     //find phone number lookup by 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
-         }
40
+    public void lookup(String name) {
41
+        if(treeMap.containsKey(name)) {
42
+            treeMap.get(name);
43
+        }
44
+
59 45
     }
60 46
 
61 47
     //print out all of the entries in PhoneBook
62 48
     //need to work on code for formatting
63
-    public String display() {
64
-        System.out.println(treeMap.entrySet().toString());
65
-        String printOut = treeMap.entrySet().toString();
49
+    public String display(){
50
+        String printOut = "";
51
+        for(Map.Entry<String, String> entry: treeMap.entrySet()) {
52
+            printOut += entry.getKey() + " " + entry.getValue() + "\n";
53
+        }
54
+        System.out.println(printOut);
66 55
         return printOut;
67 56
     }
68 57
 

+ 8
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java ファイルの表示

@@ -47,6 +47,14 @@ public class PhoneBookTest {
47 47
     }
48 48
 
49 49
     public void testDisplay() {
50
+        //Given
51
+        String expectedNameNumber = "Bob 302-555-1234 \n";
52
+        PhoneBook book = new PhoneBook();
53
+        book.add("Bob", "302-555-1234");
54
+        //When
55
+        String actualNameNumber = book.display();
56
+        //Then
57
+        Assert.assertEquals(expectedNameNumber, actualNameNumber);
50 58
 
51 59
     }
52 60