Browse Source

Missing lookup

Kthomas 6 years ago
parent
commit
b14f2b38a2

+ 5
- 4
pom.xml View File

@@ -5,7 +5,7 @@
5 5
     <modelVersion>4.0.0</modelVersion>
6 6
 
7 7
     <groupId>com.zipcodewilmington</groupId>
8
-    <artifactId>phonebok</artifactId>
8
+    <artifactId>phonebook</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
     <build>
11 11
         <plugins>
@@ -19,11 +19,12 @@
19 19
             </plugin>
20 20
         </plugins>
21 21
     </build>
22
+
22 23
     <dependencies>
23 24
         <dependency>
24
-            <groupId>org.junit.jupiter</groupId>
25
-            <artifactId>junit-jupiter-api</artifactId>
26
-            <version>RELEASE</version>
25
+            <groupId>junit</groupId>
26
+            <artifactId>junit</artifactId>
27
+            <version>4.12</version>
27 28
         </dependency>
28 29
     </dependencies>
29 30
 

+ 47
- 29
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,36 +1,54 @@
1 1
 package com.zipcodewilmington.phonebook;
2
-import org.junit.Assert;
3
-import org.junit.Test;
4
-import org.junit.jupiter.api.Test;
5
-import sun.jvm.hotspot.utilities.Assert;
6
-
7
-//added all useful utilities
8
-Private class Person {
9
-    private String name;
10
-    private String phNumber;
11
-//initialize strings
12
-    public Person (String name, String phNumber) {
13
-        this.name = name;
14
-        this.phNumber = phNumber;
15
-    }
16
-//get entry (name and number)
17
-    public void removeEntry(String name) {
18 2
 
3
+import java.util.ArrayList;
4
+import java.util.Map;
5
+import java.util.TreeMap;
6
+
7
+//Declare Treemap name
8
+public class PhoneBook {
9
+    //Declare Treemap name
10
+    //method 1
11
+    private TreeMap<String, ArrayList<String>> contacts;
12
+    private static final String add = "No such entry";
13
+    public String name;
14
+    public String phoneNumber;
15
+
16
+    public PhoneBook() {
17
+        contacts = new TreeMap<>();
19 18
     }
20
-//remove entry from list
21
-    public String[] getEntry() {
22
-        return getEntry();
19
+
20
+    //Instantiate new array list and add the number
21
+    public void add(String name, String phoneNumber) {
22
+        this.name = name;
23
+        this.phoneNumber = phoneNumber;
24
+        if (contacts.containsKey(name)) {
25
+            contacts.get(name).add(phoneNumber);
26
+        }
27
+
28
+        else {
29
+            ArrayList<String> phoneNumbers = new ArrayList<>();
30
+            phoneNumbers.add(phoneNumber);
31
+            contacts.put(name, phoneNumbers);
32
+        }
23 33
     }
24
-//Look up person
25
-    public void listAll(){
26
-        String[] listAll = new String[];
27
-        return;
34
+
35
+    public void remove(String phoneNumber) {
36
+        for (Map.Entry<String, ArrayList<String>> entry : contacts.entrySet()) {
37
+            ArrayList<String> phoneNumArrList = entry.getValue();
38
+            for (int i = 0; i < phoneNumArrList.size(); i++) {
39
+                if (phoneNumber.equals(phoneNumArrList.get(i))) {
40
+                    phoneNumArrList.remove(i);
41
+                }
42
+            }
43
+        }
28 44
     }
29
-//List all names in registry
30
-    public String getPhNumber() {
31
-        return getPhNumber+= listAll();
45
+
46
+    public ArrayList<String> lookup(String name) {
47
+        //not sure of this yet
48
+        return contacts.get(name);
32 49
     }
33
-//List all entries
34
-}
35 50
 
36
-//this is just a skeleton, gonna work on syntax
51
+    public void removeRecord(String name) {
52
+        contacts.remove(name);
53
+    }
54
+}

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

@@ -1,37 +0,0 @@
1
-package com.zipcodewilmington.phonebook;
2
-
3
-
4
-import org.junit.jupiter.api.Test;
5
-import sun.jvm.hotspot.utilities.Assert;
6
-
7
-
8
-class PersonTest {
9
-//need to generate test to add an entry to phbook
10
-        PhBook = new PhBook(addEntry);
11
-        name = new Person("name", "number");}
12
-
13
-    }
14
-
15
-    @Test
16
-    void removeEntry1() {
17
-            Person.add("name", "number");
18
-            Person.remove("name");
19
-            Assert.assertFalse(Person.entryCheck("name"));
20
-}
21
-
22
-    @Test
23
-    void getEntry() {
24
-            Person.add("name", "number");
25
-            Assert.assertEquals("name".getPhNumber(), Person.get("name"));
26
-}
27
-
28
-    @Test
29
-    void listAll() {
30
-//need to set parameters for test
31
-}
32
-
33
-    @Test
34
-    void getPhNumber1() {
35
-//not sure of the syntax how to set keys and values in array list and return
36
-}
37
-}

+ 82
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -0,0 +1,82 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.Map;
4
+import java.util.TreeMap;
5
+import org.junit.Test;
6
+import org.junit.Assert;
7
+import java.util.ArrayList;
8
+
9
+
10
+/**
11
+ * Created by leon on 1/23/18.
12
+ */
13
+public class PhoneBook {
14
+
15
+    @Test
16
+    public static void add() {
17
+        //Given
18
+        PhoneBook phoneBook = new PhoneBook();
19
+
20
+        //When
21
+        String expected = "1234567890";
22
+        PhoneBook.add();
23
+        //Assert
24
+        String actual = PhoneBook.lookup();
25
+        Assert.assertEquals(actual, expected);
26
+    }
27
+
28
+    @Test
29
+    public void add2() {
30
+        //Given
31
+        PhoneBook phoneBook = new PhoneBook();
32
+
33
+        //When
34
+        String expected = "1234567890";
35
+        phoneBook.add("bovice", "1234567890");
36
+        phoneBook.add("bovice", "1234567890");
37
+
38
+        //Assert
39
+        String actual = phoneBook.lookup("bovice").get(1);
40
+        Assert.assertEquals(actual, expected);
41
+    }
42
+
43
+    @Test
44
+    public void remove() {
45
+        //Given
46
+        PhoneBook phoneBook = new PhoneBook();
47
+        //When
48
+        phoneBook.add("bovice", "7777777777");
49
+        phoneBook.add("bovice", "1234567890");
50
+        phoneBook.remove("1234567890");
51
+
52
+        //Assert
53
+        ArrayList<String> actual = phoneBook.lookup("bovice");
54
+        Assert.assertEquals(1, actual.size());
55
+    }
56
+
57
+    @Test
58
+    public static String lookup() {
59
+//Still needs a look up function
60
+        return null;
61
+    }
62
+
63
+    @Test
64
+    public void remove2() {
65
+        //Given
66
+        PhoneBook phoneBook = new PhoneBook();
67
+        phoneBook.add("bovice", "7777777777");
68
+        phoneBook.add("bovice", "1234567890");
69
+        phoneBook.add("vance", "8888888888");
70
+        phoneBook.add("vance", "0987654321");
71
+        phoneBook.add("dr shivago", "3027778899");
72
+
73
+        //When
74
+        phoneBook.remove("7777777777");
75
+
76
+        //Assert
77
+        ArrayList<String> actual1 = phoneBook.lookup("bovice");
78
+        ArrayList<String> actual2 = phoneBook.lookup("vance");
79
+        Assert.assertEquals(1, actual1.size());
80
+        Assert.assertEquals(1, actual2.size());
81
+    }
82
+}

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