Jose Bedolla 6 年 前
コミット
d36ffc0fad
共有2 個のファイルを変更した144 個の追加0 個の削除を含む
  1. 61
    0
      src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java
  2. 83
    0
      src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java

+ 61
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java ファイルの表示

@@ -1,7 +1,68 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.TreeMap;
5
+
3 6
 /**
4 7
  * Created by leon on 1/23/18.
5 8
  */
6 9
 public class PhoneBook {
10
+
11
+    TreeMap<String, String> pb = new TreeMap<String, String>();
12
+
13
+    public PhoneBook() {
14
+    }
15
+
16
+    public boolean hasEntry(String name) {
17
+
18
+        if(pb.containsKey(name))
19
+        {
20
+            return true;
21
+        }
22
+
23
+        return false;
24
+    }
25
+
26
+    public void add(String name, String phoneNumber) {
27
+
28
+        pb.put(name, phoneNumber);
29
+
30
+    }
31
+
32
+
33
+
34
+    public String lookUp(String name) {
35
+
36
+        String number = pb.get(name);
37
+
38
+        return number;
39
+    }
40
+
41
+    public boolean remove(String name) {
42
+
43
+        pb.remove(name);
44
+        if(pb.containsKey(name))
45
+        {
46
+            return false;
47
+        }
48
+
49
+        return true;
50
+    }
51
+
52
+    public String reverseLookUp(String phoneN) {
53
+        String hold = "";
54
+        for(String a: pb.keySet())
55
+        {
56
+            if (pb.get(a).contains(phoneN))
57
+            {
58
+                hold+=a;
59
+            }
60
+        }
61
+        return hold;
62
+    }
63
+
64
+    public String namesAndNumbers() {
65
+
66
+        return String.valueOf(pb);
67
+    }
7 68
 }

+ 83
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java ファイルの表示

@@ -1,7 +1,90 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
3 6
 /**
4 7
  * Created by leon on 1/23/18.
5 8
  */
9
+
6 10
 public class PhoneBookTest {
11
+
12
+    public PhoneBookTest() {
13
+    }
14
+
15
+
16
+
17
+    @Test
18
+    public void testHasEntry()
19
+    {
20
+
21
+        PhoneBook phone = new PhoneBook();
22
+        String name = "jose";
23
+        String phoneNumber = "302-903-9837";
24
+        phone.add(name,phoneNumber);
25
+
26
+        boolean actual = phone.hasEntry(name);
27
+        boolean expected = true;
28
+
29
+        Assert.assertTrue(actual);
30
+    }
31
+
32
+
33
+    @Test
34
+    public void testLookUp()
35
+    {
36
+        PhoneBook phone = new PhoneBook();
37
+        String name = "jose";
38
+        String phoneNumber = "302-903-9837";
39
+        phone.add(name, phoneNumber);
40
+
41
+        String actual = phone.lookUp(name);
42
+        String exoected =  phoneNumber;
43
+        Assert.assertEquals(exoected,actual);
44
+    }
45
+
46
+    @Test
47
+    public void removeTest()
48
+    {
49
+        PhoneBook phone = new PhoneBook();
50
+        String name = "jose ";
51
+        String phoneNum = "587-242-3413";
52
+        phone.add(name,phoneNum);
53
+
54
+        boolean expected = true;
55
+        boolean actual = phone.remove(name);
56
+
57
+        Assert.assertEquals(expected,actual);
58
+
59
+    }
60
+    @Test
61
+    public void reverseLookUpTest()
62
+    {
63
+        PhoneBook phone = new PhoneBook();
64
+        String name = "jose";
65
+        String phoneN = "947-282-8262";
66
+        phone.add(name,phoneN);
67
+
68
+        String expected = name;
69
+        String actual = phone.reverseLookUp(phoneN);
70
+
71
+        Assert.assertEquals(expected,actual);
72
+    }
73
+
74
+    @Test
75
+    public void namesAndNumbersTest()
76
+    {
77
+        PhoneBook phone = new PhoneBook();
78
+        String name = "jose";
79
+        String phoneNumber = "938-938-9392";
80
+        phone.add(name,phoneNumber);
81
+
82
+        String expected = "{"+name+"="+phoneNumber+"}";
83
+
84
+        String actual = phone.namesAndNumbers();
85
+
86
+        Assert.assertEquals(expected,actual);
87
+
88
+    }
89
+    
7 90
 }