Browse Source

Part 2 of PhoneBook

Eric Cordell 6 years ago
parent
commit
fe7c5dff10

+ 10
- 3
src/main/java/com/zipcodewilmington/phonebook/Person.java View File

@@ -3,11 +3,10 @@ package com.zipcodewilmington.phonebook;
3 3
 class Person {
4 4
 
5 5
     private String personName;
6
-
7 6
     private String phoneNumber;
8 7
 
9
-    public Person(String name, String phoneNumber){
10
-        this.personName = name;
8
+    public Person(String personName, String phoneNumber) {
9
+        this.personName = personName;
11 10
         this.phoneNumber = phoneNumber;
12 11
     }
13 12
 
@@ -15,7 +14,15 @@ class Person {
15 14
         return personName;
16 15
     }
17 16
 
17
+    public void setPersonName(String personName) {
18
+        this.personName = personName;
19
+    }
20
+
18 21
     public String getPhoneNumber() {
19 22
         return phoneNumber;
20 23
     }
24
+
25
+    public void setPhoneNumber(String phoneNumber) {
26
+        this.phoneNumber = phoneNumber;
27
+    }
21 28
 }

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

@@ -1,6 +1,5 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-import java.util.ArrayList;
4 3
 import java.util.Map;
5 4
 import java.util.Set;
6 5
 import java.util.TreeMap;
@@ -10,45 +9,58 @@ import java.util.TreeMap;
10 9
  */
11 10
 public class PhoneBook {
12 11
 
13
-    TreeMap<String, Person> phoneBook = new TreeMap<String, Person>();
12
+    TreeMap<String, String> phoneBook = new TreeMap<String, String>();
14 13
 
15 14
 
16 15
     public void addEntryToPhoneBook(String name, String phoneNumber) {
17
-        phoneBook.put(name, new Person(name, phoneNumber));
16
+        phoneBook.put(name, phoneNumber);
18 17
     }
19 18
 
20 19
 
21
-    public void removePhoneBookEntryFromPhoneBook(String name) {
22
-        phoneBook.remove(name);
20
+    public void removeEntryFromPhoneBook(String name, String phoneNumber) {
21
+        phoneBook.remove(name, phoneNumber);
23 22
     }
24 23
 
25
-    public String[] listNames() {
26
-        return null;
24
+    public String lookUp(String name) {
25
+        return phoneBook.get(name);
26
+    }
27
+
28
+    public String listNames() {
29
+        String completeListOfNames = "";
30
+        for (String key : phoneBook.keySet()) {
31
+            completeListOfNames += key + "\n";
32
+        }
33
+
34
+        return completeListOfNames;
35
+    }
36
+
37
+    public String listNumbers() {
38
+        String completeListOfNumbers = "";
39
+        for (Map.Entry<String, String> entry : phoneBook.entrySet()) {
40
+            completeListOfNumbers += entry.getValue() + "\n";
41
+        }
42
+        return completeListOfNumbers;
27 43
     }
28 44
 
45
+
29 46
     public String entryListAll() {
30 47
         String fullList = "";
31
-        Set<String> keys = phoneBook.keySet();
32
-        for (Map.Entry<String, Person> entry: phoneBook.entrySet()) {
33
-            fullList += entry.getKey() + " : " + entry.getValue().getPhoneNumber();
48
+        for (Map.Entry<String, String> entry : phoneBook.entrySet()) {
49
+            fullList += entry.getKey() + " : " + entry.getValue() + "\n"; //reverse lookup
34 50
 
35 51
         }
36 52
         return fullList;
37 53
     }
38
-    public static void main(String[] args) {
39
-        PhoneBook nameNumber = new PhoneBook();
40
-        nameNumber.addEntryToPhoneBook("eric", "3025551111 \n");
41
-        nameNumber.addEntryToPhoneBook("eyan", "3025552222 \n");
42
-        nameNumber.entryListAll();
43
-        System.out.println(nameNumber.entryListAll());
44 54
 
55
+    public String reverseLookup(String numberToLookUp) {
56
+        for (Map.Entry<String,String > e : phoneBook.entrySet()) {
57
+            if (e.getValue().equals(numberToLookUp)){
58
+                return e.getKey();
59
+            }
60
+        }return null;
45 61
     }
46 62
 
47 63
 
48
-    public Person lookUp(String name) {
49
-        return phoneBook.get(name);
50
-
51
-    }
52 64
 
53 65
 
54 66
 }

+ 61
- 2
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -2,21 +2,80 @@ package com.zipcodewilmington.phonebook;
2 2
 
3 3
 
4 4
 import org.junit.Assert;
5
+import org.junit.Before;
5 6
 import org.junit.Test;
6 7
 
7 8
 /**
8 9
  * Created by leon on 1/23/18.
9 10
  */
10 11
 public class PhoneBookTest {
12
+    PhoneBook phoneBook;
13
+
14
+    @Before
15
+    public void setUp() {
16
+        phoneBook = new PhoneBook();
17
+        phoneBook.addEntryToPhoneBook("Eric", "3021234567");
18
+        phoneBook.addEntryToPhoneBook("Bob", "3029999999");
19
+    }
11 20
 
12 21
     @Test
13 22
     public void testAddPhoneBookEntryToPhoneBook() {
14
-        PhoneBook phoneBook = new PhoneBook();
15 23
         String phoneNumber = "3025551111";
16 24
         phoneBook.addEntryToPhoneBook("eric", phoneNumber);
17
-        String actual = phoneBook.lookUp("eric").getPhoneNumber();
25
+        String actual = phoneBook.lookUp("eric");
18 26
         Assert.assertEquals(phoneNumber, actual);
19 27
     }
20 28
 
29
+    @Test
30
+    public void testRemoveEntryFromPhoneBook() {
31
+        String phoneNumber = "3025551111";
32
+        String name = "eric";
33
+        phoneBook.addEntryToPhoneBook(name, phoneNumber);
34
+        phoneBook.removeEntryFromPhoneBook("eric", phoneNumber);
35
+
36
+        String actual = phoneBook.lookUp("eric");
37
+        Assert.assertEquals(null, actual);
38
+    }
39
+
40
+    @Test
41
+    public void testListNames() {
42
+        String expected = "Bob\n" +
43
+                "Eric\n";
44
+        String actual = phoneBook.listNames();
45
+
46
+        Assert.assertEquals(expected, actual);
47
+    }
48
+
49
+    @Test
50
+    public void testListNumber() {
51
+
52
+
53
+        String expected = "3029999999\n" +
54
+                "3021234567\n";
55
+        String actual = phoneBook.listNumbers();
56
+
57
+        Assert.assertEquals(expected, actual);
58
+    }
59
+
60
+    @Test
61
+    public void testEntryListAll() {
62
+
63
+
64
+        String expected = "Bob : 3029999999\n" +
65
+                "Eric : 3021234567\n";
66
+        String actual = phoneBook.entryListAll();
67
+
68
+        Assert.assertEquals(expected, actual);
69
+    }
70
+
71
+
72
+
73
+    @Test
74
+    public void testReverseLookup() {
75
+        String expected = "Bob";
76
+
77
+        String actual = phoneBook.reverseLookup("3029999999");
78
+        Assert.assertEquals(expected,actual);
79
+    }
21 80
 
22 81
 }