Просмотр исходного кода

Still working on the test cases

Kibret Tecle 6 лет назад
Родитель
Сommit
a0a61f6779

+ 7
- 1
pom.xml Просмотреть файл

@@ -8,5 +8,11 @@
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
-
11
+    <dependencies>
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+        </dependency>
17
+    </dependencies>
12 18
 </project>

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

@@ -1,7 +1,72 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.List;
4
+import java.util.Map;
5
+import java.util.Set;
6
+import java.util.TreeMap;
7
+
3 8
 /**
4 9
  * Created by leon on 1/23/18.
5 10
  */
6 11
 public class PhoneBook {
12
+    Map<String,String> phoneBook = new TreeMap<String, String>();
13
+    Set<String>names;
14
+    Set<String>phoneBookList;
15
+    String nameLists="";
16
+    String namesAndNumbers ="";
17
+
18
+
19
+    public PhoneBook(Map<String, String> phoneBook) {
20
+        this.phoneBook = phoneBook;
21
+    }
22
+
23
+    public PhoneBook() {
24
+
25
+    }
26
+
27
+    public void add(String name,String phoneNumber) {
28
+
29
+     phoneBook.put(name, phoneNumber);
30
+    }
31
+
32
+    public void remove(String name){
33
+        if(phoneBook.containsKey(name)){
34
+            phoneBook.remove(name);
35
+        }
36
+        else{
37
+            System.out.println(name+" does not exist in the phone book list");
38
+        }
39
+
40
+    }
41
+
42
+        public String lookup(String name){
43
+
44
+        return phoneBook.get(name);
45
+        }
46
+
47
+        public String listNames(){
48
+        if(phoneBook.isEmpty()){
49
+            return null;
50
+        }else {
51
+            Set<String> names = phoneBook.keySet();
52
+
53
+            for (String name : names) {
54
+                nameLists += name + "\n";
55
+            }
56
+            return nameLists;
57
+        }
58
+
59
+        }
60
+
61
+        public String listNamesAndNumbers(){
62
+        Set<String>PhoneBookList = phoneBook.keySet();
63
+            for (String nameKeys:phoneBookList) {
64
+                namesAndNumbers+=nameKeys + "   "+phoneBook.get(nameKeys)+ "\n";
65
+            }
66
+
67
+        return namesAndNumbers;
68
+        }
69
+
70
+
71
+
7 72
 }

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

@@ -1,7 +1,48 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.After;
4
+import org.junit.Assert;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+import java.util.Map;
9
+import java.util.TreeMap;
10
+
3 11
 /**
4 12
  * Created by leon on 1/23/18.
5 13
  */
14
+
15
+
16
+
17
+
6 18
 public class PhoneBookTest {
19
+    private static PhoneBook phoneBookRecord;
20
+
21
+    @Before
22
+    public void setUp() {
23
+        //PhoneBook phoneBookRecord;
24
+        Map<String,String>initialRecord = new TreeMap<String, String>(){ {put("John","1234567890");put("Micheal","5678901234");
25
+            put("James","1234554321");}};
26
+        this.phoneBookRecord=new PhoneBook(initialRecord);
27
+
28
+    }
29
+
30
+    @After
31
+    public void tearDown() {
32
+    }
33
+
34
+    @Test
35
+    public void testLookup() {
36
+        String expected = "5678901234";
37
+        String actual = phoneBookRecord.lookup("Micheal");
38
+        Assert.assertEquals(expected, actual);
39
+    }
40
+    public void testlistNames(){
41
+        String expected = "John\n" +
42
+                            "Micheal\n"+
43
+                            "James\n";
44
+        String actual = phoneBookRecord.listNames();
45
+        Assert.assertEquals(expected, actual);
46
+
47
+    }
7 48
 }