CHU1TA26 6 лет назад
Родитель
Сommit
0c30d37e4b

+ 82
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Просмотреть файл

@@ -1,7 +1,89 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Map;
5
+import java.util.TreeMap;
6
+
3 7
 /**
4 8
  * Created by leon on 1/23/18.
5 9
  */
6 10
 public class PhoneBook {
11
+    public static void main (String[] args){
12
+
13
+    }
14
+
15
+
16
+    private Map<String, ArrayList<String>> directory;
17
+
18
+    public PhoneBook (){
19
+        directory= new TreeMap< String, ArrayList<String>>();
20
+
21
+    }
22
+    public  void setValues(String name, ArrayList<String>phonenumber){
23
+        this.directory.put(name, phonenumber);
24
+    }
25
+
26
+
27
+    public boolean hasEntry(String name) {
28
+
29
+return directory.containsKey(name);
30
+
31
+}
32
+
33
+    public void add(String name, String number) {
34
+        ArrayList<String> contact = new ArrayList<>();
35
+        contact.add(number);
36
+        directory.put(name,contact);
37
+
38
+    }
39
+
40
+
41
+
42
+    public ArrayList<String> getContact(String name) {
43
+         return directory.get(name);
44
+     }
45
+     public String reverseLookUp(String number){
46
+        String repply= "Not here";
47
+        for (String name:directory.keySet()){
48
+            ArrayList<String> reverse= directory.get(name);
49
+            for (String looking: reverse){
50
+                if (looking.equals(number)){
51
+                     repply= name;
52
+                }
53
+            }
54
+        }
55
+        return repply;
56
+     }
57
+
58
+     public  void  remove (String name){
59
+        if (directory.containsKey(name)){
60
+            directory.remove(name);
61
+        }
62
+     }
63
+
64
+    public String listNameAndNumbers() {
65
+        StringBuilder builder = new StringBuilder();
66
+        for (String name:directory.keySet()){
67
+            for (int i= 0; i< directory.get(name).size(); i++){
68
+                builder.append(name + " " + directory.get(name).get(i) + "\n");
69
+            }
70
+        }
71
+        return builder.toString();
72
+    }
73
+
74
+
75
+    public Map<String, ArrayList<String>> getDirectory() {
76
+        return directory;
77
+    }
78
+    public Map<String, ArrayList<String>> getContact() {
79
+        return directory;
80
+    }
81
+
82
+
83
+    public void setDirectory(Map<String, ArrayList<String>> directory) {
84
+        this.directory = directory;
85
+    }
86
+
87
+
88
+
7 89
 }

+ 99
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Просмотреть файл

@@ -1,7 +1,106 @@
1 1
 package com.zipcodewilmington.phonebook;
2
+import org.junit.Assert;
3
+import org.junit.Test;
4
+
5
+import java.util.ArrayList;
2 6
 
3 7
 /**
4 8
  * Created by leon on 1/23/18.
5 9
  */
6 10
 public class PhoneBookTest {
11
+
12
+    @Test
13
+     public void hasEntrytest  ()  {
14
+        PhoneBook info = new PhoneBook();
15
+        String name = "Nuridalia";
16
+        String number= "267-363-8738";
17
+        info.add(name, number);
18
+
19
+        //when
20
+
21
+    boolean actualOutput= info.hasEntry(name);
22
+
23
+
24
+        Assert.assertTrue(actualOutput);
25
+
26
+
27
+    }
28
+
29
+
30
+    @Test
31
+    public void addEntrytest  ()  {
32
+        PhoneBook info = new PhoneBook();
33
+        String name = "Nuridalia";
34
+        String number= "267-363-8738";
35
+        info.add(name, number);
36
+
37
+        //when
38
+
39
+        Assert.assertEquals(1, info.getContact().size());
40
+
41
+
42
+    }
43
+    @Test
44
+    public void getContactTest(){
45
+        PhoneBook info = new PhoneBook();
46
+        String name= "Nuridalia";
47
+        String number= "267-777-9999";
48
+        info.add(name , number);
49
+
50
+        String expected= number;
51
+        ArrayList<String> actual= info.getContact(number);
52
+    }
53
+
54
+
55
+
56
+    @Test
57
+    public  void removeTest (){
58
+
59
+        PhoneBook info = new PhoneBook();
60
+        String name = "Nuridalia";
61
+        String number = "267-777-9999";
62
+        info.add(name, number);
63
+        info.remove(name);
64
+
65
+        boolean actual = info.hasEntry(name);
66
+        boolean expected = true;
67
+        Assert.assertFalse(actual);
68
+
69
+
70
+    }
71
+
72
+
73
+    @Test
74
+    public void  reverseLookupTest(){
75
+        PhoneBook info =new PhoneBook();
76
+        String name= "Evangelina";
77
+        String number = "267-999-8888";
78
+        info.add(name, number);
79
+
80
+        String actual= info.reverseLookUp(number);
81
+        String expected = name;
82
+
83
+
84
+        Assert.assertEquals(expected, actual);
85
+
86
+
87
+    }
88
+
89
+
90
+    @Test
91
+    public  void listNamesAndNumbers(){
92
+        PhoneBook info = new PhoneBook ();
93
+        info.add("Eva", "999-999-999");
94
+        info.add("nury", "888-999-8787");
95
+        info. add ("Dayhani", "888-777-9090");
96
+
97
+        String actual= info.listNameAndNumbers();
98
+        String expected= "Dayhani 888-777-9090\nEva 999-999-999\nnury 888-999-8787\n";
99
+
100
+        Assert.assertEquals(expected,actual);
101
+
102
+
103
+
104
+
105
+    }
7 106
 }