Browse Source

Merge 1202a44bf7a165f6a63cde6884e56795a33f98dc into 74a9673e36c7d25c28315cb54247a91792f8fbc0

danzcw 6 years ago
parent
commit
5a2e347653
No account linked to committer's email

+ 11
- 0
pom.xml View File

@@ -8,5 +8,16 @@
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+    <!-- https://mvnrepository.com/artifact/junit/junit -->
13
+    <dependency>
14
+        <groupId>junit</groupId>
15
+        <artifactId>junit</artifactId>
16
+        <version>4.12</version>
17
+        <scope>test</scope>
18
+    </dependency>
19
+
20
+    </dependencies>
21
+
11 22
 
12 23
 </project>

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

@@ -1,7 +1,125 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
4
+
5
+import java.util.*;
6
+
3 7
 /**
4 8
  * Created by leon on 1/23/18.
5 9
  */
6 10
 public class PhoneBook {
11
+
12
+    private String name;
13
+    private ArrayList<String> number;
14
+
15
+    TreeMap<String, ArrayList<String>> myTree = new TreeMap<String, ArrayList<String>>();
16
+
17
+
18
+    public PhoneBook(){
19
+    }
20
+
21
+
22
+    public void addEntry(String name, String... number) {
23
+        ArrayList<String> allNumbers = new ArrayList<String>(Arrays.asList(number));
24
+        myTree.put(name, allNumbers);
25
+
26
+    }
27
+
28
+    public void additionalPhoneNumberToKey(String name, String number) {
29
+
30
+        myTree.get(name).add(number);
31
+    }
32
+
33
+
34
+    public void removeNumberFromName(String name, String number) {
35
+        List<String> phoneNumbers = myTree.get(name);
36
+        phoneNumbers.remove(number);
37
+    }
38
+
39
+
40
+    public void removeEntry(String name) {
41
+        myTree.remove(name);
42
+
43
+    }
44
+
45
+    public String lookupNumber(String name) {
46
+        ArrayList<String> matchingNumbers = myTree.get(name);
47
+        String listedNumbers = "";
48
+        for (String phoneNumbers : matchingNumbers) {
49
+            listedNumbers +=phoneNumbers + ", ";
50
+        }
51
+       return listedNumbers.trim();
52
+
53
+
54
+    }
55
+
56
+    public String listNames() {
57
+
58
+        Set<String> contacts = myTree.keySet();
59
+
60
+        String allNames = "";
61
+        for (String contact : contacts) {
62
+            allNames += contact + "\n";
63
+
64
+        }
65
+        return allNames.trim();
66
+    }
67
+
68
+
69
+    public String listPhoneBook() {
70
+
71
+        Set<String> contacts = myTree.keySet();
72
+        String phoneBook = "";
73
+
74
+        for (String name : contacts) {
75
+            phoneBook += name + ": ";
76
+          for (int x = 0; x<myTree.get(name).size(); x++) {
77
+                phoneBook += myTree.get(name).get(x) + ", ";
78
+            }
79
+
80
+        }
81
+
82
+
83
+        return phoneBook.trim();
84
+    }
85
+
86
+    public String reverseLookup(String number) {
87
+
88
+        Set<String> contacts = myTree.keySet();
89
+
90
+        for (String name : contacts) {
91
+            for (int y = 0; y < myTree.get(name).size(); y++) {
92
+                if (myTree.get(name).get(y).equals(number)) {
93
+                    return name;
94
+                }
95
+            }
96
+        }
97
+
98
+
99
+        return "not in my phone book";
100
+
101
+    }
102
+
103
+
104
+
105
+
106
+    public static void main(String[] args) {
107
+
108
+        PhoneBook phonebookEntry = new PhoneBook();
109
+
110
+        phonebookEntry.addEntry("Brian", "3027619121");
111
+        phonebookEntry.addEntry("Dan", "4846394190", "9897878787");
112
+        phonebookEntry.addEntry("Garret", "3027894586");
113
+        phonebookEntry.addEntry("Luke", "3147619121");
114
+        phonebookEntry.addEntry("Kate", "4899994190");
115
+        phonebookEntry.addEntry("Lenore", "3097894586");
116
+        phonebookEntry.removeEntry("Dan");
117
+
118
+
119
+
120
+        phonebookEntry.listPhoneBook();
121
+        System.out.println(phonebookEntry.listPhoneBook());
122
+
123
+    }
124
+
7 125
 }

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

@@ -0,0 +1,19 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+//import org.junit.Assert;
4
+//import org.junit.Before;
5
+//import org.junit.Test;
6
+//import org.junit.experimental.theories.suppliers.TestedOn;
7
+//
8
+//public class PersonTest {
9
+//    @Test
10
+//    public void testGetName() {
11
+//
12
+//        Person p = new Person("Kate");
13
+//        String name = p.getName();
14
+//
15
+//        Assert.assertTrue(name == "Kate");
16
+//
17
+//    }
18
+//
19
+//}

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

@@ -1,7 +1,112 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+import org.junit.experimental.theories.suppliers.TestedOn;
7
+
8
+import java.util.ArrayList;
9
+
10
+
3 11
 /**
4 12
  * Created by leon on 1/23/18.
5 13
  */
6 14
 public class PhoneBookTest {
15
+
16
+
17
+
18
+    @Test
19
+    public void addTest() {
20
+        PhoneBook testBook = new PhoneBook();
21
+        testBook.addEntry("Joe", "1234567", "2345678");
22
+        String testNumber = testBook.lookupNumber("Joe");
23
+        Assert.assertTrue(testNumber.equals("1234567, 2345678,"));
24
+
25
+    }
26
+
27
+//    @Test
28
+//    public void addTest1() {
29
+//        PhoneBook testBook = new PhoneBook();
30
+//        Person expected = new Person("name", "arraylist of phone numbers");
31
+//        testBook.addEntry("Kate", expected);
32
+////        Person actual = testBook.lookupNumber("Kate");
33
+////        Assert.assertTrue(expected.equals(actual));
34
+//
35
+//    }
36
+//
37
+    @Test
38
+    public void removeTest1() {
39
+        PhoneBook testBook = new PhoneBook();
40
+        testBook.addEntry("Kate", "4846569090");
41
+        testBook.addEntry("Vince", "3333333333");
42
+        testBook.removeEntry("Kate");
43
+        String testNumber = testBook.listNames();
44
+        Assert.assertEquals(testNumber, "Vince");
45
+
46
+
47
+    }
48
+
49
+    @Test
50
+    public void stringLookupTest() {
51
+        PhoneBook testBook = new PhoneBook();
52
+        testBook.addEntry("Kate", "4846569090", "9899890099");
53
+        String testNumber = testBook.lookupNumber("Kate");
54
+        Assert.assertTrue(testNumber.equals("4846569090, 9899890099,"));
55
+
56
+    }
57
+
58
+    @Test
59
+    public void listNamesTest1() {
60
+        PhoneBook testBook = new PhoneBook();
61
+        testBook.addEntry("Kate", "4846569090");
62
+        testBook.addEntry("Adam", "4864569090");
63
+        testBook.addEntry("Zeke", "4787869090");
64
+        String actual = testBook.listNames();
65
+        String expected = "Adam\nKate\nZeke";
66
+        Assert.assertEquals(actual, expected);
67
+
68
+
69
+    }
70
+
71
+    @Test
72
+    public void listPhonebookTest1() {
73
+        PhoneBook testBook = new PhoneBook();
74
+        testBook.addEntry("Kate", "4846569090");
75
+        testBook.addEntry("Adam", "4864569090", "7879891212");
76
+        testBook.addEntry("Zeke", "4787869090");
77
+        String actual = testBook.listPhoneBook();
78
+        String expected = "Adam: 4864569090, 7879891212, Kate: 4846569090, Zeke: 4787869090,";
79
+        Assert.assertEquals(expected, actual);
80
+
81
+
82
+    }
83
+    @Test
84
+    public void ReverseLookupTest() {
85
+        PhoneBook testBook = new PhoneBook();
86
+        testBook.addEntry("Kate", "4846569090");
87
+        String testNumber = testBook.reverseLookup("4846569090");
88
+        Assert.assertTrue(testNumber.equals("Kate"));
89
+
90
+    }
91
+
92
+    @Test
93
+    public void RemoveNumberFromNameTest(){
94
+        PhoneBook testBook = new PhoneBook();
95
+        testBook.addEntry("Adam", "4864569090", "7879891212");
96
+        testBook.removeNumberFromName("Adam", "4864569090");
97
+        String expected = "7879891212,";
98
+        String actual = testBook.lookupNumber("Adam");
99
+        System.out.println(actual);
100
+        Assert.assertTrue(expected.equals(actual));
101
+    }
102
+
103
+    @Test
104
+    public void additionalPhoneNumberToKeyTest() {
105
+        PhoneBook testBook = new PhoneBook();
106
+        testBook.addEntry("Kate", "5659871212");
107
+        testBook.additionalPhoneNumberToKey("Kate", "4999999999");
108
+        String testNumber = testBook.lookupNumber("Kate");
109
+        Assert.assertTrue(testNumber.equals("5659871212, 4999999999,"));
110
+
111
+    }
7 112
 }