Browse Source

Part 1 only, entryList test not working

Luis Romero 6 years ago
parent
commit
ba5f4c861a

+ 7
- 0
pom.xml View File

@@ -7,6 +7,13 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>phonebok</artifactId>
9 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 19
 </project>

+ 29
- 0
src/main/java/com/zipcodewilmington/phonebook/Person.java View File

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

+ 45
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,7 +1,52 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.Map;
4
+import java.util.HashMap;
5
+import java.util.ArrayList;
3 6
 /**
4 7
  * Created by leon on 1/23/18.
5 8
  */
6 9
 public class PhoneBook {
10
+
11
+    protected HashMap<String, Person> hashMap;
12
+    protected ArrayList<String> entryList;
13
+
14
+    public PhoneBook() {
15
+        this.hashMap = new HashMap<String, Person>();
16
+        this.hashMap.put("Name_empty", new Person("Name_empty", "Phone_number_empty"));
17
+    }
18
+
19
+    //PART I
20
+    public Person lookup(String name) {
21
+        return hashMap.get(name);
22
+    }
23
+
24
+    public void addEntry(String name, String phoneNumber) {
25
+        hashMap.put(name, new Person(name, phoneNumber));
26
+    }
27
+
28
+    public void removeEntry(String name) {
29
+        hashMap.remove(name);
30
+    }
31
+
32
+    public ArrayList<String> getEntryList() { //names
33
+        for (Map.Entry<String, Person> entry: hashMap.entrySet()) {
34
+            entryList.add(entry.getKey() + " : " + entry.getValue().getPhoneNumber());
35
+        }
36
+        return entryList;
37
+    }
38
+
39
+    public String[] entriesList() { //names and phone numbers
40
+        return new String[0];
41
+    }
42
+
43
+    //PART II
44
+    public String reverseLookup() { //lookup names from phone numbers
45
+        return "null";
46
+    }
47
+
48
+    //PART III
49
+    //after I'm done w/ Part I and Part II
50
+
51
+
7 52
 }

+ 50
- 0
src/test/java/com/zipcodewilmington/phonebook/PersonTest.java View File

@@ -0,0 +1,50 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+/**
8
+ * Created by Luis J. Romero on 2/12/2018
9
+ */
10
+
11
+public class PersonTest {
12
+
13
+    private static Person person;
14
+
15
+    @Before
16
+    public void setup() {
17
+        this.person = new Person("John Smith", "(111) 222-3333");
18
+    }
19
+
20
+    @Test
21
+    public void testSetName() {
22
+        String expected = "John Smith";
23
+        person.setName(expected);
24
+        String actual = person.getName();
25
+        Assert.assertEquals(expected, actual);
26
+    }
27
+
28
+    @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() {
38
+        String expected = "John Smith";
39
+        String actual = person.getName();
40
+        Assert.assertEquals(expected, actual);
41
+    }
42
+
43
+    @Test
44
+    public void testGetPhoneNumber() {
45
+        String expected = "(111) 222-3333";
46
+        String actual = person.getPhoneNumber();
47
+        Assert.assertEquals(expected, actual);
48
+    }
49
+
50
+}

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

@@ -1,7 +1,90 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.Arrays;
8
+
3 9
 /**
4 10
  * Created by leon on 1/23/18.
5 11
  */
6 12
 public class PhoneBookTest {
13
+
14
+    private static PhoneBook phonebook;
15
+
16
+    @Before
17
+    public void setup() {
18
+        this.phonebook = new PhoneBook() ;
19
+    }
20
+
21
+    @Test
22
+    public void testDefaultConstructor() {
23
+        Assert.assertNotNull(this.phonebook);
24
+    }
25
+
26
+    @Test
27
+    public void testLookup() {
28
+        Person expectedPerson = new Person("Jane", "(111) 222-3333");
29
+        this.phonebook.addEntry("Jane", "(111) 222-3333");
30
+
31
+        String expectedPersonName = expectedPerson.getName();
32
+        String expectedPersonPhoneNumber = expectedPerson.getPhoneNumber();
33
+
34
+        boolean actualPersonName = this.phonebook.hashMap.containsKey("Jane");
35
+        boolean actualPhoneNumber = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Jane"));
36
+
37
+        Assert.assertTrue(actualPersonName);
38
+        Assert.assertTrue(actualPhoneNumber);
39
+    }
40
+
41
+    @Test
42
+    public void testAddEntry() {
43
+        this.phonebook.addEntry("Adam", "(222) 333-4444");
44
+
45
+        boolean actualPersonName = this.phonebook.hashMap.containsKey("Adam");
46
+        boolean actualPhoneNumber = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Adam"));
47
+
48
+        Assert.assertTrue(actualPersonName);
49
+        Assert.assertTrue(actualPhoneNumber);
50
+    }
51
+
52
+    @Test
53
+    public void testRemoveEntry() {
54
+        this.phonebook.addEntry("Bob", "(333) 444-5555");
55
+        this.phonebook.removeEntry("Bob");
56
+
57
+        boolean actualPersonName = this.phonebook.hashMap.containsKey("Bob");
58
+        boolean actualPersonObject = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Bob"));
59
+
60
+        Assert.assertFalse(actualPersonName);
61
+        Assert.assertFalse(actualPersonObject);
62
+    }
63
+
64
+    @Test
65
+    public void testGetEntryList() {
66
+        this.phonebook.addEntry("Ava", "(000) 111-2222");
67
+        this.phonebook.addEntry("Brian", "(222) 333-4444");
68
+        this.phonebook.addEntry("Charlie", "(444) 555-6666");
69
+        this.phonebook.addEntry("David", "(666) 777-8888");
70
+        this.phonebook.addEntry("Edward", "(777) 888-9999");
71
+        this.phonebook.addEntry("Frank", "(888) 999-0000");
72
+
73
+        Assert.assertNotNull(this.phonebook.getEntryList());
74
+
75
+        /*
76
+        this.phonebook.removeEntry("Bob");
77
+
78
+        boolean actualPersonName = this.phonebook.hashMap.containsKey("Bob");
79
+        boolean actualPersonObject = this.phonebook.hashMap.containsValue(this.phonebook.hashMap.get("Bob"));
80
+
81
+        Assert.assertFalse(actualPersonName);
82
+        Assert.assertFalse(actualPersonObject);
83
+        */
84
+    }
85
+
7 86
 }
87
+
88
+
89
+
90
+