Bladeren bron

60% of the time, it works everytime

April Rivera 6 jaren geleden
bovenliggende
commit
bec5d555ec

+ 30
- 29
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Bestand weergeven

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

+ 17
- 18
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Bestand weergeven

@@ -4,7 +4,9 @@ import org.junit.Assert;
4 4
 import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7
-
7
+import java.util.ArrayList;
8
+import java.util.Map;
9
+import java.util.Set;
8 10
 
9 11
 
10 12
 /**
@@ -21,39 +23,36 @@ public class PhoneBookTest {
21 23
 
22 24
     }
23 25
     @Test
24
-    public void testAddContact(){
26
+    public void addContactTest(){
25 27
         phoneBook.addContact("Genya", "6578356767");
26 28
         boolean genyaIsInPhoneBook = phoneBook.getContactsList().keySet().contains("Genya");
27 29
         Assert.assertTrue(genyaIsInPhoneBook);
28 30
 
29 31
     }
30 32
     @Test
31
-    public void testRemoveContact(){
33
+    public void removeContactTest(){
32 34
         phoneBook.removeContact("Pete Jones");
33 35
         boolean peteJonesHasBeenRemoved = !phoneBook.getContactsList().containsKey("Pete Jones");
34 36
         Assert.assertTrue(peteJonesHasBeenRemoved);
35 37
     }
38
+
36 39
     @Test
37
-    public void testLookUpContactByName(){
38
-        phoneBook.lookUpContactByName("Nala Bits");
39
-        String expected = "2345578122";
40
-        String actual = phoneBook.getContactsList().get("Nala Bits");
41
-        Assert.assertEquals(expected, actual);
42
-    }
43
-    @Test
44
-    public void testListAllByName(){
45
-        phoneBook.listAllByName(phoneBook.getContactsList().keySet().toString());
46
-        String expected = ;
47
-        String actual = phoneBook.getContactsList().keySet().toString();
40
+    public void listAllNamesAndNumbersTest(){
41
+        phoneBook.listAllNamesAndNumbers();
42
+        String expected = new String();
43
+        Set<Map.Entry<String, ArrayList>> actual = phoneBook.getContactsList().entrySet();
48 44
         Assert.assertEquals(expected, actual);
49 45
     }
50 46
 
51 47
     @Test
52
-    public void testListAllNamesAndNumbers(){
53
-        phoneBook.listAllNamesAndNumber();
54
-        String expected = ;
55
-        String actual = phoneBook.getContactsList().entrySet();
48
+    public void reverseLookUpTest(){
49
+        phoneBook.reverseLookUp();
50
+        String expected = new String();
51
+        Set<Map.Entry<String, ArrayList>> actual = phoneBook.getContactsList().entrySet();
56 52
         Assert.assertEquals(expected, actual);
53
+
57 54
     }
55
+
58 56
 }
59 57
 
58
+