Browse Source

finished all except display() all entries in part I

Luis Romero 6 years ago
parent
commit
cf7e3e14dc

+ 23
- 9
src/main/java/com/zipcodewilmington/phonebook/Person.java View File

@@ -1,29 +1,43 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.ArrayList;
4
+
3 5
 public class Person {
4 6
 
5 7
     private String name;
6
-    private String phoneNumber;
8
+    public ArrayList<String> phoneNumberList;
7 9
 
8
-    public Person(String name, String phoneNumber) {
10
+    public Person(String name, String... phoneNumbers) {
9 11
         this.name = name;
10
-        this.phoneNumber = phoneNumber;
12
+        this.phoneNumberList = new ArrayList<String>();
13
+        for (String phoneNumber : phoneNumbers) {
14
+            this.phoneNumberList.add(phoneNumber);
15
+        }
11 16
     }
12 17
 
13 18
     public void setName(String name) {
14 19
         this.name = name;
15 20
     }
16 21
 
17
-    public void setPhoneNumber(String phoneNumber) {
18
-        this.phoneNumber = phoneNumber;
19
-    }
20
-
21 22
     public String getName() {
22 23
         return name;
23 24
     }
24 25
 
25
-    public String getPhoneNumber() {
26
-        return phoneNumber;
26
+    public void addPhoneNumbers(String... phoneNumbersToAdd) {
27
+        for (String phoneNumber : phoneNumbersToAdd) {
28
+            this.phoneNumberList.add(phoneNumber);
29
+        }
30
+
27 31
     }
28 32
 
33
+    public void removePhoneNumbers(String... phoneNumbersToRemove) {
34
+        for (String phoneNumber : phoneNumbersToRemove) {
35
+            this.phoneNumberList.remove(phoneNumber);
36
+        }
37
+
38
+    }
39
+
40
+    // method to return phone number list?
41
+
42
+
29 43
 }

+ 9
- 5
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -13,7 +13,8 @@ public class PhoneBook {
13 13
 
14 14
     public PhoneBook() {
15 15
         this.hashMap = new HashMap<String, Person>();
16
-        this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
16
+        this.entryList = new ArrayList<String>();
17
+        //this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
17 18
     }
18 19
 
19 20
     //PART I
@@ -21,20 +22,23 @@ public class PhoneBook {
21 22
         return hashMap.get(name);
22 23
     }
23 24
 
24
-    public void addEntry(String name, String phoneNumber) {
25
-        hashMap.put(name, new Person(name, phoneNumber));
25
+    public void addEntry(String name, String... phoneNumbers) {
26
+        hashMap.put(name, new Person(name, phoneNumbers));
26 27
     }
27 28
 
28
-    public void removeEntry(String name) {
29
+    // Changed method name for Part III
30
+    public void removeRecord(String name) {
29 31
         hashMap.remove(name);
30 32
     }
31 33
 
34
+    /*
32 35
     public ArrayList<String> getEntryList() { //names
33 36
         for (Map.Entry<String, Person> entry: hashMap.entrySet()) {
34 37
             entryList.add(entry.getKey() + " : " + entry.getValue().getPhoneNumber());
35 38
         }
36 39
         return entryList;
37 40
     }
41
+    */
38 42
 
39 43
     public String[] entriesList() { //names and phone numbers
40 44
         return new String[0];
@@ -43,7 +47,7 @@ public class PhoneBook {
43 47
     //PART II
44 48
     public String reverseLookup(String phoneNumber) { //lookup names from phone numbers
45 49
         for (String name : hashMap.keySet()) {
46
-            if (hashMap.get(name).getPhoneNumber() == phoneNumber) {
50
+            if (hashMap.get(name).phoneNumberList.contains(phoneNumber) == true) {
47 51
                 return name;
48 52
                 //break;
49 53
             }

+ 31
- 15
src/test/java/com/zipcodewilmington/phonebook/PersonTest.java View File

@@ -17,8 +17,9 @@ public class PersonTest {
17 17
         this.person = new Person("John Smith", "(111) 222-3333");
18 18
     }
19 19
 
20
+
20 21
     @Test
21
-    public void testSetName() {
22
+    public void setNameTest() {
22 23
         String expected = "John Smith";
23 24
         person.setName(expected);
24 25
         String actual = person.getName();
@@ -26,25 +27,40 @@ public class PersonTest {
26 27
     }
27 28
 
28 29
     @Test
29
-    public void testSetPhoneNumber() {
30
-        String expected = "(111) 222-3333";
31
-        person.setPhoneNumber(expected);
32
-        String actual = person.getPhoneNumber();
33
-        Assert.assertEquals(expected, actual);
34
-    }
35
-
36
-    @Test
37
-    public void testGetName() {
30
+    public void getNameTest() {
38 31
         String expected = "John Smith";
39 32
         String actual = person.getName();
40 33
         Assert.assertEquals(expected, actual);
41 34
     }
42 35
 
43
-    @Test
44
-    public void testGetPhoneNumber() {
45
-        String expected = "(111) 222-3333";
46
-        String actual = person.getPhoneNumber();
47
-        Assert.assertEquals(expected, actual);
36
+    public void addPhoneNumbersTest() {
37
+        this.person = new Person("John Smith", "(000) 000-0000");
38
+        String expectedPhoneNumberToAdd1 = "(111) 111-1111";
39
+        String expectedPhoneNumberToAdd2 = "(222) 222-2222";
40
+
41
+        this.person.addPhoneNumbers(expectedPhoneNumberToAdd1, expectedPhoneNumberToAdd2);
42
+
43
+        String actualPhoneNumberAdded1 = this.person.phoneNumberList.get(1);
44
+        String actualPhoneNumberAdded2 = this.person.phoneNumberList.get(2);
45
+
46
+        Assert.assertEquals(expectedPhoneNumberToAdd1, actualPhoneNumberAdded1);
47
+        Assert.assertEquals(expectedPhoneNumberToAdd2, actualPhoneNumberAdded2);
48
+
49
+    }
50
+
51
+    public void removePhoneNumbersTest() {
52
+        this.person = new Person("John Smith", "(000) 000-0000", "(111) 111-1111", "(222) 222-2222");
53
+        String expectedPhoneNumberToDelete1 = "(111) 111-1111";
54
+        String expectedPhoneNumberToDelete2 = "(222) 222-2222";
55
+
56
+        this.person.removePhoneNumbers(expectedPhoneNumberToDelete1, expectedPhoneNumberToDelete2);
57
+
58
+        boolean isPhoneNumberInList1 = this.person.phoneNumberList.contains(expectedPhoneNumberToDelete1);
59
+        boolean isPhoneNumberInList2 = this.person.phoneNumberList.contains(expectedPhoneNumberToDelete2);
60
+
61
+        Assert.assertFalse(isPhoneNumberInList1);
62
+        Assert.assertFalse(isPhoneNumberInList2);
48 63
     }
49 64
 
50 65
 }
66
+

+ 27
- 13
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -23,36 +23,47 @@ public class PhoneBookTest {
23 23
         Assert.assertNotNull(this.phonebook);
24 24
     }
25 25
 
26
+
26 27
     @Test
27 28
     public void lookupTest() {
28 29
         Person expectedPerson = new Person("Jane", "(111) 222-3333");
29 30
         this.phonebook.addEntry("Jane", "(111) 222-3333");
30 31
 
31
-        String expectedPersonName = expectedPerson.getName();
32
-        String expectedPersonPhoneNumber = expectedPerson.getPhoneNumber();
32
+        String actualPersonName = expectedPerson.getName();
33
+        String actualPersonPhoneNumber = expectedPerson.phoneNumberList.get(0);
33 34
 
34
-        boolean actualPersonName = this.phonebook.hashMap.containsKey("Jane");
35
-        boolean actualPhoneNumber = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Jane"));
35
+        boolean actualPersonNameExists = this.phonebook.hashMap.containsKey("Jane");
36
+        //boolean actualPhoneNumberExists = this.phonebook.hashMap.get("Jane").phoneNumberList.get(0);
36 37
 
37
-        Assert.assertTrue(actualPersonName);
38
-        Assert.assertTrue(actualPhoneNumber);
38
+        Assert.assertTrue(actualPersonNameExists);
39
+        //Assert.assertTrue(actualPhoneNumberExists);
39 40
     }
40 41
 
42
+
41 43
     @Test
42 44
     public void addEntryTest() {
43
-        this.phonebook.addEntry("Adam", "(222) 333-4444");
45
+        this.phonebook.addEntry("Adam", "(000) 000-0000", "(111) 111-1111");
44 46
 
45 47
         boolean actualPersonName = this.phonebook.hashMap.containsKey("Adam");
46 48
         boolean actualPhoneNumber = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Adam"));
47
-
48 49
         Assert.assertTrue(actualPersonName);
49 50
         Assert.assertTrue(actualPhoneNumber);
51
+
52
+        String expectedPhoneNumber1 = "(000) 000-0000";
53
+        String expectedPhoneNumber2 = "(111) 111-1111";
54
+
55
+        String actualPhoneNumber1 = this.phonebook.hashMap.get("Adam").phoneNumberList.get(0);
56
+        String actualPhoneNumber2 = this.phonebook.hashMap.get("Adam").phoneNumberList.get(1);
57
+
58
+        Assert.assertEquals(expectedPhoneNumber1, actualPhoneNumber1);
59
+        Assert.assertEquals(expectedPhoneNumber2, actualPhoneNumber2);
60
+
50 61
     }
51 62
 
52 63
     @Test
53
-    public void removeEntryTest() {
64
+    public void removeRecordTest() {
54 65
         this.phonebook.addEntry("Bob", "(333) 444-5555");
55
-        this.phonebook.removeEntry("Bob");
66
+        this.phonebook.removeRecord("Bob");
56 67
 
57 68
         boolean actualPersonName = this.phonebook.hashMap.containsKey("Bob");
58 69
         boolean actualPersonObject = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Bob"));
@@ -61,6 +72,7 @@ public class PhoneBookTest {
61 72
         Assert.assertFalse(actualPersonObject);
62 73
     }
63 74
 
75
+    /*
64 76
     @Test
65 77
     public void getEntryListTest() {
66 78
         this.phonebook.addEntry("Ava", "(000) 111-2222");
@@ -81,7 +93,9 @@ public class PhoneBookTest {
81 93
         Assert.assertFalse(actualPersonName);
82 94
         Assert.assertFalse(actualPersonObject);
83 95
         */
96
+    /*
84 97
     }
98
+    */
85 99
 
86 100
     @Test
87 101
     public void reverseLookupTest() {
@@ -98,11 +112,11 @@ public class PhoneBookTest {
98 112
         String expectedName = "Ava";
99 113
         String expectedPhoneNumber = "(000) 111-2222";
100 114
 
101
-        String actualNameFromPerson = expectedPerson.getName();
102
-        String actualPhoneNumberFromPerson = expectedPerson.getPhoneNumber();
115
+        //String actualNameFromPerson = expectedPerson.getName();
116
+        //String actualPhoneNumberFromPerson = expectedPerson.getPhoneNumber();
103 117
 
104 118
         String actualNameFromPhoneBook = "Ava";
105
-        String actualPhoneNumberFromPhoneBook = this.phonebook.hashMap.get(actualNameFromPhoneBook).getPhoneNumber();
119
+        boolean actualPhoneNumberFromPhoneBook = this.phonebook.hashMap.get(actualNameFromPhoneBook).phoneNumberList.contains(expectedPhoneNumber);
106 120
 
107 121
         Assert.assertEquals(expectedName, this.phonebook.reverseLookup(givenPhoneNumber));
108 122
         // Then