Browse Source

added get/add entry

Rachelle 6 years ago
parent
commit
25d39f2e68
3 changed files with 54 additions and 35 deletions
  1. 23
    0
      Entry.java
  2. 20
    1
      PhoneBook.java
  3. 11
    34
      PhoneBookTest.java

+ 23
- 0
Entry.java View File

@@ -0,0 +1,23 @@
1
+import java.util.ArrayList;
2
+import java.util.List;
3
+public class Entry
4
+{
5
+    String name;
6
+    String phoneNumber;
7
+    List<String> entryArray = new ArrayList <String>();
8
+
9
+    public Entry(String phoneNumber){
10
+        entryArray.add(0,phoneNumber);
11
+    }
12
+
13
+    public String getPhoneNumber(){
14
+        StringBuilder phoneNumber = new StringBuilder();
15
+        for(int i  = 0; i < entryArray.size(); i++){
16
+            phoneNumber.append(entryArray.get(i));
17
+            if(entryArray.size() > 1){
18
+                phoneNumber.append(", ");
19
+            }
20
+        }
21
+        return phoneNumber.toString();
22
+    }
23
+} 

+ 20
- 1
PhoneBook.java View File

@@ -1,7 +1,26 @@
1
- 
1
+ import java.util.TreeMap;
2
+ import java.util.Map;
3
+ import java.util.ArrayList;
2 4
 
3 5
 /**
4 6
  * Created by leon on 1/23/18.
5 7
  */
6 8
 public class PhoneBook {
9
+    Map<String,Entry> phonebook = new TreeMap<String,Entry>();
10
+    public PhoneBook(){       
11
+        
12
+    }
13
+    
14
+    public void addEntry(String name, String phoneNumber){
15
+        Entry newEntry = new Entry(phoneNumber);
16
+        phonebook.put(name,newEntry);        
17
+    }
18
+    
19
+    public Entry getEntry(String name){
20
+        return phonebook.get(name);
21
+    }
22
+    
23
+    
24
+    
25
+    
7 26
 }

+ 11
- 34
PhoneBookTest.java View File

@@ -1,42 +1,19 @@
1
-
2
-
3 1
 import static org.junit.Assert.*;
4
-import org.junit.After;
5
-import org.junit.Before;
6 2
 import org.junit.Test;
3
+import java.util.ArrayList;
7 4
 
8
-/**
9
- * The test class PhoneBookTest.
10
- *
11
- * @author  (your name)
12
- * @version (a version number or a date)
13
- */
14 5
 public class PhoneBookTest
15
-{
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
20
-    {
21
-    }
22
-
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
28
-    @Before
29
-    public void setUp()
30
-    {
6
+{   
7
+    @Test 
8
+    public void addEntryTest(){
9
+        PhoneBook newEntry = new PhoneBook();
10
+        newEntry.addEntry("Rachelle","123-4567");
11
+        Entry actual = newEntry.getEntry("Rachelle");
12
+        assertEquals("123-4567",actual.getPhoneNumber());
31 13
     }
32 14
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
15
+    @Test
16
+    public void hasEntryTest(){
17
+        
41 18
     }
42 19
 }