Bladeren bron

Merge b14f2b38a2e028ba9e17f4d1c4e5bd9b47fe45fb into 74a9673e36c7d25c28315cb54247a91792f8fbc0

SupaGrammer 6 jaren geleden
bovenliggende
commit
f931c020f9
Geen account gekoppeld aan de committers e-mail

+ 21
- 1
pom.xml Bestand weergeven

@@ -5,8 +5,28 @@
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
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+
23
+    <dependencies>
24
+        <dependency>
25
+            <groupId>junit</groupId>
26
+            <artifactId>junit</artifactId>
27
+            <version>4.12</version>
28
+        </dependency>
29
+    </dependencies>
10 30
 
11 31
 
12 32
 </project>

+ 51
- 4
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Bestand weergeven

@@ -1,7 +1,54 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
3
+import java.util.ArrayList;
4
+import java.util.Map;
5
+import java.util.TreeMap;
6
+
7
+//Declare Treemap name
6 8
 public class PhoneBook {
7
-}
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<>();
18
+    }
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
+        }
33
+    }
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
+        }
44
+    }
45
+
46
+    public ArrayList<String> lookup(String name) {
47
+        //not sure of this yet
48
+        return contacts.get(name);
49
+    }
50
+
51
+    public void removeRecord(String name) {
52
+        contacts.remove(name);
53
+    }
54
+}

+ 82
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBook.java Bestand weergeven

@@ -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
- 7
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Bestand weergeven

@@ -1,7 +0,0 @@
1
-package com.zipcodewilmington.phonebook;
2
-
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
6
-public class PhoneBookTest {
7
-}