Sfoglia il codice sorgente

Finished display() entries method, but need alphabetical order

Luis Romero 6 anni fa
parent
commit
204b9d9b92

+ 15
- 1
src/main/java/com/zipcodewilmington/phonebook/Person.java Vedi File

5
 public class Person {
5
 public class Person {
6
 
6
 
7
     private String name;
7
     private String name;
8
-    public ArrayList<String> phoneNumberList;
8
+    protected ArrayList<String> phoneNumberList;
9
 
9
 
10
     public Person(String name, String... phoneNumbers) {
10
     public Person(String name, String... phoneNumbers) {
11
         this.name = name;
11
         this.name = name;
23
         return name;
23
         return name;
24
     }
24
     }
25
 
25
 
26
+    public ArrayList<String> getPhoneNumberListArray() {
27
+        return phoneNumberList;
28
+    }
29
+
30
+    public String getPhoneNumberListString() {
31
+
32
+        StringBuilder sb = new StringBuilder();
33
+
34
+        for (String phoneNumber : phoneNumberList) {
35
+            sb.append("\t" + phoneNumber + "\n");
36
+        }
37
+        return sb.toString();
38
+    }
39
+
26
     public void addPhoneNumbers(String... phoneNumbersToAdd) {
40
     public void addPhoneNumbers(String... phoneNumbersToAdd) {
27
         for (String phoneNumber : phoneNumbersToAdd) {
41
         for (String phoneNumber : phoneNumbersToAdd) {
28
             this.phoneNumberList.add(phoneNumber);
42
             this.phoneNumberList.add(phoneNumber);

+ 17
- 13
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Vedi File

3
 import java.util.Map;
3
 import java.util.Map;
4
 import java.util.HashMap;
4
 import java.util.HashMap;
5
 import java.util.ArrayList;
5
 import java.util.ArrayList;
6
+import java.util.Set;
7
+
6
 /**
8
 /**
7
  * Created by leon on 1/23/18.
9
  * Created by leon on 1/23/18.
8
  */
10
  */
10
 
12
 
11
     protected HashMap<String, Person> hashMap;
13
     protected HashMap<String, Person> hashMap;
12
     protected ArrayList<String> entryList;
14
     protected ArrayList<String> entryList;
15
+    protected Set<String> keySet;
13
 
16
 
14
     public PhoneBook() {
17
     public PhoneBook() {
15
         this.hashMap = new HashMap<String, Person>();
18
         this.hashMap = new HashMap<String, Person>();
16
         this.entryList = new ArrayList<String>();
19
         this.entryList = new ArrayList<String>();
20
+        this.keySet = hashMap.keySet();
17
         //this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
21
         //this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
18
     }
22
     }
19
 
23
 
31
         hashMap.remove(name);
35
         hashMap.remove(name);
32
     }
36
     }
33
 
37
 
34
-    /*
35
-    public ArrayList<String> getEntryList() { //names
36
-        for (Map.Entry<String, Person> entry: hashMap.entrySet()) {
37
-            entryList.add(entry.getKey() + " : " + entry.getValue().getPhoneNumber());
38
-        }
39
-        return entryList;
38
+    public Set<String> getKeySet() { //names and phone numbers
39
+        return hashMap.keySet();
40
+        //return all persons
40
     }
41
     }
41
-    */
42
 
42
 
43
-    public String[] entriesList() { //names and phone numbers
44
-        return new String[0];
43
+    public StringBuilder displayAllEntries() { //names and phone numbers
44
+        StringBuilder sb = new StringBuilder();
45
+
46
+        for (String key : hashMap.keySet()) {
47
+            sb.append(hashMap.get(key).getName() + "\n");
48
+            sb.append(hashMap.get(key).getPhoneNumberListString());
49
+        }
50
+
51
+        return sb;
52
+        //call getentries
45
     }
53
     }
46
 
54
 
47
     //PART II
55
     //PART II
55
         return "No_name";
63
         return "No_name";
56
     }
64
     }
57
 
65
 
58
-    //PART III
59
-    //after I'm done w/ Part I and Part II
60
-
61
-
62
 }
66
 }

+ 33
- 1
src/test/java/com/zipcodewilmington/phonebook/PersonTest.java Vedi File

1
 package com.zipcodewilmington.phonebook;
1
 package com.zipcodewilmington.phonebook;
2
 
2
 
3
+import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
3
 import org.junit.Assert;
4
 import org.junit.Assert;
4
 import org.junit.Before;
5
 import org.junit.Before;
5
 import org.junit.Test;
6
 import org.junit.Test;
6
 
7
 
8
+import javax.sound.midi.Soundbank;
9
+import java.util.Arrays;
10
+
7
 /**
11
 /**
8
  * Created by Luis J. Romero on 2/12/2018
12
  * Created by Luis J. Romero on 2/12/2018
9
  */
13
  */
17
         this.person = new Person("John Smith", "(111) 222-3333");
21
         this.person = new Person("John Smith", "(111) 222-3333");
18
     }
22
     }
19
 
23
 
20
-
21
     @Test
24
     @Test
22
     public void setNameTest() {
25
     public void setNameTest() {
23
         String expected = "John Smith";
26
         String expected = "John Smith";
34
     }
37
     }
35
 
38
 
36
     @Test
39
     @Test
40
+    public void getPhoneNumberListArrayTest() {
41
+        this.person = new Person("Adam", "(000) 000-0000", "(011) 111-1111");
42
+
43
+        String expectedPhoneNumber1 = "(000) 000-0000";
44
+        String expectedPhoneNumber2 = "(011) 111-1111";
45
+
46
+        String actualPhoneNumber1 = person.getPhoneNumberListArray().get(0);
47
+        String actualPhoneNumber2 = person.getPhoneNumberListArray().get(1);
48
+
49
+        Assert.assertEquals(expectedPhoneNumber1, actualPhoneNumber1);
50
+        Assert.assertEquals(expectedPhoneNumber2, actualPhoneNumber2);
51
+    }
52
+
53
+    @Test
54
+    public void getPhoneNumberListArrayStringTest() {
55
+        this.person = new Person("Adam", "(000) 000-0000", "(011) 111-1111");
56
+
57
+        String expectedPhoneNumbers = "\t" + "(000) 000-0000" + "\n" +
58
+                                      "\t" + "(011) 111-1111" + "\n";
59
+        String actualPhoneNumbers = person.getPhoneNumberListString();
60
+
61
+//        System.out.println(expectedPhoneNumbers);
62
+//        System.out.println(person.getPhoneNumberListString());
63
+
64
+        Assert.assertEquals(expectedPhoneNumbers, actualPhoneNumbers);
65
+    }
66
+
67
+
68
+    @Test
37
     public void addPhoneNumbersTest() {
69
     public void addPhoneNumbersTest() {
38
         this.person = new Person("John Smith", "(000) 000-0000");
70
         this.person = new Person("John Smith", "(000) 000-0000");
39
         String expectedPhoneNumberToAdd1 = "(111) 111-1111";
71
         String expectedPhoneNumberToAdd1 = "(111) 111-1111";

+ 26
- 24
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Vedi File

33
         String actualPersonPhoneNumber = expectedPerson.phoneNumberList.get(0);
33
         String actualPersonPhoneNumber = expectedPerson.phoneNumberList.get(0);
34
 
34
 
35
         boolean actualPersonNameExists = this.phonebook.hashMap.containsKey("Jane");
35
         boolean actualPersonNameExists = this.phonebook.hashMap.containsKey("Jane");
36
-        //boolean actualPhoneNumberExists = this.phonebook.hashMap.get("Jane").phoneNumberList.get(0);
37
 
36
 
38
         Assert.assertTrue(actualPersonNameExists);
37
         Assert.assertTrue(actualPersonNameExists);
39
-        //Assert.assertTrue(actualPhoneNumberExists);
40
     }
38
     }
41
 
39
 
42
 
40
 
72
         Assert.assertFalse(actualPersonObject);
70
         Assert.assertFalse(actualPersonObject);
73
     }
71
     }
74
 
72
 
75
-    /*
76
     @Test
73
     @Test
77
-    public void getEntryListTest() {
78
-        this.phonebook.addEntry("Ava", "(000) 111-2222");
79
-        this.phonebook.addEntry("Brian", "(222) 333-4444");
80
-        this.phonebook.addEntry("Charlie", "(444) 555-6666");
81
-        this.phonebook.addEntry("David", "(666) 777-8888");
82
-        this.phonebook.addEntry("Edward", "(777) 888-9999");
83
-        this.phonebook.addEntry("Frank", "(888) 999-0000");
84
-
85
-        Assert.assertNotNull(this.phonebook.getEntryList());
74
+    public void getKeySetTest() {
75
+        this.phonebook.addEntry("Frank", "(000) 000-0000", "(100) 000-0000");
76
+        this.phonebook.addEntry("Gabriel", "(100) 000-0000", "(110) 000-0000");
86
 
77
 
87
-        /*
88
-        this.phonebook.removeEntry("Bob");
78
+        System.out.println("getAllEntriesTest:");
79
+        String expectedKeySet = "[Gabriel, Frank]";
80
+        String actualKeySet = this.phonebook.getKeySet().toString();
89
 
81
 
90
-        boolean actualPersonName = this.phonebook.hashMap.containsKey("Bob");
91
-        boolean actualPersonObject = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Bob"));
82
+        Assert.assertEquals(expectedKeySet, actualKeySet);
83
+    }
92
 
84
 
93
-        Assert.assertFalse(actualPersonName);
94
-        Assert.assertFalse(actualPersonObject);
95
-        */
96
-    /*
85
+    @Test
86
+    public void displayAllEntriesTest() {
87
+        this.phonebook.addEntry("Herbert", "(200) 000-0000", "(210) 000-0000");
88
+        this.phonebook.addEntry("Isabel", "(300) 000-0000", "(310) 000-0000");
89
+
90
+        String expectedEntries = "Isabel" + "\n" +
91
+                                 "\t" + "(300) 000-0000" + "\n" +
92
+                                 "\t" + "(310) 000-0000" + "\n" +
93
+                                 "Herbert" + "\n" +
94
+                                 "\t" + "(200) 000-0000" + "\n" +
95
+                                 "\t" + "(210) 000-0000" + "\n";
96
+        String actualEntries = this.phonebook.displayAllEntries().toString();
97
+
98
+//        System.out.println("displayAllEntriesTest:");
99
+//        System.out.println(expectedEntries);
100
+//        System.out.println(actualEntries);
101
+
102
+        Assert.assertEquals(expectedEntries, actualEntries);
97
     }
103
     }
98
-    */
99
 
104
 
100
     @Test
105
     @Test
101
     public void reverseLookupTest() {
106
     public void reverseLookupTest() {
112
         String expectedName = "Ava";
117
         String expectedName = "Ava";
113
         String expectedPhoneNumber = "(000) 111-2222";
118
         String expectedPhoneNumber = "(000) 111-2222";
114
 
119
 
115
-        //String actualNameFromPerson = expectedPerson.getName();
116
-        //String actualPhoneNumberFromPerson = expectedPerson.getPhoneNumber();
117
-
118
         String actualNameFromPhoneBook = "Ava";
120
         String actualNameFromPhoneBook = "Ava";
119
         boolean actualPhoneNumberFromPhoneBook = this.phonebook.hashMap.get(actualNameFromPhoneBook).phoneNumberList.contains(expectedPhoneNumber);
121
         boolean actualPhoneNumberFromPhoneBook = this.phonebook.hashMap.get(actualNameFromPhoneBook).phoneNumberList.contains(expectedPhoneNumber);
120
 
122