Browse Source

Phonebook composed

NedRedmond 6 years ago
parent
commit
51d5fbd71a

+ 49
- 1
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,7 +1,55 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.HashMap;
4
+import java.util.Map;
5
+
6
+import static javax.swing.UIManager.put;
7
+
3 8
 /**
4 9
  * Created by leon on 1/23/18.
10
+ * https://git.zipcode.rocks/Cohort4.2/CR-MicroLabs-Composition-PhoneBook
5 11
  */
6 12
 public class PhoneBook {
7
-}
13
+
14
+    private HashMap<String, String> listings  = new HashMap<>();
15
+
16
+    PhoneBook() {}
17
+
18
+    public int getEntryCount() {
19
+        return listings.size();
20
+    }
21
+
22
+    public boolean hasEntry(String name) {
23
+        if (listings.get(name) == null) { return false; }
24
+        return true;
25
+    }
26
+
27
+    public void  add(String name, String phoneNumber) {
28
+//        adds an entry to the composite associate data type
29
+        listings.put(name, phoneNumber);
30
+    }
31
+
32
+    public String lookup(String name) {
33
+//        returns a phone number for the respective input name
34
+        return listings.get(name);
35
+    }
36
+
37
+    public void remove(String name) {
38
+//        removes an entry to the composite associate data type
39
+        listings.remove(name);
40
+    }
41
+
42
+    public String reverseLookup(String phonenumber) {
43
+//        returns a name for the respective input phoneNumber
44
+        return listings.entrySet().stream()
45
+                .filter(entry -> phonenumber.equals(entry.getValue()))
46
+                .map(Map.Entry::getKey)
47
+                .findFirst().get();
48
+    }
49
+
50
+    public void listNamesAndNumbers() {
51
+//        return a human-readable list of all entries (names and phone numbers) in alphabetical order.
52
+//        Format should be [name] [phone number]
53
+        listings.forEach((k,v) -> System.out.println(k + " " + v));
54
+    }
55
+}

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

@@ -1,7 +1,79 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
3 7
 /**
4 8
  * Created by leon on 1/23/18.
9
+ * https://git.zipcode.rocks/Cohort4.2/CR-MicroLabs-Composition-PhoneBook
5 10
  */
6 11
 public class PhoneBookTest {
12
+    String testName;
13
+    String testNumber;
14
+
15
+    @Before
16
+    public void setup() {
17
+        testName = "Test Testmond";
18
+        testNumber = "302-299-6553";
19
+    }
20
+
21
+    @Test
22
+    public void  addTest() {
23
+//        adds an entry to the composite associate data type
24
+        PhoneBook app = new PhoneBook();
25
+
26
+        double before = app.getEntryCount();
27
+
28
+        app.add(testName, testNumber);
29
+
30
+        double after = app.getEntryCount();
31
+
32
+        Assert.assertTrue((before + 1) == after);
33
+    }
34
+
35
+    @Test
36
+    public void hasEntryTest() {
37
+        PhoneBook app = new PhoneBook();
38
+
39
+        app.add(testName, testNumber);
40
+        Assert.assertTrue(app.hasEntry(testName));
41
+    }
42
+
43
+    @Test
44
+    public void lookupTest() {
45
+        PhoneBook app = new PhoneBook();
46
+
47
+        app.add(testName, testNumber);
48
+        Assert.assertTrue(app.lookup(testName).equals(testNumber));
49
+    }
50
+
51
+    @Test
52
+    public void removeTest() {
53
+        PhoneBook app = new PhoneBook();
54
+//        removes an entry to the composite associate data type
55
+        app.add(testName, testNumber);
56
+        app.remove(testName);
57
+        Assert.assertFalse(app.hasEntry(testName));
58
+    }
59
+
60
+    @Test
61
+    public void reverseLookupTest() {
62
+        PhoneBook app = new PhoneBook();
63
+//        returns a name for the respective input phoneNumber
64
+        app.add(testName, testNumber);
65
+        String entry = app.reverseLookup(testNumber);
66
+        Assert.assertTrue(entry.equals((testName)));
67
+    }
68
+
69
+    @Test
70
+    public void listNamesAndNumbersTest() {
71
+        PhoneBook app = new PhoneBook();
72
+
73
+//        return a human-readable list of all entries (names and phone numbers) in alphabetical order.
74
+//        Format should be [name] [phone number]
75
+
76
+//        System.out.println()
77
+    }
78
+
7 79
 }