Sfoglia il codice sorgente

first commit, need to finish tests

April Rivera 6 anni fa
parent
commit
e3328a39c5

+ 56
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Vedi File

@@ -1,7 +1,63 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+import java.util.Map;
3
+import java.util.Set;
4
+import java.util.HashMap;
2 5
 
3 6
 /**
4 7
  * Created by leon on 1/23/18.
5 8
  */
6 9
 public class PhoneBook {
10
+
11
+    String name;
12
+    String number;
13
+
14
+
15
+    private Map<String, String> contactsList;
16
+
17
+    public PhoneBook(){
18
+        this.contactsList = new HashMap<String, String>();
19
+        contactsList.put("Pete Jones", "2678849087");
20
+        contactsList.put("Ron Burgundy", "5604329932");
21
+        contactsList.put("Nala Bits", "2345578122");
22
+
23
+    }
24
+    public Map<String, String> getContactsList(){
25
+        return contactsList;
26
+    }
27
+
28
+    public void addContact(String name, String number) {
29
+        if (!contactsList.containsKey(name)) {
30
+            contactsList.put(name, number);
31
+            System.out.println("New contact " + name + " has been added");
32
+        } else {
33
+            System.out.println("Contact already exists");
34
+        }
35
+    }
36
+
37
+    public void removeContact(String name) {
38
+        if (contactsList.containsKey(name)) {
39
+            contactsList.remove(name);
40
+            System.out.println("Contact has been removed");
41
+        } else {
42
+            System.out.println("No " + name + " contact exists");
43
+        }
44
+    }
45
+
46
+    public String lookUpContactByName(String name){
47
+        return contactsList.get(name) == null ? "Sorry name not found" : contactsList.get(name);
48
+    }
49
+
50
+    public void listAllByName(String name){
51
+        for(String ContactsByName : contactsList.keySet()){
52
+            System.out.println(contactsList.get(name));
53
+        }
54
+
55
+    }
56
+    public void listAllNamesAndNumbers(){
57
+        Set<Map.Entry<String, String>>listingAll = contactsList.entrySet();
58
+        System.out.println(contactsList);
59
+    }
60
+
61
+}
62
+
7 63
 }

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

@@ -4,4 +4,40 @@ package com.zipcodewilmington.phonebook;
4 4
  * Created by leon on 1/23/18.
5 5
  */
6 6
 public class PhoneBookTest {
7
+
8
+
9
+    PhoneBook phoneBook;
10
+
11
+    @Before
12
+    public void setUp(){
13
+        phoneBook = new PhoneBook();
14
+
15
+    }
16
+    @Test
17
+    public void testAddContact(){
18
+        phoneBook.addContact("Genya", "6578356767");
19
+        boolean genyaIsInPhoneBook = phoneBook.getContactsList().keySet().contains("Genya");
20
+        assertTrue(genyaIsInPhoneBook);
21
+
22
+    }
23
+    @Test
24
+    public void testRemoveContact(){
25
+        phoneBook.removeContact("Pete Jones");
26
+        boolean peteJonesHasBeenRemoved = !phoneBook.getContactsList().containsKey("Pete Jones");
27
+        assertTrue(peteJonesHasBeenRemoved);
28
+    }
29
+    @Test
30
+    public void testLookUpContactByName(){
31
+        phoneBook.lookUpContactByName("Nala Bits");
32
+        String expected = "2345578122";
33
+        String actual = phoneBook.getContactsList().get("Nala Bits");
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+    @Test
37
+    public void testListAllByName(){
38
+
39
+    }
40
+
41
+}
42
+
7 43
 }