Browse Source

finished assisnment

Chad 6 years ago
parent
commit
47f0fb627d
3 changed files with 226 additions and 11 deletions
  1. 89
    1
      PhoneBook.java
  2. 112
    1
      PhoneBookTest.java
  3. 25
    9
      package.bluej

+ 89
- 1
PhoneBook.java View File

@@ -1,7 +1,95 @@
1
- 
1
+import java.util.*;
2
+import java.util.List;
3
+import java.util.Arrays;
4
+import java.util.Iterator;
2 5
 
3 6
 /**
4 7
  * Created by leon on 1/23/18.
5 8
  */
6 9
 public class PhoneBook {
10
+    String name;
11
+    String phoneNumber;
12
+    TreeMap<String,ArrayList<String>> directory = new TreeMap<String,ArrayList<String>>();
13
+    HashMap<ArrayList<String>,String> reverseDirectory = new HashMap<ArrayList<String>,String>();
14
+    public PhoneBook(){
15
+
16
+    }
17
+
18
+    public TreeMap getDirectory(){
19
+        return directory;
20
+    }
21
+
22
+    public HashMap<ArrayList<String>, String> invertMap() {
23
+        for(String k: directory.keySet()){
24
+            reverseDirectory.put(directory.get(k), k);
25
+        }
26
+        return reverseDirectory;
27
+    }
28
+
29
+    public TreeMap add(String name, ArrayList<String> phoneNumber){
30
+        directory.put(name, phoneNumber);
31
+        return directory;
32
+    }
33
+
34
+    public TreeMap removeRecord(String name){
35
+        directory.remove(name);
36
+        return directory;
37
+    }
38
+
39
+    public ArrayList<String> lookUp(String name){
40
+        return directory.get(name);
41
+    }
42
+
43
+    public String reverseLookUp(String phoneNumber){
44
+        this.invertMap();
45
+        String answer = "";
46
+        for(ArrayList<String> element: reverseDirectory.keySet()){
47
+            if(arrayContains(element, phoneNumber) == true){
48
+                answer = reverseDirectory.get(element);
49
+            }
50
+        }
51
+
52
+        return answer;
53
+    }
54
+
55
+    public String display(){
56
+        StringBuilder print = new StringBuilder("");
57
+
58
+        for(String key : directory.keySet()){
59
+            print.append(key + " " + printArray(directory.get(key)) + "\n");
60
+        }
61
+
62
+        return print.toString();
63
+    }
64
+
65
+    public String printArray(ArrayList<String> array){
66
+        String result = String.join(", ", array);
67
+
68
+        return result;
69
+    }
70
+
71
+    public boolean arrayContains(ArrayList<String> list, String phoneNumber){
72
+        boolean correct = false;
73
+
74
+        for(String element : list){
75
+            if(phoneNumber.equals(element)){
76
+                correct = true;
77
+                break;
78
+            }
79
+        }
80
+
81
+        return correct;
82
+    }
83
+
84
+    public TreeMap remove(String name, String phoneNumber){
85
+
86
+        Iterator search = directory.get(name).iterator();
87
+        while(search.hasNext()){
88
+            if(search.next().equals(phoneNumber)){
89
+                search.remove();
90
+                break;
91
+            }
92
+        }
93
+        return directory;
94
+    }
7 95
 }

+ 112
- 1
PhoneBookTest.java View File

@@ -1,9 +1,9 @@
1 1
 
2
-
3 2
 import static org.junit.Assert.*;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
6 5
 import org.junit.Test;
6
+import java.util.*;
7 7
 
8 8
 /**
9 9
  * The test class PhoneBookTest.
@@ -13,6 +13,7 @@ import org.junit.Test;
13 13
  */
14 14
 public class PhoneBookTest
15 15
 {
16
+    PhoneBook test = new PhoneBook();
16 17
     /**
17 18
      * Default constructor for test class PhoneBookTest
18 19
      */
@@ -30,6 +31,116 @@ public class PhoneBookTest
30 31
     {
31 32
     }
32 33
 
34
+    @Test
35
+    public void addTest(){
36
+        //Given
37
+        ArrayList<String> list = new ArrayList<String>();
38
+        list.add("5704282076");
39
+        //When
40
+        int expected = 1;
41
+        TreeMap actual = test.add("Chad", list);
42
+
43
+        //Result
44
+        assertEquals(expected, actual.size());
45
+    }
46
+
47
+    @Test
48
+    public void removeRecordTest(){
49
+        //Given
50
+        ArrayList<String> list = new ArrayList<String>();
51
+        list.add("5704282076");
52
+        list.add("5707429337");
53
+        test.add("Chad", list);
54
+        //When
55
+        int expected = 0;
56
+        TreeMap actual = test.removeRecord("Chad");
57
+
58
+        //Result
59
+        assertEquals(expected, actual.size());
60
+    }
61
+
62
+    @Test
63
+    public void lookUpTest(){
64
+        //Given
65
+        ArrayList<String> list = new ArrayList<String>();
66
+        list.add("5704282076");
67
+        test.add("Chad", list);
68
+        //When
69
+        ArrayList<String> expected = list;
70
+        ArrayList<String> actual = test.lookUp("Chad");
71
+
72
+        //Result
73
+        assertEquals(expected, actual);
74
+    }
75
+
76
+    @Test
77
+    public void invertMapTest(){
78
+        //Given
79
+        ArrayList<String> list = new ArrayList<String>();
80
+        list.add("5704282076");
81
+        test.add("Chad", list);
82
+        test.invertMap();
83
+        //When
84
+        String expected = "Chad";
85
+        String actual = test.reverseLookUp("5704282076");
86
+
87
+        //Result
88
+        assertEquals(expected, actual);
89
+    }
90
+
91
+    @Test
92
+    public void displayTest(){
93
+        //Given
94
+        ArrayList<String> list = new ArrayList<String>();
95
+        list.add("5704282076");
96
+        ArrayList<String> list1 = new ArrayList<String>();
97
+        list1.add("111-222-333");
98
+        ArrayList<String> list2 = new ArrayList<String>();
99
+        list2.add("222-444-4444");
100
+        test.add("Chad", list);
101
+        test.add("Zebra", list1);
102
+        test.add("Dog", list2);
103
+
104
+        //When
105
+        String expected = "Chad 5704282076\nDog 222-444-4444\nZebra 111-222-333\n";
106
+        String actual = test.display();
107
+
108
+        //Result
109
+        assertEquals(expected, actual);
110
+    }
111
+    
112
+    @Test
113
+    public void arrayContainsTest(){
114
+        //Given
115
+        ArrayList<String> list = new ArrayList<String>();
116
+        list.add("5704282076");
117
+        //When
118
+        boolean actual = test.arrayContains(list,"5704282076");
119
+        //Result
120
+        assertTrue(actual);
121
+    }
122
+
123
+    @Test
124
+    public void removeTest(){
125
+        //Given
126
+        ArrayList<String> list = new ArrayList<String>();
127
+        list.add("5704282076");
128
+        list.add("5707429337");
129
+        ArrayList<String> list1 = new ArrayList<String>();
130
+        list1.add("111-222-333");
131
+        ArrayList<String> list2 = new ArrayList<String>();
132
+        list2.add("222-444-4444");
133
+        test.add("Chad", list);
134
+        test.add("Zebra", list1);
135
+        test.add("Dog", list2);
136
+        
137
+        test.remove("Chad","5704282076");
138
+        //When
139
+        boolean actual = test.arrayContains(list,"5704282076");
140
+        //Result
141
+        assertFalse(actual);
142
+    }
143
+    
33 144
     /**
34 145
      * Tears down the test fixture.
35 146
      *

+ 25
- 9
package.bluej View File

@@ -1,20 +1,23 @@
1 1
 #BlueJ package file
2
+dependency1.from=PhoneBookTest
3
+dependency1.to=PhoneBook
4
+dependency1.type=UsesDependency
2 5
 editor.fx.0.height=0
3 6
 editor.fx.0.width=0
4 7
 editor.fx.0.x=0
5 8
 editor.fx.0.y=0
6
-objectbench.height=101
7
-objectbench.width=461
9
+objectbench.height=164
10
+objectbench.width=776
8 11
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
12
+package.divider.vertical=0.6845018450184502
13
+package.editor.height=364
11 14
 package.editor.width=674
12
-package.editor.x=427
13
-package.editor.y=143
15
+package.editor.x=496
16
+package.editor.y=94
14 17
 package.frame.height=600
15 18
 package.frame.width=800
16
-package.numDependencies=0
17
-package.numTargets=1
19
+package.numDependencies=1
20
+package.numTargets=2
18 21
 package.showExtends=true
19 22
 package.showUses=true
20 23
 project.charset=UTF-8
@@ -23,4 +26,17 @@ readme.name=@README
23 26
 readme.width=47
24 27
 readme.x=10
25 28
 readme.y=10
26
-
29
+target1.height=50
30
+target1.name=PhoneBookTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=120
34
+target1.x=100
35
+target1.y=10
36
+target2.height=50
37
+target2.name=PhoneBook
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=100
41
+target2.x=100
42
+target2.y=40