Eric Cordell пре 6 година
родитељ
комит
8c375b5b86

+ 0
- 53
src/main/java/com/zipcodewilmington/phonebook/Person.java Прегледај датотеку

@@ -1,53 +0,0 @@
1
-package com.zipcodewilmington.phonebook;
2
-
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
5
-
6
-public class Person {
7
-
8
-    private String personName;
9
-    private ArrayList<String> phoneNumbers;
10
-
11
-    public Person(String personName){
12
-        this.personName = personName;
13
-        this.phoneNumbers = new ArrayList<>();
14
-        phoneNumbers
15
-    }
16
-
17
-
18
-    public String getPersonName() {
19
-        return personName;
20
-    }
21
-
22
-
23
-//public String addNumbers(){
24
-//        return null;
25
-//}
26
-
27
-
28
-   //find a way to print each item in the ArrayList in order
29
-   public String getPhoneNumbers() {
30
-       return phoneNumbers.toString();
31
-   }
32
-//       StringBuilder sb = new StringBuilder();
33
-//       for(int i = 0; i < phoneNumbers.size(); i++){
34
-//           if(i == phoneNumbers.size()){
35
-//               sb.append();
36
-//           }
37
-//       }
38
-//        return phoneNumbers;
39
-//    }
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-    }
50
-
51
-
52
-
53
-

+ 48
- 41
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Прегледај датотеку

@@ -2,70 +2,77 @@ package com.zipcodewilmington.phonebook;
2 2
 
3 3
 import java.util.*;
4 4
 
5
-/**
6
- * Created by leon on 1/23/18.
7
- */
8 5
 public class PhoneBook {
9 6
 
10
-    TreeMap<String, Person> phoneBook;
11
-
12
-public phoneBook(){
13
-    phoneBook = new TreeMap<String, Person>();
14
-    }
7
+    // created class variables
8
+    protected TreeMap<String, ArrayList<String>> treeMap;
15 9
 
16 10
 
17
-    public void addEntryToPhoneBook(String name, String phoneNumber) {
18
-       Person person = new Person(name, phoneNumber);
19
-        phoneBook.put(name, person);
11
+    // initiate instance variables in constructor
12
+    public PhoneBook() {
13
+        this.treeMap = new TreeMap<>();
20 14
     }
21 15
 
16
+    // .getKey =    name
17
+    // .getValue =  arraylist phone #'s
18
+    // .entrySet = efficient way of iterating over map
19
+    // .containsKey = used to return true if map contains
22 20
 
23
-    public void removeEntryFromPhoneBook(String name, String phoneNumber) {
24
-        phoneBook.remove(name, phoneNumber);
25
-    }
26
-
27
-    public String lookUp(String name) {
28
-        return phoneBook.get(name).getPhoneNumbers();
21
+    public void addEntryToPhoneBook(String name, ArrayList<String> phoneNumber) {
22
+        // checking name if it's in map
23
+        if (!treeMap.containsKey(name)) {
24
+            treeMap.put(name, phoneNumber); //not there, add info
25
+        }
29 26
     }
30 27
 
31
-    public String listNames() {
32
-        String completeListOfNames = "";
33
-        for (String key : phoneBook.keySet()) {
34
-            completeListOfNames += key + "\n";
35
-        }
36 28
 
37
-        return completeListOfNames;
29
+    public void removeEntryFromPhoneBook(String name) {
30
+        treeMap.remove(name);
38 31
     }
39 32
 
40
-    public String listNumbers() {
41
-        String completeListOfNumbers = "";
42
-        for (Map.Entry<String, Person> entry : phoneBook.entrySet()) {
43
-            completeListOfNumbers += entry.getValue() + "\n";
44
-        }
45
-        return completeListOfNumbers;
33
+    public ArrayList<String> lookUp(String name) {
34
+        return treeMap.get(name);
46 35
     }
47 36
 
48 37
 
49 38
     public String entryListAll() {
50
-        String fullList = "";
51
-        for (Map.Entry<String, Person> entry : phoneBook.entrySet()) {
52
-            fullList += entry.getKey() + " : " + entry.getValue() + "\n"; //reverse lookup
53 39
 
40
+        StringBuilder fullList = new StringBuilder();
41
+
42
+        for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
43
+            fullList.append(entry.getKey())
44
+                    .append(" ")
45
+                    .append(entry.getValue())
46
+                    .append("\n"); // appended on new lines for readability
54 47
         }
55 48
         System.out.println(fullList);
56
-        return fullList;
49
+        return fullList.toString();
50
+
51
+//        Noting originally had below syntax
52
+//        String fullList = "";
53
+//        for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) {
54
+//            fullList += entry.getKey() + " : " + entry.getValue() + "\n";
55
+//
56
+//        }
57
+//        System.out.println(fullList);
58
+//        return fullList;
57 59
     }
60
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
58 61
 
62
+    // want to look up number and return the persons name
59 63
     public String reverseLookup(String numberToLookUp) {
60
-        for (Map.Entry<String,Person > entry : phoneBook.entrySet()) {
61
-            if (entry.getValue().equals(numberToLookUp)){
62
-                return entry.getKey();
63
-            }
64
-        }return null;
65
-    }
66
-
67 64
 
65
+        String name = "";   //empty string
68 66
 
67
+        for (Map.Entry<String, ArrayList<String>> entry : treeMap.entrySet()) { //Looping entire TreeMap
69 68
 
69
+            for (String numberWeWant : entry.getValue()) { //searching #'s in TreeMap value
70
+                if (numberToLookUp.contains(numberWeWant)) {
71
+                    name = entry.getKey();  // getKey reps names in TreeMap
72
+                }
73
+            }
74
+        }
75
+        System.out.println(name);
76
+        return name;
77
+    }
70 78
 }
71
-

+ 0
- 67
src/test/java/com/zipcodewilmington/phonebook/PersonTest.java Прегледај датотеку

@@ -1,67 +0,0 @@
1
-package com.zipcodewilmington.phonebook;
2
-
3
-import org.junit.Assert;
4
-import org.junit.Before;
5
-import org.junit.Test;
6
-
7
-import java.util.ArrayList;
8
-import java.util.Arrays;
9
-
10
-import static org.junit.Assert.*;
11
-
12
-public class PersonTest {
13
-
14
-
15
-    @Test
16
-    public void getPersonNameTest() {
17
-        //Given
18
-        Person human = new Person("Eric", "302-999-1234");
19
-
20
-        //When
21
-        String expectedName = "Eric";
22
-        String expectedNumber = "302-999-1234";
23
-        String actual = human.getPersonName() + human.getPhoneNumbers();
24
-
25
-
26
-        Assert.assertTrue(actual.contains(expectedName) && actual.contains(expectedNumber));
27
-    }
28
-
29
-//@Test
30
-//    public void addNumbersTest() {
31
-//        //Given
32
-//    Person human = new Person("Eric", "302-999-1234");
33
-//
34
-//    //When
35
-//    human.addNumbers("302-222-9876");
36
-//    String expectedNumber = "302-222-1234";
37
-//    String actual = human.getPhoneNumbers();
38
-//
39
-//    Assert.assertTrue(actual.contains(expectedNumber));
40
-//}
41
-
42
-@Test
43
-    public void printNameTest(){
44
-        //Given
45
-    Person human = new Person("Eric", "302-999-1234");
46
-
47
-    //When
48
-    String expectedName = "Eric";
49
-    String actual = human.getPersonName();
50
-
51
-    Assert.assertTrue(actual.contains(expectedName));
52
-}
53
-
54
-//@Test
55
-//    public void printNumbersTest(){
56
-//        //Given
57
-//    Person human = new Person("Eric", "302-999-1234");
58
-//    human.addNumbers("302-222-9876");
59
-//
60
-//    //When
61
-//    String expectedNumbers = "";
62
-//
63
-//}
64
-
65
-
66
-}
67
-

+ 37
- 30
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Прегледај датотеку

@@ -6,66 +6,68 @@ import org.junit.Before;
6 6
 import org.junit.Test;
7 7
 
8 8
 import java.util.ArrayList;
9
+import java.util.Arrays;
10
+import java.util.Collections;
11
+import java.util.Set;
9 12
 
10 13
 /**
11 14
  * Created by leon on 1/23/18.
12 15
  */
13 16
 public class PhoneBookTest {
14
-    PhoneBook phoneBook;
17
+    PhoneBook phoneBook = new PhoneBook();
15 18
 
16 19
     @Before
17 20
     public void setUp() {
18
-        phoneBook = new PhoneBook();
19
-        phoneBook.addEntryToPhoneBook("Eric", "302-123-4567");
20
-        phoneBook.addEntryToPhoneBook("Bob", "302-999-9999");
21
+        phoneBook = new PhoneBook();  // automatically goes in each test
22
+//        phoneBook.addEntryToPhoneBook("Eric", "302-555-1111");
23
+//        phoneBook.addEntryToPhoneBook("Bob", "302-999-9999");
21 24
     }
22 25
 
23 26
 
24 27
     @Test
25 28
     public void testAddPhoneBookEntryToPhoneBook() {
26
-        String phoneNumber = "302-555-1111";
27
-        phoneBook.addEntryToPhoneBook("eric", phoneNumber);
28
-        String actual = phoneBook.lookUp("eric");
29
-        Assert.assertEquals(phoneNumber, actual);
29
+
30
+        ArrayList<String> addition = new ArrayList<>(Arrays.asList("302-555-1111", "302-222-2222"));
31
+
32
+        phoneBook.addEntryToPhoneBook("Eric", addition);
33
+
34
+        ArrayList<String> actual = new ArrayList<String>(phoneBook.lookUp("Eric"));
35
+        Assert.assertEquals(addition, actual);
30 36
     }
31 37
 
32 38
     @Test
33 39
     public void testRemoveEntryFromPhoneBook() {
34
-        String phoneNumber = "302-555-1111";
35
-        String name = "eric";
36
-        phoneBook.addEntryToPhoneBook(name, phoneNumber);
37
-        phoneBook.removeEntryFromPhoneBook("eric", phoneNumber);
40
+        ArrayList<String> ericsNumbers = new ArrayList<>(Arrays.asList("302-555-1111", "302-222-2222"));
38 41
 
39
-        String actual = phoneBook.lookUp("eric");
40
-        Assert.assertEquals(null, actual);
41
-    }
42
+        phoneBook.addEntryToPhoneBook("Eric", ericsNumbers);
42 43
 
43
-    @Test
44
-    public void testListNames() {
45
-        String expected = "Bob\n" +
46
-                "Eric\n";
47
-        String actual = phoneBook.listNames();
44
+        String expected = "";
45
+        phoneBook.removeEntryFromPhoneBook("Eric");
46
+
47
+        String actual = phoneBook.entryListAll();
48 48
 
49 49
         Assert.assertEquals(expected, actual);
50 50
     }
51 51
 
52 52
     @Test
53
-    public void testListNumber() {
53
+    public void lookUpTest() {
54
+        ArrayList<String> addition = new ArrayList<>(Arrays.asList("302-555-1111", "302-222-2222"));
54 55
 
56
+        phoneBook.addEntryToPhoneBook("Eric", addition);
55 57
 
56
-        String expected = "302-999-9999\n" +
57
-                "302-123-4567\n";
58
-        String actual = phoneBook.listNumbers();
59
-
60
-        Assert.assertEquals(expected, actual);
58
+        ArrayList<String> actual = new ArrayList<String>(phoneBook.lookUp("Eric"));
59
+        Assert.assertEquals(addition, actual);
61 60
     }
62 61
 
62
+
63 63
     @Test
64 64
     public void testEntryListAll() {
65 65
 
66
+        ArrayList<String> ericsNumbers = new ArrayList<>(Arrays.asList("302-111-1111", "302-222-2222", "302-333-3333"));
67
+        phoneBook.addEntryToPhoneBook("Eric :", ericsNumbers);
68
+
69
+        String expected = "Eric : " + ericsNumbers + "\n";
66 70
 
67
-        String expected = "Bob : 302-999-9999\n" +
68
-                "Eric : 302-123-4567\n";
69 71
         String actual = phoneBook.entryListAll();
70 72
 
71 73
         Assert.assertEquals(expected, actual);
@@ -75,9 +77,14 @@ public class PhoneBookTest {
75 77
 
76 78
     @Test
77 79
     public void testReverseLookup() {
78
-        String expected = "Bob";
80
+        ArrayList<String> ericsNumbers = new ArrayList<>(Arrays.asList("302-555-1111", "302-222-2222"));
81
+
82
+        String expected = "Eric";
83
+
84
+        phoneBook.addEntryToPhoneBook("Eric", ericsNumbers);
85
+
86
+        String actual = phoneBook.reverseLookup("302-555-1111");
79 87
 
80
-        String actual = phoneBook.reverseLookup("302-999-9999");
81 88
         Assert.assertEquals(expected,actual);
82 89
     }
83 90