#38 methods written tests passed

Ouvert
jacob-andersen veut fusionner 1 révision(s) depuis jacob-andersen/CR-MicroLabs-Composition-PhoneBook:master vers master

+ 107
- 1
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Voir le fichier

@@ -1,7 +1,113 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Map;
5
+import java.util.TreeMap;
6
+
3 7
 /**
4 8
  * Created by leon on 1/23/18.
5 9
  */
6 10
 public class PhoneBook {
7
-}
11
+    public static void main(String[] args) {
12
+
13
+
14
+
15
+
16
+
17
+        PhoneBook phonebook = new PhoneBook();
18
+
19
+        phonebook.add("johnny", "302-555-6789");
20
+        phonebook.add("lewis black", "244-324-5590");
21
+        phonebook.add("johnny", "215-344-5645");
22
+         phonebook.add("sally fields", "214-335-4545");
23
+        phonebook.add("sally fields", "213-336-4588");
24
+        phonebook.add("jimbo", "210-2478-1111");
25
+        phonebook.add("eric idle", "215-2245-3323");
26
+        System.out.println(phonebook.listNamesAndNumbers());
27
+
28
+
29
+    }
30
+
31
+    private Map<String, ArrayList<String>> contacts;
32
+
33
+    public PhoneBook() {
34
+        contacts = new TreeMap<String, ArrayList<String>>();
35
+
36
+    }
37
+
38
+    public boolean hasEntry(String name) {
39
+
40
+        return contacts.containsKey(name);
41
+    }
42
+
43
+    public void add(String name, String phoneNumber) {
44
+        ArrayList<String> details = new ArrayList<>();
45
+        if (hasEntry(name)) {
46
+            details = contacts.get(name);
47
+        }
48
+        details.add(phoneNumber);
49
+        contacts.put(name, details);
50
+
51
+    }
52
+
53
+    public String lookup(String name) {
54
+        String s = "";
55
+        ArrayList<String> pnumber = new ArrayList<>();
56
+        pnumber = contacts.get(name);
57
+        //    for (int i = 0; i < pnumber.size(); i++) {
58
+        //   s += pnumber.get(i);
59
+        s += pnumber.get(0);
60
+        return s;
61
+    }
62
+
63
+
64
+    public String reverseLookup(String phoneNumber) {
65
+        // if (contacts.containsValue(phoneNumber)) {
66
+        String status = "person not found!";
67
+        for (String name : contacts.keySet()) {
68
+            ArrayList<String> reverse = contacts.get(name);
69
+            for (String searching : reverse) {
70
+                if (searching.equals(phoneNumber)) {
71
+                    status = name;
72
+                }
73
+            }
74
+        }
75
+        return status;
76
+    }
77
+
78
+    public void remove(String name) {
79
+        contacts.remove(name);
80
+    }
81
+
82
+    public void removeRecord(String name)
83
+    {
84
+        contacts.remove(name);
85
+    }
86
+
87
+    public void removePhoneNumber(String number) {
88
+        String srcName = reverseLookup(number);
89
+        ArrayList<String> newPhonelist = new ArrayList<>();
90
+        ArrayList<String> pnumber = new ArrayList<>();
91
+        pnumber = contacts.get(srcName);
92
+        for (int i = 0; i < pnumber.size(); i++) {
93
+            if (!(pnumber.get(i).equals(number))) {
94
+                newPhonelist.add(i, pnumber.get(i));
95
+            }
96
+            contacts.put(srcName, newPhonelist);
97
+        }
98
+    }
99
+
100
+    public String listNamesAndNumbers() {
101
+        {
102
+            String s = "";
103
+            //System.out.println(contacts.keySet());
104
+            for (String name : contacts.keySet()) {
105
+                ArrayList<String> phoneList = contacts.get(name);
106
+                for (int i = 0; i < phoneList.size(); i++) {
107
+                    s += " " + name + " " + " " + phoneList.get(i) + "\n";
108
+                }
109
+            }
110
+            return(s);
111
+        }
112
+    }
113
+}

+ 91
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Voir le fichier

@@ -1,7 +1,98 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
3 6
 /**
4 7
  * Created by leon on 1/23/18.
5 8
  */
6 9
 public class PhoneBookTest {
10
+
11
+    @Test
12
+    public void hasEntryTest() {
13
+        //Given
14
+        PhoneBook book = new PhoneBook();
15
+        String name = "sally fields";
16
+        String phonenumber = "214-335-4545";
17
+        //when
18
+        book.add(name, phonenumber);
19
+        boolean actualOutput = book.hasEntry(name);
20
+        //then
21
+        Assert.assertTrue(actualOutput);
22
+    }
23
+
24
+    @Test
25
+    public void addTest() {
26
+        //Given
27
+        PhoneBook book = new PhoneBook();
28
+        String name = "bruce willis";
29
+        String phonenumber = "123-456-7890";
30
+        //when
31
+        book.add(name, phonenumber);
32
+        String expected = "123-456-7890";
33
+        String actual = book.lookup(name);
34
+        Assert.assertEquals(expected, actual);
35
+        //then
36
+    }
37
+
38
+    @Test
39
+    public void lookupTest() {
40
+        PhoneBook book = new PhoneBook();
41
+        book.add("johnny", "302-555-6789");
42
+        String name = "johnny";
43
+
44
+        String expected = "302-555-6789";
45
+
46
+        String actual = book.lookup(name);
47
+        Assert.assertEquals(expected, actual);
48
+    }
49
+
50
+    @Test
51
+    public void reverseLookUpTest() {
52
+        PhoneBook book = new PhoneBook();
53
+        book.add("charlie chaplin", "123-456-7890");
54
+
55
+        String expected = "charlie chaplin";
56
+
57
+        String actual = book.reverseLookup("123-456-7890");
58
+        Assert.assertEquals(expected, actual);
59
+    }
60
+
61
+    @Test
62
+    public void removeTest() {
63
+        PhoneBook book = new PhoneBook();
64
+        book.add("johnny carson", "124-680-3345");
65
+        book.removeRecord("johnny carson");
66
+
67
+        boolean expected = false;
68
+
69
+        boolean actual = book.hasEntry("johnny carson");
70
+        Assert.assertEquals(expected, actual);
71
+    }
72
+
73
+    @Test
74
+    public void removeRecord() {
75
+        PhoneBook book = new PhoneBook();
76
+        book.add("jimbo", "210-2478-1111");
77
+        book.removeRecord("jimbo");
78
+
79
+        boolean expected = false;
80
+
81
+        boolean actual = book.hasEntry("jimbo");
82
+        Assert.assertEquals(expected, actual);
83
+
84
+    }
85
+    @Test
86
+    public void removePhoneNumber() {
87
+        PhoneBook book = new PhoneBook();
88
+        book.add("jimbo", "210-2478-1111");
89
+        book.removePhoneNumber("210-2478-1111");
90
+
91
+        String expected = "person not found!";
92
+
93
+        String actual = book.reverseLookup("210-2478-1111");
94
+        Assert.assertEquals(expected, actual);
95
+
96
+    }
97
+
7 98
 }