Przeglądaj źródła

added multiple phonenumber method

Keith Brinker 6 lat temu
rodzic
commit
9422e30a9b

+ 34
- 29
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Wyświetl plik

@@ -3,72 +3,77 @@ package com.zipcodewilmington.phonebook;
3 3
 import java.util.TreeMap;
4 4
 import java.util.Set;
5 5
 import java.util.ArrayList;
6
+
6 7
 /**
7 8
  * Created by leon on 1/23/18.
8 9
  */
9 10
 public class PhoneBook {
10 11
 
11
-        String name;
12
-        String phonenum;
13
-        //TreeMap<String, String> contacts = new TreeMap<String, String>();
12
+    String name;
13
+    String phonenum;
14
+    //TreeMap<String, String> contacts = new TreeMap<String, String>();
14 15
     //   TreeMap<String, ArrayList> contacts = new TreeMap<String, ArrayList<String>>();
15 16
     TreeMap<String, PhoneNumberStorage> contacts = new TreeMap<String, PhoneNumberStorage>();
16 17
 
17 18
 
19
+    public PhoneBook() {
20
+    }
18 21
 
19
-public PhoneBook() {
20
-}
21
-
22
- public void addName(String name, String phonenum){
23
-       contacts.put(name, new PhoneNumberStorage(name, phonenum));
24
- }
22
+    public void addName(String name, String phonenum) {
23
+        contacts.put(name, new PhoneNumberStorage(name, phonenum));
24
+    }
25 25
 
26
-    public void addName(String name, PhoneNumberStorage phoneNumberStorage){
26
+    public void addName(String name, PhoneNumberStorage phoneNumberStorage) {
27 27
         contacts.put(name, phoneNumberStorage);
28 28
     }
29 29
 
30
- public void deleter(String name){
30
+    public void deleter(String name) {
31 31
         contacts.remove(name);
32
- }
32
+    }
33 33
 
34
- public PhoneNumberStorage lookup(String name){
34
+    public PhoneNumberStorage lookup(String name) {
35 35
         return contacts.get(name);
36
- }
36
+    }
37 37
 
38
- public String listNames(){
39
-    StringBuilder namesnumbers = new StringBuilder();
38
+    public String listNames() {
39
+        StringBuilder namesnumbers = new StringBuilder();
40 40
         Set<String> keys = contacts.keySet();
41
-        for(String key: keys)
41
+        for (String key : keys)
42 42
 
43 43
         {
44 44
 
45
-            namesnumbers.append(key+"\n");
45
+            namesnumbers.append(key + "\n");
46 46
         }
47 47
         return namesnumbers.toString();
48
- }
48
+    }
49 49
 
50
-    public String listNamesAndNumbers(){
50
+    public String listNamesAndNumbers() {
51 51
         StringBuilder namesnumbers = new StringBuilder();
52 52
         Set<String> keys = contacts.keySet();
53
-        for(String key: keys)
54
-        {
55
-            namesnumbers.append(key+" "+contacts.get(key)+"\n");
53
+        for (String key : keys) {
54
+            namesnumbers.append(key + " " + contacts.get(key) + "\n");
56 55
         }
57 56
         return namesnumbers.toString();
58 57
     }
59 58
 
60
-    public String reverselookup(String number){
59
+    public String reverselookup(String number) {
61 60
         //Set<String> names = contacts.keySet();
62
-        for(PhoneNumberStorage nums : contacts.values())
63
-        { for( String phoneNumbers : nums.getPhoneNumbers())
61
+        for (PhoneNumberStorage nums : contacts.values()) {
62
+            for (String phoneNumbers : nums.getPhoneNumbers())
64 63
 
65
-            if (number.equals(phoneNumbers)){
66
-                return nums.lookup();
67
-            }
64
+                if (number.equals(phoneNumbers)) {
65
+                    return nums.lookup();
66
+                }
68 67
         }
69 68
         return null;
70 69
     }
71 70
 
71
+    public void addmultiple(String name, String... numbers){
72
+        this.name = name;
73
+        for (int i = 0; i < numbers.length; i++){
74
+            contacts.get(name).add(numbers[i]);
75
+        }
76
+    }
72 77
 
73 78
 
74 79
 }

+ 7
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneNumberStorage.java Wyświetl plik

@@ -20,7 +20,10 @@ public class PhoneNumberStorage {
20 20
     }
21 21
 
22 22
 
23
+public void add(String phonenumber){
24
+        phoneNumbers.add(phonenumber);
23 25
 
26
+}
24 27
 
25 28
     public String lookup(){
26 29
         return this.name;
@@ -29,4 +32,8 @@ public class PhoneNumberStorage {
29 32
     public ArrayList<String> getPhoneNumbers() {
30 33
         return phoneNumbers;
31 34
     }
35
+
36
+    public PhoneNumberStorage(String name, String... number) {
37
+        this.name = name;
38
+        this.phoneNumbers = phoneNumbers;}
32 39
 }

+ 10
- 8
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Wyświetl plik

@@ -90,15 +90,17 @@ public class PhoneBookTest {
90 90
         Assert.assertEquals("Keith", testnumber);
91 91
     }
92 92
 
93
-//    @Test
94
-//    public void testPhoneBookLookUpMultiple(){
95
-//        PhoneBook testbook = new PhoneBook();
96
-//
97
-//        testbook.add("Keith",
98
-//        String testnumber = testbook.lookup("Keith");
99
-//        Assert.assertEquals("555-666-7777", "555-333-9999", testnumber);
100
-//    }
93
+   @Test
94
+   public void testPhoneBookAddMultiple() {
95
+       // Given
96
+       PhoneBook testbook = new PhoneBook();
97
+       PhoneNumberStorage temp = new PhoneNumberStorage("Keith", "555-666-7777", "555-000-8888");
98
+       testbook.addName("Keith", temp);
99
+
100
+       PhoneNumberStorage testNumber = testbook.lookup("Keith");
101
+       Assert.assertTrue(testNumber.equals(temp));
101 102
 
103
+   }
102 104
 
103 105
 
104 106
 }