Browse Source

Merge 9422e30a9bce8cb7e527d783aa2da44d862de313 into 74a9673e36c7d25c28315cb54247a91792f8fbc0

kbrinker1 6 years ago
parent
commit
9bfeca7594
No account linked to committer's email

+ 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>RELEASE</version>
15
+        </dependency>
16
+    </dependencies>
10 17
 
11 18
 
12 19
 </project>

+ 18
- 0
src/main/java/com/zipcodewilmington/phonebook/Main.java View File

@@ -0,0 +1,18 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+public class Main {
4
+
5
+    public static void main(String[] args) {
6
+
7
+        PhoneBook tester = new PhoneBook();
8
+        tester.addName("Keith", "555-666-7777");
9
+        tester.addName("John", "555-612-7777");
10
+        tester.addName("Mike", "555-623-7777");
11
+        tester.addName("Steve", "555-645-7777");
12
+       String s = tester.listNames();
13
+        System.out.println(s);
14
+
15
+        s = tester.listNamesAndNumbers();
16
+        System.out.println(s);
17
+    }
18
+}

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

@@ -1,7 +1,79 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.TreeMap;
4
+import java.util.Set;
5
+import java.util.ArrayList;
6
+
3 7
 /**
4 8
  * Created by leon on 1/23/18.
5 9
  */
6 10
 public class PhoneBook {
11
+
12
+    String name;
13
+    String phonenum;
14
+    //TreeMap<String, String> contacts = new TreeMap<String, String>();
15
+    //   TreeMap<String, ArrayList> contacts = new TreeMap<String, ArrayList<String>>();
16
+    TreeMap<String, PhoneNumberStorage> contacts = new TreeMap<String, PhoneNumberStorage>();
17
+
18
+
19
+    public PhoneBook() {
20
+    }
21
+
22
+    public void addName(String name, String phonenum) {
23
+        contacts.put(name, new PhoneNumberStorage(name, phonenum));
24
+    }
25
+
26
+    public void addName(String name, PhoneNumberStorage phoneNumberStorage) {
27
+        contacts.put(name, phoneNumberStorage);
28
+    }
29
+
30
+    public void deleter(String name) {
31
+        contacts.remove(name);
32
+    }
33
+
34
+    public PhoneNumberStorage lookup(String name) {
35
+        return contacts.get(name);
36
+    }
37
+
38
+    public String listNames() {
39
+        StringBuilder namesnumbers = new StringBuilder();
40
+        Set<String> keys = contacts.keySet();
41
+        for (String key : keys)
42
+
43
+        {
44
+
45
+            namesnumbers.append(key + "\n");
46
+        }
47
+        return namesnumbers.toString();
48
+    }
49
+
50
+    public String listNamesAndNumbers() {
51
+        StringBuilder namesnumbers = new StringBuilder();
52
+        Set<String> keys = contacts.keySet();
53
+        for (String key : keys) {
54
+            namesnumbers.append(key + " " + contacts.get(key) + "\n");
55
+        }
56
+        return namesnumbers.toString();
57
+    }
58
+
59
+    public String reverselookup(String number) {
60
+        //Set<String> names = contacts.keySet();
61
+        for (PhoneNumberStorage nums : contacts.values()) {
62
+            for (String phoneNumbers : nums.getPhoneNumbers())
63
+
64
+                if (number.equals(phoneNumbers)) {
65
+                    return nums.lookup();
66
+                }
67
+        }
68
+        return null;
69
+    }
70
+
71
+    public void addmultiple(String name, String... numbers){
72
+        this.name = name;
73
+        for (int i = 0; i < numbers.length; i++){
74
+            contacts.get(name).add(numbers[i]);
75
+        }
76
+    }
77
+
78
+
7 79
 }

+ 39
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneNumberStorage.java View File

@@ -0,0 +1,39 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class PhoneNumberStorage {
6
+
7
+    public String name;
8
+    public ArrayList<String> phoneNumbers;
9
+
10
+
11
+    public PhoneNumberStorage(String name, ArrayList<String> phoneNumbers) {
12
+        this.name = name;
13
+        this.phoneNumbers = phoneNumbers;
14
+    }
15
+
16
+    public PhoneNumberStorage(String name, String phoneNumbers){
17
+        ArrayList<String> temp = new ArrayList<String>();
18
+        temp.add(phoneNumbers);
19
+        new PhoneNumberStorage(name, temp);
20
+    }
21
+
22
+
23
+public void add(String phonenumber){
24
+        phoneNumbers.add(phonenumber);
25
+
26
+}
27
+
28
+    public String lookup(){
29
+        return this.name;
30
+    }
31
+
32
+    public ArrayList<String> getPhoneNumbers() {
33
+        return phoneNumbers;
34
+    }
35
+
36
+    public PhoneNumberStorage(String name, String... number) {
37
+        this.name = name;
38
+        this.phoneNumbers = phoneNumbers;}
39
+}

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

@@ -1,7 +1,106 @@
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.TreeMap;
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
11
+
6 12
 public class PhoneBookTest {
13
+
14
+    PhoneBook testbook;
15
+    @Before
16
+    public void setUp(){
17
+        testbook = new PhoneBook();
18
+    }
19
+
20
+    @Test
21
+    public void testPhoneBookAddName() {
22
+        // Given
23
+        PhoneBook testbook = new PhoneBook();
24
+        PhoneNumberStorage temp = new PhoneNumberStorage("Keith", "555-666-7777");
25
+       testbook.addName("Keith", temp);
26
+
27
+      PhoneNumberStorage testNumber = testbook.lookup("Keith");
28
+      Assert.assertTrue(testNumber.equals(temp));
29
+
30
+    }
31
+@Test
32
+    public void testPhoneBookDelete(){
33
+        PhoneBook testbook = new PhoneBook();
34
+        testbook.addName("Keith", "555-666-7777");
35
+        testbook.deleter("Keith");
36
+        PhoneNumberStorage temp = testbook.lookup("Keith");
37
+        Assert.assertNull(temp );
38
+    }
39
+@Test
40
+    public void testPhoneBookLookUp(){
41
+        PhoneBook testbook = new PhoneBook();
42
+        PhoneNumberStorage temp = new PhoneNumberStorage("Keith", "555-666-7777");
43
+        testbook.addName("Keith", temp);
44
+        PhoneNumberStorage actual = testbook.lookup("Keith");
45
+        Assert.assertEquals(actual, temp);
46
+    }
47
+
48
+    @Test
49
+    public void testPhoneBookList(){
50
+
51
+        PhoneBook testbook = new PhoneBook();
52
+        testbook.addName("Keith", "555-666-7777");
53
+        testbook.addName("John", "555-612-7777");
54
+        testbook.addName("Mike", "555-623-7777");
55
+        testbook.addName("Steve", "555-645-7777");
56
+
57
+        String expected = "John 555-612-7777\n" +
58
+                "Keith 555-666-7777\n" +
59
+                "Mike 555-623-7777\n" +
60
+                "Steve 555-645-7777\n";
61
+        String actual = testbook.listNamesAndNumbers();
62
+        Assert.assertEquals(expected, actual);
63
+    }
64
+
65
+    @Test
66
+    public void testPhoneBookNames(){
67
+
68
+        PhoneBook testbook = new PhoneBook();
69
+        testbook.addName("Keith", "555-666-7777");
70
+        testbook.addName("John", "555-612-7777");
71
+        testbook.addName("Mike", "555-623-7777");
72
+        testbook.addName("Steve", "555-645-7777");
73
+
74
+        String expected = "John\n" +
75
+                "Keith\n" +
76
+                "Mike\n" +
77
+                "Steve\n";
78
+        String actual = testbook.listNames();
79
+        Assert.assertEquals(expected, actual);
80
+    }
81
+    @Test
82
+    public void testPhoneBookReverseLookUp() {
83
+        PhoneBook testbook = new PhoneBook();
84
+
85
+        testbook.addName("Keith", "555-666-7777");
86
+        testbook.addName("John", "555-612-7777");
87
+        testbook.addName("Mike", "555-623-7777");
88
+        testbook.addName("Steve", "555-645-7777");
89
+        String testnumber = testbook.reverselookup("555-666-7777");
90
+        Assert.assertEquals("Keith", testnumber);
91
+    }
92
+
93
+   @Test
94
+   public void testPhoneBookAddMultiple() {
95
+       // Given
96
+       PhoneBook testbook = new PhoneBook();
97
+       PhoneNumberStorage temp = new PhoneNumberStorage("Keith", "555-666-7777", "555-000-8888");
98
+       testbook.addName("Keith", temp);
99
+
100
+       PhoneNumberStorage testNumber = testbook.lookup("Keith");
101
+       Assert.assertTrue(testNumber.equals(temp));
102
+
103
+   }
104
+
105
+
7 106
 }