Bladeren bron

phonebook

Soujanya Buragapu 6 jaren geleden
bovenliggende
commit
f0d8a9976d

+ 84
- 4
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Bestand weergeven

@@ -1,7 +1,87 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
3
+import java.util.Map;
4
+
5
+import java.util.TreeMap;
6
+
7
+
8
+
6 9
 public class PhoneBook {
7
-}
10
+
11
+    private TreeMap<String, String> treemap;
12
+
13
+    public PhoneBook() {
14
+        treemap = new TreeMap<String, String>();
15
+        addInitialList();
16
+    }
17
+
18
+    public void setTreemap(String name, String phoneNumber) {
19
+        this.treemap.put(name, phoneNumber);
20
+
21
+    }
22
+
23
+    public boolean hasEntry(String name) {
24
+
25
+        if (treemap.containsKey(name)) {
26
+            return true;
27
+        } else
28
+            return false;
29
+    }
30
+
31
+    public String add(String name, String phoneNumber) {
32
+        String str = this.treemap.put(name, phoneNumber);
33
+        return str;
34
+    }
35
+
36
+
37
+    public String lookup(String name) {
38
+
39
+        String str = treemap.get(name);
40
+        return str;
41
+    }
42
+
43
+    public String hasRemove(String name) {
44
+
45
+        return name;
46
+    }
47
+
48
+
49
+    public void addInitialList() {
50
+
51
+        this.setTreemap("John", "111-111-1111");
52
+        this.setTreemap("Kayle", "408-111-2222");
53
+        this.setTreemap("Tyler", "408-999-3333");
54
+
55
+    }
56
+
57
+    public String reverseLookup(String phoneNumber) {
58
+        String key = "";
59
+        for (Map.Entry<String, String> entry : treemap.entrySet()) {
60
+            if (entry.getValue().equals(phoneNumber)) {
61
+                key = entry.getKey();
62
+            }
63
+        }
64
+        return key;
65
+    }
66
+
67
+    public String listNamesAndNumbers() {
68
+
69
+        StringBuffer buffer = new StringBuffer();
70
+
71
+        for (Map.Entry<String, String> entry : treemap.entrySet()) {
72
+
73
+
74
+            buffer.append(entry.getKey());
75
+            buffer.append(" ");
76
+            buffer.append(entry.getValue());
77
+            buffer.append("\n");
78
+        }
79
+        return buffer.toString();
80
+
81
+    }
82
+
83
+    public void clearSortedMap() {
84
+        treemap.clear();
85
+    }
86
+
87
+}

+ 100
- 4
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Bestand weergeven

@@ -1,7 +1,103 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
6
-public class PhoneBookTest {
3
+import com.zipcodewilmington.phonebook.PhoneBook;
4
+import org.junit.Assert;
5
+import org.junit.Test;
6
+
7
+public class PhoneBookTest
8
+{
9
+    private static final int Expected = 0;
10
+
11
+
12
+@Test
13
+ public void testHasEntry()
14
+{
15
+        //Given
16
+        PhoneBook book = new PhoneBook();
17
+        String name = "John";
18
+        String phoneNumber = "111-111-1111";
19
+        book.hasEntry(name);
20
+        //When
21
+        boolean actualOutput = book.hasEntry(name);
22
+        //Then
23
+        Assert.assertEquals(true,actualOutput);
24
+        }
25
+
26
+@Test
27
+public void testAdd(){
28
+        //Given
29
+        PhoneBook book = new PhoneBook();
30
+        String name = "kayle";
31
+        String phoneNumber = "408-111-2222";
32
+
33
+        //When
34
+        //book.add(name, phoneNumber);
35
+        String actualOutput = book.add(name, phoneNumber);
36
+        //Then
37
+        Assert.assertEquals(null, actualOutput);
38
+        }
39
+
40
+@Test
41
+public void testLookup()
42
+        {
43
+        //Given
44
+        PhoneBook book = new PhoneBook();
45
+        String name = "Tayler";
46
+        String phoneNumber = "408-999-3333";
47
+
48
+        String actualOutput=book.lookup(name);
49
+
50
+        Assert.assertEquals(phoneNumber, actualOutput);
51
+        }
52
+
53
+@Test
54
+public void testLookup_forMultipleEntries()
55
+        {
56
+        //Given
57
+        PhoneBook book = new PhoneBook();
58
+        book.add("name1", "111");
59
+        book.add("name2", "408");
60
+
61
+        String name = "John";
62
+        String phoneNumber = "111-111-1111";
63
+
64
+        //book.lookup(name);
65
+        String actualOutput=book.lookup(name);
66
+
67
+        Assert.assertEquals(actualOutput ,phoneNumber );
68
+        }
69
+@Test
70
+public void testRemove()
71
+        {
72
+        PhoneBook  book= new PhoneBook();
73
+        String expectedname="Kayle";
74
+        String name="Kayle";
75
+
76
+
77
+        String actualOutput=book.hasRemove(name);
78
+        Assert.assertEquals(actualOutput,name);
79
+
80
+        }
81
+@Test
82
+public void testReserveLookup()
83
+        {
84
+        PhoneBook  book= new PhoneBook();
85
+        String name="John";
86
+        String phoneNumber = book.lookup(name);
87
+        String actualOutput = book.reverseLookup(phoneNumber);
88
+
89
+        Assert.assertEquals(name,actualOutput);
90
+        }
91
+
92
+@Test
93
+public void testListNamesandNumbers()
94
+        {
95
+        PhoneBook  book= new PhoneBook();
96
+        book.clearSortedMap();
97
+        book.add("Kayle", "408-111-2222");
98
+        book.add("John", "111-111-1111");
99
+        String expected = "John 111-111-1111\nKayle 408-111-2222\n";
100
+        String actualOutput= book.listNamesAndNumbers();
101
+        Assert.assertEquals(expected, actualOutput);
102
+        }
7 103
 }