Browse Source

PhoneBook

Kibret Tecle 6 years ago
parent
commit
077b732886

+ 47
- 52
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,81 +1,76 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-import java.util.List;
4
-import java.util.Map;
5
-import java.util.Set;
6
-import java.util.TreeMap;
3
+import java.util.*;
7 4
 
8 5
 /**
9 6
  * Created by leon on 1/23/18.
10 7
  */
11 8
 public class PhoneBook {
12
-    Map<String,String> phoneBook = new TreeMap<String, String>();
13
-    Set<String>names;
14
-    Set<String>phoneBookList;
15
-    String nameLists="";
16
-    String namesAndNumbers ="";
17
-
18
-
19
-    public PhoneBook(Map<String, String> phoneBook) {
20
-        this.phoneBook = phoneBook;
21
-    }
9
+    Map<String, List<String>> phoneBook;
22 10
 
23 11
     public PhoneBook() {
24
-
12
+        this.phoneBook = new TreeMap<String, List<String>>();
25 13
     }
26 14
 
27
-    public boolean add(String name,String phoneNumber) {
28
-
29
-     phoneBook.put(name, phoneNumber);
30
-     if(phoneBook.get(name).equals(phoneNumber)){
31
-         return true;
32
-     }return false;
33
-    }
15
+    public boolean add(String name, String phone) {
16
+        if (phoneBook.containsKey(name)) {
17
+            phoneBook.get(name).add(phone);
18
+            return true;
34 19
 
35
-    public boolean remove(String name){
36
-        if(phoneBook.containsKey(name)){
37
-            phoneBook.remove(name);
38
-        }
39
-        else{
40
-            System.out.println(name+" does not exist in the phone book list");
20
+        } else {
21
+            List<String> contactNumbers = new ArrayList<String>();
22
+            contactNumbers.add(phone);
23
+            phoneBook.put(name, contactNumbers);
41 24
         }
42
-        return !phoneBook.containsKey(name);
43 25
 
26
+        return true;
27
+    }
44 28
 
29
+    public boolean remove(String name) {
30
+        return phoneBook.remove(name) != null;
45 31
     }
46 32
 
47
-        public String lookup(String name){
33
+    public String lookup(String name) {
34
+        String myNumbers = "";
35
+        for (String key : phoneBook.keySet()) {
36
+            for (int i = 0; i < phoneBook.get(key).size(); i++) {
37
+                myNumbers += phoneBook.get(key).get(i) + "  ";
38
+            }
48 39
 
49
-        return phoneBook.get(name);
50 40
         }
41
+        return myNumbers;
42
+    }
51 43
 
52
-        public String listNames(){
53
-        if(phoneBook.isEmpty()){
54
-            return null;
55
-        }else {
56
-            Set<String> names = phoneBook.keySet();
57
-
58
-            for (String name : names) {
59
-                nameLists += name + "\n";
60
-            }
61
-            return nameLists;
44
+    public String listNames() {
45
+        String names = "";
46
+        for (String a : phoneBook.keySet()) {
47
+            names += a + "\n";
62 48
         }
63 49
 
64
-        }
50
+        return names;
51
+    }
65 52
 
66
-        public String listNamesAndNumbers(){
67
-            if(phoneBook.isEmpty()){
68
-                return null;
69
-            }else {
70
-                Set<String> PhoneBookList = phoneBook.keySet();
71
-                for (String nameKeys : phoneBookList) {
72
-                    namesAndNumbers += nameKeys + "   " + phoneBook.get(nameKeys) + "\n";
73
-                }
53
+    public String listNamesAndNumbers() {
54
+        StringBuilder namesAndNumbers = new StringBuilder();
55
+        for (String keys : phoneBook.keySet()) {
56
+            List<String> contact = phoneBook.get(keys);
57
+            for (String num : contact) {
58
+                namesAndNumbers.append(keys).append("    ").append(num);
74 59
             }
75
-
76
-        return namesAndNumbers;
60
+            namesAndNumbers.append("\n");
77 61
         }
62
+        return namesAndNumbers.toString();
63
+    }
78 64
 
65
+    public String reverseLookUp(String number) {
66
+        for (String keys : phoneBook.keySet()) {
67
+            for (int i = 0; i < phoneBook.get(keys).size(); i++) {
68
+                if (phoneBook.get(keys).get(i).equals(number))
69
+                    return keys;
70
+            }
71
+        }
72
+        return null;
73
+    }
79 74
 
80 75
 
81 76
 }

+ 39
- 20
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -5,6 +5,7 @@ import org.junit.Assert;
5 5
 import org.junit.Before;
6 6
 import org.junit.Test;
7 7
 
8
+import java.util.List;
8 9
 import java.util.Map;
9 10
 import java.util.TreeMap;
10 11
 
@@ -16,14 +17,12 @@ import java.util.TreeMap;
16 17
 
17 18
 
18 19
 public class PhoneBookTest {
19
-    private static PhoneBook phoneBookRecord;
20
+    private static PhoneBook phoneBookTest;
20 21
 
21
-    @Before
22
-    public void setUp() {
23
-        //PhoneBook phoneBookRecord;
24
-        Map<String,String>initialRecord = new TreeMap<String, String>(){ {put("John","1234567890");put("Micheal","5678901234");
25
-            put("James","1234554321");}};
26
-        this.phoneBookRecord=new PhoneBook(initialRecord);
22
+     @Before
23
+     public void setUp() {
24
+        PhoneBook phoneBookTest = new PhoneBook();
25
+        phoneBookTest.add("John","1234567890");
27 26
 
28 27
     }
29 28
 
@@ -32,42 +31,62 @@ public class PhoneBookTest {
32 31
     }
33 32
 
34 33
     @Test
35
-    public void testLookup() {
36
-        String expected = "5678901234";
37
-        String actual = phoneBookRecord.lookup("Micheal");
38
-        Assert.assertEquals(expected, actual);
34
+   public void testLookup() {
35
+        PhoneBook phoneBookTest = new PhoneBook();
36
+        phoneBookTest.add("James","555555555");
37
+
38
+        String actual = phoneBookTest.lookup("John");
39
+        Assert.assertEquals("555555555  ",actual);
39 40
     }
40 41
     @Test
41 42
     public void testListNames(){
43
+        PhoneBook phoneBookTest = new PhoneBook();
44
+        phoneBookTest.add("John","1234567890");
45
+        phoneBookTest.add("James","555555555");
42 46
         String expected = "James\n" +
43
-                            "John\n"+
44
-                            "Micheal\n";
45
-        String actual = phoneBookRecord.listNames();
47
+                            "John\n";
48
+        String actual = phoneBookTest.listNames();
46 49
         Assert.assertEquals(expected, actual);
47 50
 
48 51
     }
49 52
     @Test
50 53
     public void testListNumbersAndNumbers(){
51
-        String expected = "James   "+ "1234554321\n"+
52
-                "John   "+"1234567890\n"+
53
-                "Micheal   "+"5678901234";
54
-        String actual =phoneBookRecord.listNamesAndNumbers();
54
+        PhoneBook phoneBookTest = new PhoneBook();
55
+        phoneBookTest.add("John","1234567890");
56
+        phoneBookTest.add("James","555555555");
57
+        phoneBookTest.add("Micheal","5678901234");
58
+        String expected = "James    "+ "555555555\n"+
59
+                "John    "+"1234567890\n"+
60
+                "Micheal    "+"5678901234\n";
61
+        String actual =phoneBookTest.listNamesAndNumbers();
55 62
         Assert.assertEquals(expected, actual);
56 63
     }
57 64
 
58 65
     @Test
59 66
     public void testAdd(){
67
+        PhoneBook phoneBookTest = new PhoneBook();
60 68
         boolean expected = true;
61
-        boolean actual = phoneBookRecord.add("Peter","7777777777");
69
+        boolean actual = phoneBookTest.add("Peter","7777777777");
62 70
         Assert.assertEquals(expected, actual);
63 71
 
64 72
     }
65 73
     @Test
66 74
     public void testRemove(){
75
+        PhoneBook phoneBookTest = new PhoneBook();
76
+        phoneBookTest.add("John","1234567890");
67 77
         boolean expected =true;
68
-        boolean actual = phoneBookRecord.remove("John");
78
+        boolean actual = phoneBookTest.remove("John");
69 79
         Assert.assertEquals(expected, actual);
70 80
     }
81
+    @Test
82
+    public void testReverseLookup() {
83
+        PhoneBook phoneBookTest = new PhoneBook();
84
+        phoneBookTest.add("James","555555555");
85
+
86
+        String actual = phoneBookTest.reverseLookUp("555555555");
87
+        Assert.assertEquals("James",actual);
88
+    }
89
+
71 90
 
72 91
 
73 92
 }