Browse Source

soooo close

Daniel Horowitz 6 years ago
parent
commit
2355aefa13

+ 36
- 27
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

1
 package com.zipcodewilmington.phonebook;
1
 package com.zipcodewilmington.phonebook;
2
 
2
 
3
-import java.util.Set;
4
-import java.util.TreeMap;
5
-import java.util.TreeSet;
6
-import java.util.Map;
3
+import java.util.*;
7
 
4
 
8
 /**
5
 /**
9
  * Created by leon on 1/23/18.
6
  * Created by leon on 1/23/18.
13
     private String name;
10
     private String name;
14
     private String number;
11
     private String number;
15
 
12
 
16
-    TreeMap<String, String> myTree = new TreeMap<String, String>();
13
+    TreeMap<String, ArrayList<String>> myTree = new TreeMap<String, ArrayList<String>>();
17
 
14
 
18
 
15
 
19
     public PhoneBook(){
16
     public PhoneBook(){
21
     }
18
     }
22
 
19
 
23
 
20
 
24
-    public void addEntry(String name, String number) {
25
-        myTree.put(name, number);
26
-
21
+    public void addEntry(String name, String... number) {
22
+        ArrayList<String> listOfPhoneNumbers = new ArrayList<String>(Arrays.asList(number));
23
+        myTree.put(name, listOfPhoneNumbers);
27
 
24
 
28
     }
25
     }
29
 
26
 
27
+    public void additionalPhoneNumberToKey(String name, String number) {
28
+        myTree.get(name).add(number);
29
+    }
30
 
30
 
31
-    public void remove(String name) {
32
-        myTree.remove(name);
33
 
31
 
32
+    public void removeNumberFromName(String name, String number) {
33
+        myTree.get(name).remove(number);
34
     }
34
     }
35
 
35
 
36
-    public String stringLookup(String name) {
37
 
36
 
38
-       return myTree.get(name);
37
+    public void removeEntry(String name) {
38
+        myTree.remove(name);
39
 
39
 
40
+    }
41
+
42
+    public String lookupNumber(String name) {
43
+        ArrayList<String> matchingNumbers = myTree.get(name);
44
+        String listedNumbers = "";
45
+        for (String phoneNumbers : matchingNumbers) {
46
+            listedNumbers +=phoneNumbers + "\n";
47
+        }
48
+       return listedNumbers.trim();
40
 
49
 
41
 
50
 
42
     }
51
     }
43
 
52
 
44
     public String listNames() {
53
     public String listNames() {
45
 
54
 
46
-
47
-
48
         Set<String> contacts = myTree.keySet();
55
         Set<String> contacts = myTree.keySet();
49
 
56
 
50
         String allNames = "";
57
         String allNames = "";
52
             allNames += contact + "\n";
59
             allNames += contact + "\n";
53
 
60
 
54
         }
61
         }
55
-        return allNames;
62
+        return allNames.trim();
56
     }
63
     }
57
 
64
 
58
     public String listPhoneBook() {
65
     public String listPhoneBook() {
59
 
66
 
67
+        Set<String> contacts = myTree.keySet();
60
         StringBuilder printBook = new StringBuilder();
68
         StringBuilder printBook = new StringBuilder();
61
 
69
 
62
-        for (Map.Entry<String, String> entry : myTree.entrySet()) {
63
-            String key = entry.getKey();
64
-            String value = entry.getValue();
65
-
66
-            printBook.append(key).append(" = ").append(value).append("\n");
70
+        for (String name : contacts) {
71
+            printBook.append(name).append(": ");
72
+            for (int x = 0; x<myTree.get(name).size(); x++) {
73
+                printBook.append(x).append("  ");
74
+            } printBook.append("\n");
67
 
75
 
68
         }
76
         }
69
         String result = printBook.toString();
77
         String result = printBook.toString();
73
 
81
 
74
     public String reverseLookup(String number) {
82
     public String reverseLookup(String number) {
75
 
83
 
76
-        String getName = "";
77
-        for (Map.Entry<String, String> entry : myTree.entrySet()) {
78
-            if (entry.getValue() == number) {
79
-                getName += entry.getKey();
80
-                return getName;
81
-            }
84
+        Set<String> contacts = myTree.keySet();
82
 
85
 
86
+        for (String name : contacts) {
87
+            for (int y = 0; y < myTree.get(name).size(); y++) {
88
+                if (myTree.get(name).get(y).equals(number)) {
89
+                    return name;
90
+                }
91
+            }
83
         }
92
         }
84
 
93
 
85
 
94
 
105
 
114
 
106
 
115
 
107
         phonebookEntry.listPhoneBook();
116
         phonebookEntry.listPhoneBook();
108
-        System.out.println(phonebookEntry.reverseLookup("4899994190"));
117
+        System.out.println(phonebookEntry.listPhoneBook());
109
 
118
 
110
     }
119
     }
111
 
120
 

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

16
     public void addTest() {
16
     public void addTest() {
17
         PhoneBook testBook = new PhoneBook();
17
         PhoneBook testBook = new PhoneBook();
18
         testBook.addEntry("Luke", "6109998787");
18
         testBook.addEntry("Luke", "6109998787");
19
-        String testNumber = testBook.stringLookup("Luke");
19
+        String testNumber = testBook.lookupNumber("Luke");
20
         Assert.assertTrue(testNumber.equals("6109998787"));
20
         Assert.assertTrue(testNumber.equals("6109998787"));
21
 
21
 
22
     }
22
     }
25
     public void addTest1() {
25
     public void addTest1() {
26
         PhoneBook testBook = new PhoneBook();
26
         PhoneBook testBook = new PhoneBook();
27
         testBook.addEntry("Kate", "4846569090");
27
         testBook.addEntry("Kate", "4846569090");
28
-        String testNumber = testBook.stringLookup("Kate");
28
+        String testNumber = testBook.lookupNumber("Kate");
29
         Assert.assertTrue(testNumber.equals("4846569090"));
29
         Assert.assertTrue(testNumber.equals("4846569090"));
30
 
30
 
31
     }
31
     }
34
     public void removeTest1() {
34
     public void removeTest1() {
35
         PhoneBook testBook = new PhoneBook();
35
         PhoneBook testBook = new PhoneBook();
36
         testBook.addEntry("Kate", "4846569090");
36
         testBook.addEntry("Kate", "4846569090");
37
-        testBook.remove("Kate");
38
-        String testNumber = testBook.stringLookup("Kate");
37
+        testBook.removeEntry("Kate");
38
+        String testNumber = testBook.lookupNumber("Kate");
39
         Assert.assertEquals(null, testNumber);
39
         Assert.assertEquals(null, testNumber);
40
 
40
 
41
 
41
 
45
     public void stringLookupTest() {
45
     public void stringLookupTest() {
46
         PhoneBook testBook = new PhoneBook();
46
         PhoneBook testBook = new PhoneBook();
47
         testBook.addEntry("Kate", "4846569090");
47
         testBook.addEntry("Kate", "4846569090");
48
-        String testNumber = testBook.stringLookup("Kate");
48
+        String testNumber = testBook.lookupNumber("Kate");
49
         Assert.assertFalse(testNumber.equals("4846587091"));
49
         Assert.assertFalse(testNumber.equals("4846587091"));
50
 
50
 
51
     }
51
     }
54
     public void stringLookupTest1() {
54
     public void stringLookupTest1() {
55
         PhoneBook testBook = new PhoneBook();
55
         PhoneBook testBook = new PhoneBook();
56
         testBook.addEntry("Kate", "4846569090");
56
         testBook.addEntry("Kate", "4846569090");
57
-        String testNumber = testBook.stringLookup("Kate");
57
+        String testNumber = testBook.lookupNumber("Kate");
58
         Assert.assertTrue(testNumber.equals("4846569090"));
58
         Assert.assertTrue(testNumber.equals("4846569090"));
59
 
59
 
60
     }
60
     }
79
         testBook.addEntry("Adam", "4864569090");
79
         testBook.addEntry("Adam", "4864569090");
80
         testBook.addEntry("Zeke", "4787869090");
80
         testBook.addEntry("Zeke", "4787869090");
81
         String actual = testBook.listPhoneBook();
81
         String actual = testBook.listPhoneBook();
82
-        String expected = "Adam = 4864569090\nKate = 4846569090\nZeke = 4787869090\n";
83
-        Assert.assertEquals(actual, expected);
82
+        String expected = "Adam: 4864569090\nKate: 4846569090\nZeke: 4787869090\n";
83
+        Assert.assertEquals(expected, actual);
84
 
84
 
85
 
85
 
86
     }
86
     }