Browse Source

almost finished part 3 but not quite

Carolynn Vansant 6 years ago
parent
commit
b28f8c66e1

+ 30
- 21
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

@@ -1,8 +1,5 @@
1 1
 package com.zipcodewilmington.phonebook;
2
-import java.util.TreeMap;
3
-import java.util.Set;
4
-import java.util.Iterator;
5
-import java.util.Map;
2
+import java.util.*;
6 3
 
7 4
 /**
8 5
  * Created by leon on 1/23/18.
@@ -10,12 +7,12 @@ import java.util.Map;
10 7
 public class PhoneBook {
11 8
 
12 9
     //instance variable
13
-    private TreeMap<String, String> treeMap;
10
+    private TreeMap<String, ArrayList<String>> treeMap;
14 11
 
15 12
 
16 13
     // void "no arguments" constructor -creates empty map
17 14
     public PhoneBook() {
18
-        this.treeMap= new TreeMap<String, String>();
15
+        this.treeMap= new TreeMap<String, ArrayList<String>>();
19 16
     }
20 17
 
21 18
     // single argument constructor -type Map -creates new map w/ same key/value mappings as argument
@@ -23,32 +20,45 @@ public class PhoneBook {
23 20
         this.treeMap = treeMap;
24 21
     }
25 22
 
26
-    //add a name & number entry
27
-    public void add(String name, String number) {
28
-
29
-        if(!treeMap.containsKey(name)) {
23
+    //add a name & number entry, add number
24
+    public void inputEntry(String name, ArrayList<String> number) {
25
+        if (!treeMap.containsKey(name)) {
30 26
             treeMap.put(name, number);
31 27
         }
28
+    }
29
+
30
+    //add and remove numbers from existing names
31
+    //unfinished, no test cases yet
32
+    public void modifyNumbers(String name, ArrayList<String> number) {
33
+        for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
34
+            ArrayList<String> value = entry.getValue();
35
+            for (String firstNumber : value) {
36
+
37
+            }
38
+        }
32 39
 
33 40
     }
34 41
 
42
+
35 43
     //remove a name & number entry
36
-    public void remove(String name) {
44
+    public void removeEntry(String name) {
37 45
             treeMap.remove(name);
38 46
     }
39 47
 
40 48
     //find phone number lookup by name
41
-    public String lookup(String name) {
42
-        String number = treeMap.get(name);
43
-        return number;
49
+    public ArrayList<String> lookup(String name) {
50
+        return treeMap.get(name);
44 51
     }
45 52
 
46 53
     //reverse lookup
47 54
     public String reverseLookup (String number) {
48 55
         String name = "";
49
-        for(Map.Entry<String, String> entry: treeMap.entrySet()) {
50
-            if(entry.getValue().equals(number)) {
51
-                name = entry.getKey();
56
+        for(Map.Entry<String, ArrayList<String>> entry: treeMap.entrySet()) {
57
+            ArrayList<String> value = entry.getValue();
58
+            for(String revnumber: value) {
59
+                if(number.contains(revnumber)) {
60
+                    name = entry.getKey();
61
+                }
52 62
             }
53 63
         }
54 64
         return name;
@@ -57,15 +67,14 @@ public class PhoneBook {
57 67
     //print out all of the entries in PhoneBook
58 68
     public String display(){
59 69
         StringBuilder printOut = new StringBuilder();
60
-        for(Map.Entry<String, String> entry: treeMap.entrySet()) {
70
+        for(Map.Entry<String, ArrayList<String>> entry: treeMap.entrySet()) {
61 71
             printOut
62 72
                     .append(entry.getKey())
63 73
                     .append(" ")
64
-                    .append(entry.getValue())
74
+                    .append(entry.getValue().toString())
65 75
                     .append("\n");
66 76
         }
67
-        String print = printOut.toString();
68
-        return print;
77
+        return printOut.toString();
69 78
     }
70 79
 
71 80
 

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

@@ -2,7 +2,9 @@ package com.zipcodewilmington.phonebook;
2 2
 
3 3
 import org.junit.Assert;
4 4
 import org.junit.Test;
5
+import sun.nio.cs.ext.SJIS;
5 6
 
7
+import java.util.ArrayList;
6 8
 import java.util.TreeMap;
7 9
 
8 10
 /**
@@ -19,7 +21,10 @@ public class PhoneBookTest {
19 21
     @Test
20 22
     public void testConstructorWithArgument() {
21 23
         PhoneBook book = new PhoneBook();
22
-        book.add("Bob", "302-555-1234");
24
+        ArrayList<String> number = new ArrayList<String>();
25
+        number.add("302-555-2222");
26
+        number.add("302-555-3456");
27
+        book.inputEntry("Bob", number);
23 28
         Assert.assertNotNull(book);
24 29
     }
25 30
 
@@ -28,11 +33,13 @@ public class PhoneBookTest {
28 33
 
29 34
         //Given
30 35
         PhoneBook book = new PhoneBook();
31
-        String expectedAddition = "302-555-1234";
32 36
 
37
+        ArrayList<String> expectedAddition = new ArrayList<String>();
38
+        expectedAddition.add("302-555-2222");
39
+        expectedAddition.add("302-555-3456");
33 40
         //When
34
-        book.add("Bob", "302-555-1234");
35
-        String actualAddition = book.lookup("Bob");
41
+        book.inputEntry("Bob", expectedAddition);
42
+        ArrayList<String> actualAddition = new ArrayList<String>(book.lookup("Bob"));
36 43
 
37 44
         //Then
38 45
         Assert.assertEquals(expectedAddition, actualAddition);
@@ -40,15 +47,18 @@ public class PhoneBookTest {
40 47
     }
41 48
 
42 49
     @Test
43
-    public void testRemove() {
50
+    public void testRemoveEntry() {
44 51
 
45 52
         //Given
46 53
         PhoneBook book = new PhoneBook();
47
-        book.add("Bob", "302-555-1234");
54
+        ArrayList<String> bobNumber = new ArrayList<String>();
55
+        bobNumber.add("302-555-2222");
56
+        bobNumber.add("302-555-3456");
57
+        book.inputEntry("Bob", bobNumber);
48 58
         String expectedValue = "";
49 59
 
50 60
         //When
51
-        book.remove("Bob");
61
+        book.removeEntry("Bob");
52 62
         String actualValue = book.display();
53 63
 
54 64
         //Then
@@ -56,46 +66,37 @@ public class PhoneBookTest {
56 66
     }
57 67
 
58 68
     @Test
59
-    public void testRemove2() {
69
+    public void testRemoveEntry2() {
60 70
 
61 71
         //Given
62 72
         PhoneBook book = new PhoneBook();
63
-        book.add("Bob", "302-555-1234");
64
-        String expectedValue = "";
73
+
74
+        ArrayList<String> expectedValue = new ArrayList<String>();
75
+        expectedValue.add("302-555-2222");
76
+        expectedValue.add("302-555-3456");
77
+        book.inputEntry("Bob", expectedValue);
65 78
 
66 79
         //When
67
-        book.remove("Robert");
80
+        book.removeEntry("Robert");
68 81
         String actualValue = book.display();
69 82
 
70 83
         //Then
71 84
         Assert.assertNotEquals(expectedValue, actualValue);
72
-
73
-
74 85
     }
75 86
 
76
-
77 87
     @Test
78 88
     public void testLookup(){
79
-
80 89
         //Given
81 90
         PhoneBook book = new PhoneBook();
82
-        String expectedNumber = "302-555-1234";
83
-        book.add("Bob", expectedNumber);
84
-        //When
85
-        String actualNumber = book.lookup("Bob");
86
-        //Then
87
-        Assert.assertEquals(expectedNumber, actualNumber);
88
-    }
89 91
 
90
-    @Test
91
-    public void testLookup2(){
92
+        ArrayList<String> expectedNumber = new ArrayList<String>();
93
+        expectedNumber.add("302-555-2223");
94
+        expectedNumber.add("302-555-3456");
95
+        book.inputEntry("Bob", expectedNumber);
92 96
 
93
-        //Given
94
-        PhoneBook book = new PhoneBook();
95
-        book.add("Bob", "302-555-1234");
96
-        String expectedNumber = null;
97 97
         //When
98
-        String actualNumber = book.lookup("Frank");
98
+        ArrayList<String> actualNumber = new ArrayList<String>(book.lookup("Bob"));
99
+
99 100
         //Then
100 101
         Assert.assertEquals(expectedNumber, actualNumber);
101 102
     }
@@ -105,11 +106,17 @@ public class PhoneBookTest {
105 106
 
106 107
         //Given
107 108
         PhoneBook book = new PhoneBook();
108
-        book.add("Bob", "302-555-1234");
109
-        String expectedNumber = "302-555-9988";
110
-        book.add("Frank", expectedNumber);
109
+
110
+        ArrayList<String> bobNumber = new ArrayList<String>();
111
+        bobNumber.add("302-555-2223");
112
+        book.inputEntry("Bob", bobNumber);
113
+
114
+        ArrayList<String> expectedNumber = new ArrayList<String>();
115
+        expectedNumber.add("302-555-9988");
116
+        book.inputEntry("Frank", expectedNumber);
117
+
111 118
         //When
112
-        String actualNumber = book.lookup("Frank");
119
+        ArrayList<String> actualNumber = new ArrayList<String> (book.lookup("Frank"));
113 120
         //Then
114 121
         Assert.assertEquals(expectedNumber, actualNumber);
115 122
     }
@@ -120,9 +127,13 @@ public class PhoneBookTest {
120 127
         //Given
121 128
         PhoneBook book = new PhoneBook();
122 129
         String expectedName = "Bob";
123
-        book.add(expectedName, "302-555-1234");
124
-        //When
125
-        String actualName = book.reverseLookup("302-555-1234");
130
+        ArrayList<String> bobNumber = new ArrayList<String>();
131
+        bobNumber.add("302-555-2223");
132
+        book.inputEntry(expectedName, bobNumber);
133
+
134
+        //When;
135
+        String actualName = book.reverseLookup("302-555-2223");
136
+
126 137
         //Then
127 138
         Assert.assertEquals(expectedName, actualName);
128 139
     }
@@ -130,11 +141,18 @@ public class PhoneBookTest {
130 141
 
131 142
     @Test
132 143
     public void testDisplay() {
133
-        //Given: need help from instructor
134
-        String expectedNameNumber = "Bob 302-555-1234\nFrank 777-555-1111\n";
144
+        //Given:
135 145
         PhoneBook book = new PhoneBook();
136
-        book.add("Bob", "302-555-1234");
137
-        book.add("Frank", "777-555-1111");
146
+        ArrayList<String> bobNumber = new ArrayList<String>();
147
+        bobNumber.add("302-555-1234");
148
+        bobNumber.add("302-556-1245");
149
+        ArrayList<String> frankNumber = new ArrayList<String>();
150
+        frankNumber.add("777-555-1111");
151
+        book.inputEntry("Bob", bobNumber);
152
+        book.inputEntry("Frank", frankNumber);
153
+
154
+        String expectedNameNumber = "Bob " + bobNumber.toString() + "\n" + "Frank " + frankNumber.toString() + "\n";
155
+
138 156
         //When
139 157
         String actualNameNumber = book.display();
140 158
         //Then