Ahmad Rusdi 6 anos atrás
pai
commit
acc5a78bf1
3 arquivos alterados com 148 adições e 31 exclusões
  1. 64
    2
      PhoneBook.java
  2. 65
    23
      PhoneBookTest.java
  3. 19
    6
      package.bluej

+ 64
- 2
PhoneBook.java Ver arquivo

@@ -1,7 +1,69 @@
1
- 
2
-
3 1
 /**
4 2
  * Created by leon on 1/23/18.
5 3
  */
4
+import java.util.*;
5
+
6 6
 public class PhoneBook {
7
+    private TreeMap<String, ArrayList<String>> book = new TreeMap<String, ArrayList<String>>();
8
+
9
+    public ArrayList<String> add(String name, String phoneNumber) {
10
+        ArrayList<String> converted = new ArrayList<String>();
11
+
12
+        if (book.containsKey(name)) converted = book.get(name);
13
+
14
+        converted.add(phoneNumber);
15
+        add(name, converted);
16
+        return book.get(name);
17
+    }
18
+
19
+    public ArrayList<String> add(String name, ArrayList<String> phoneNumbers) {
20
+        book.put(name, phoneNumbers);
21
+        return book.get(name);
22
+    }
23
+
24
+    public TreeMap<String, ArrayList<String>> remove(String name) {
25
+        book.remove(name);
26
+        return book;
27
+    }
28
+
29
+    public ArrayList<String> remove(String name, String number) {
30
+        ArrayList<String> numbers = book.get(name);
31
+        numbers.remove(numbers.indexOf(number));
32
+        book.put(name, numbers);
33
+        return book.get(name);
34
+    }
35
+
36
+    public ArrayList<String> lookup(String name) {
37
+        return book.get(name);
38
+    }
39
+
40
+    public String reverseLookup(String phoneNumber) {
41
+        String result = "none";
42
+        for(Map.Entry<String, ArrayList<String>> entry : book.entrySet()) {
43
+            ArrayList<String> numbers = entry.getValue();
44
+            for (String number : numbers) {
45
+                if (number.equals(phoneNumber)) result = entry.getKey();
46
+            }
47
+        }
48
+        return result;
49
+    }
50
+
51
+    public String display() {
52
+        StringBuilder result = new StringBuilder();
53
+        for(Map.Entry<String, ArrayList<String>> entry : book.entrySet()) {
54
+            result.append(entry.getKey() + " ");
55
+
56
+            ArrayList<String> numbers = entry.getValue();
57
+            for (String number : numbers) {
58
+                result.append(number + " ");
59
+            }
60
+
61
+            result.append("\n");
62
+        }
63
+        return result.toString();
64
+    }
65
+
66
+    public static void main(String[] args) {
67
+        PhoneBook phoneBook = new PhoneBook();
68
+    };
7 69
 }

+ 65
- 23
PhoneBookTest.java Ver arquivo

@@ -1,9 +1,8 @@
1
-
2
-
3 1
 import static org.junit.Assert.*;
4 2
 import org.junit.After;
5 3
 import org.junit.Before;
6 4
 import org.junit.Test;
5
+import java.util.*;
7 6
 
8 7
 /**
9 8
  * The test class PhoneBookTest.
@@ -13,30 +12,73 @@ import org.junit.Test;
13 12
  */
14 13
 public class PhoneBookTest
15 14
 {
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
20
-    {
21
-    }
15
+    private PhoneBook pb = new PhoneBook();
22 16
 
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28 17
     @Before
29
-    public void setUp()
30
-    {
18
+    public void setup() {
19
+        pb = new PhoneBook();
20
+    }
21
+
22
+    @Test
23
+    public void addNameStringNumbers() {
24
+        assertEquals(
25
+            pb.add("John", "101-101-1011"),
26
+            new ArrayList<String>(
27
+                Arrays.asList("101-101-1011")
28
+                )
29
+        );
31 30
     }
32 31
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
32
+    @Test
33
+    public void addNameArrayList() {
34
+        ArrayList<String> numbers = new ArrayList<String>(Arrays.asList(
35
+                    "101-101-1011",
36
+                    "202-202-2022"
37
+                    ));
38
+        assertEquals(
39
+                pb.add("John", numbers),
40
+                numbers
41
+                );
42
+    };
43
+
44
+    @Test
45
+    public void removeByName() {
46
+        pb.add("John", "101-101-1011");
47
+        pb.remove("John");
48
+        
49
+        assertEquals(
50
+                pb.lookup("John"),
51
+                null
52
+                );
53
+    }
54
+    
55
+    @Test
56
+    public void removeMultipleNumbers() {
57
+        pb.add("John", "101-101-1011");
58
+        pb.add("John", "201-101-1011");
59
+        pb.remove("John", "201-101-1011");
60
+        
61
+        assertEquals(
62
+            pb.lookup("John"),
63
+            new ArrayList<String>(Arrays.asList("101-101-1011"))
64
+            );
65
+    }
66
+    
67
+    @Test
68
+    public void lookup() {
69
+        pb.add("John", "101-101-1011");
70
+        assertEquals(
71
+            pb.lookup("John"),
72
+            new ArrayList<String>(Arrays.asList("101-101-1011"))
73
+            );
74
+    }
75
+    
76
+    @Test
77
+    public void reverseLookup() {
78
+        pb.add("John", "101-101-1011");
79
+        assertEquals(
80
+            pb.reverseLookup("101-101-1011"),
81
+            "John"
82
+            );
41 83
     }
42 84
 }

+ 19
- 6
package.bluej Ver arquivo

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