Browse Source

Merge 6fa6558aad3d1a9af41a6edd30d46dc4bb3b8a49 into 74a9673e36c7d25c28315cb54247a91792f8fbc0

erc91087 6 years ago
parent
commit
08c6bfee5f
No account linked to committer's email

+ 19
- 0
pom.xml View File

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>phonebok</artifactId>
8
     <artifactId>phonebok</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.8</source>
17
+                    <target>1.8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>4.12</version>
27
+        </dependency>
28
+    </dependencies>
10
 
29
 
11
 
30
 
12
 </project>
31
 </project>

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

1
+package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+
6
+public class Person {
7
+
8
+    private String personName;
9
+    private ArrayList<String> phoneNumbers;
10
+
11
+    public Person(String personName){
12
+        this.personName = personName;
13
+        this.phoneNumbers = new ArrayList<>();
14
+        phoneNumbers
15
+    }
16
+
17
+
18
+    public String getPersonName() {
19
+        return personName;
20
+    }
21
+
22
+
23
+//public String addNumbers(){
24
+//        return null;
25
+//}
26
+
27
+
28
+   //find a way to print each item in the ArrayList in order
29
+   public String getPhoneNumbers() {
30
+       return phoneNumbers.toString();
31
+   }
32
+//       StringBuilder sb = new StringBuilder();
33
+//       for(int i = 0; i < phoneNumbers.size(); i++){
34
+//           if(i == phoneNumbers.size()){
35
+//               sb.append();
36
+//           }
37
+//       }
38
+//        return phoneNumbers;
39
+//    }
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+    }
50
+
51
+
52
+
53
+

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

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

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

1
+package com.zipcodewilmington.phonebook;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.ArrayList;
8
+import java.util.Arrays;
9
+
10
+import static org.junit.Assert.*;
11
+
12
+public class PersonTest {
13
+
14
+
15
+    @Test
16
+    public void getPersonNameTest() {
17
+        //Given
18
+        Person human = new Person("Eric", "302-999-1234");
19
+
20
+        //When
21
+        String expectedName = "Eric";
22
+        String expectedNumber = "302-999-1234";
23
+        String actual = human.getPersonName() + human.getPhoneNumbers();
24
+
25
+
26
+        Assert.assertTrue(actual.contains(expectedName) && actual.contains(expectedNumber));
27
+    }
28
+
29
+//@Test
30
+//    public void addNumbersTest() {
31
+//        //Given
32
+//    Person human = new Person("Eric", "302-999-1234");
33
+//
34
+//    //When
35
+//    human.addNumbers("302-222-9876");
36
+//    String expectedNumber = "302-222-1234";
37
+//    String actual = human.getPhoneNumbers();
38
+//
39
+//    Assert.assertTrue(actual.contains(expectedNumber));
40
+//}
41
+
42
+@Test
43
+    public void printNameTest(){
44
+        //Given
45
+    Person human = new Person("Eric", "302-999-1234");
46
+
47
+    //When
48
+    String expectedName = "Eric";
49
+    String actual = human.getPersonName();
50
+
51
+    Assert.assertTrue(actual.contains(expectedName));
52
+}
53
+
54
+//@Test
55
+//    public void printNumbersTest(){
56
+//        //Given
57
+//    Person human = new Person("Eric", "302-999-1234");
58
+//    human.addNumbers("302-222-9876");
59
+//
60
+//    //When
61
+//    String expectedNumbers = "";
62
+//
63
+//}
64
+
65
+
66
+}
67
+

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

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