Xcuello преди 6 години
родител
ревизия
f3abe98d55
променени са 2 файла, в които са добавени 176 реда и са изтрити 1 реда
  1. 81
    0
      src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java
  2. 95
    1
      src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java

+ 81
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Целия файл

@@ -1,7 +1,88 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.sql.SQLOutput;
4
+import java.util.ArrayList;
5
+import java.util.HashMap;
6
+import java.util.Map;
7
+import java.util.TreeMap;
8
+import java.util.Collection;
9
+
3 10
 /**
4 11
  * Created by leon on 1/23/18.
5 12
  */
13
+
6 14
 public class PhoneBook {
15
+
16
+    TreeMap <String, String> phoneBook = new TreeMap <String, String>();
17
+
18
+    public PhoneBook() {
19
+
20
+
21
+    }
22
+
23
+    public boolean hasEntry(String name) {
24
+
25
+
26
+
27
+        return phoneBook.containsKey(name);
28
+    }
29
+
30
+
31
+    public void add(String name, String phoneNumber) {
32
+
33
+
34
+        phoneBook.put(name, phoneNumber);
35
+
36
+
37
+
38
+    }
39
+
40
+    public String lookUp(String name) {
41
+
42
+
43
+        return phoneBook.get(name);
44
+
45
+
46
+    }
47
+
48
+    public void remove(String name) {
49
+
50
+
51
+        phoneBook.remove(name);
52
+
53
+    }
54
+
55
+    public String reverseLookUp(String phoneNumber) {
56
+
57
+        String result = "";
58
+
59
+        for(String keyName : phoneBook.keySet()) {
60
+
61
+            if(phoneBook.get(keyName).equals(phoneNumber)) {
62
+
63
+                result = keyName;
64
+
65
+            }
66
+        }
67
+
68
+        return result;
69
+    }
70
+
71
+    public void listNamesAndNumbers() {
72
+
73
+        for(String keyName : phoneBook.keySet()) {
74
+            System.out.println(keyName + " " + phoneBook.get(keyName));
75
+        }
76
+
77
+
78
+        }
79
+
80
+
81
+
82
+
83
+
7 84
 }
85
+
86
+
87
+
88
+

+ 95
- 1
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Целия файл

@@ -1,7 +1,101 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.TreeMap;
7
+
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
6 11
 public class PhoneBookTest {
7
-}
12
+
13
+
14
+
15
+
16
+
17
+    @Test
18
+    public void hasEntryTest() {
19
+        boolean containsKey;
20
+
21
+        PhoneBook phone = new PhoneBook();
22
+        String name = "xzavia";
23
+        String phoneNumber = "302-399-3694";
24
+        phone.add(name, phoneNumber);
25
+
26
+        boolean actual = phone.hasEntry(name);
27
+
28
+        Assert.assertTrue(actual);
29
+
30
+
31
+    }
32
+
33
+
34
+     @Test
35
+     public void lookUpTest() {
36
+
37
+         PhoneBook phone = new PhoneBook();
38
+         String name = "xzavia";
39
+         String phoneNumber = "302-399-3694";
40
+         phone.add(name, phoneNumber);
41
+         String expected = (phoneNumber);
42
+         String actual = phone.lookUp(name);
43
+
44
+         Assert.assertEquals(expected, actual);
45
+
46
+     }
47
+
48
+     @Test
49
+     public void removeTest() {
50
+
51
+         PhoneBook phone = new PhoneBook();
52
+         String name = "xzavia";
53
+         String phoneNumber = "302-399-3694";
54
+         phone.add(name, phoneNumber);
55
+
56
+         phone.remove(name);
57
+
58
+         boolean actual = phone.hasEntry(name);
59
+
60
+
61
+         Assert.assertFalse(actual);
62
+
63
+
64
+
65
+     }
66
+     @Test
67
+    public void reverseLookUpTest() {
68
+
69
+         PhoneBook phone = new PhoneBook();
70
+         String name = "xzavia";
71
+         String phoneNumber = "302-399-3694";
72
+         phone.add(name, phoneNumber);
73
+         String expected = (name);
74
+         String actual = phone.reverseLookUp(phoneNumber);
75
+
76
+         Assert.assertEquals(expected,actual);
77
+
78
+
79
+     }
80
+
81
+
82
+
83
+    //@Test
84
+    //public void testContainsValueTest() {
85
+        // Given
86
+        // WE have a new phone book
87
+       // TreeMap <Integer, String> phoneBook = new TreeMap<Integer, String>();
88
+
89
+        // When
90
+        // We add an entry
91
+       // phoneBook.put(1 , "name");
92
+
93
+        // Then
94
+        // It will contain an entry
95
+        //Assert.assertTrue(phoneBook.containsValue("name"));
96
+
97
+
98
+    }
99
+
100
+
101
+