Quellcode durchsuchen

Merge fdd899640afa2f4ad58047053ae7d0d19ec197ef into 74a9673e36c7d25c28315cb54247a91792f8fbc0

April Rivera vor 6 Jahren
Ursprung
Commit
46d5cb68a6
Es ist kein Account mit dieser Commiter-Email verbunden

+ 12
- 0
pom.xml Datei anzeigen

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+        </dependency>
16
+        <dependency>
17
+            <groupId>junit</groupId>
18
+            <artifactId>junit</artifactId>
19
+            <version>RELEASE</version>
20
+        </dependency>
21
+    </dependencies>
10 22
 
11 23
 
12 24
 </project>

+ 54
- 0
src/main/java/com/zipcodewilmington/phonebook/Contact.java Datei anzeigen

@@ -0,0 +1,54 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class Contact {
6
+
7
+
8
+    private String name;
9
+    private ArrayList<String> phoneNumbers;
10
+
11
+
12
+    public Contact(){
13
+        this.phoneNumbers = new ArrayList<>();
14
+    }
15
+
16
+
17
+    public Contact(String name, String phoneNumbers){
18
+        this.name = name;
19
+        this.phoneNumbers = new ArrayList<>();
20
+        addNumber(phoneNumbers);
21
+    }
22
+
23
+    public void addNumber(String number) {
24
+        getPhoneNumbers().add(number);
25
+    }
26
+
27
+    public void removeNumber(String number) {
28
+        getPhoneNumbers().remove(number);
29
+    }
30
+
31
+    public String getName() {
32
+        return name;
33
+    }
34
+
35
+    public void setName(String name) {
36
+        this.name = name;
37
+    }
38
+
39
+    public ArrayList<String> getPhoneNumbers() {
40
+        return phoneNumbers;
41
+    }
42
+    @Override
43
+    public String toString(){
44
+        return getName() + " " + getListOfPhoneNumbers();
45
+    }
46
+    public String getListOfPhoneNumbers(){
47
+        String listOfNumbers = "";
48
+        for(String number : getPhoneNumbers()){
49
+            listOfNumbers += number + " ";
50
+        }
51
+        return listOfNumbers.trim();
52
+    }
53
+
54
+}

+ 64
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Datei anzeigen

@@ -1,7 +1,71 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.util.*;
4
+import java.util.TreeMap;
5
+
6
+
3 7
 /**
4 8
  * Created by leon on 1/23/18.
5 9
  */
6 10
 public class PhoneBook {
11
+
12
+    private TreeMap<String, Contact> phoneBook;
13
+
14
+
15
+    public PhoneBook() {
16
+        this.phoneBook = new TreeMap<>(String::compareToIgnoreCase);
17
+    }
18
+    public void addNewEntry(String name, String number){
19
+        getPhoneBook().put(name, new Contact(name, number));
20
+    }
21
+
22
+    public void removeEntry(String name){
23
+        getPhoneBook().remove(name);
24
+    }
25
+    public void removeNumber(String name, String number){
26
+        getPhoneBook().get(name).getPhoneNumbers().remove(number);
27
+    }
28
+
29
+    public ArrayList<String> lookUpContactPhoneNumbers(String name){
30
+        return getPhoneBook().get(name).getPhoneNumbers();
31
+    }
32
+
33
+    public void listAllNamesAndNumbers() {
34
+        System.out.println(toString());
35
+
36
+    }
37
+    public String reverseLookUp(String number) {
38
+        for (Map.Entry<String, Contact> contactEntry : getPhoneBook().entrySet()) {
39
+            if (contactEntry.getValue().getPhoneNumbers().contains(number)) {
40
+                return contactEntry.getKey();
41
+            }
42
+
43
+        }
44
+        return "Contact not found";
45
+    }
46
+
47
+    @Override
48
+    public String toString(){
49
+        String toBeReturned = "";
50
+        for(Map.Entry<String, Contact> contactEntry : getPhoneBook().entrySet()){
51
+           toBeReturned += contactEntry.getValue().toString();
52
+           toBeReturned += " ";
53
+        }
54
+        return toBeReturned.trim();
55
+    }
56
+    public TreeMap<String, Contact> getPhoneBook(){
57
+        return phoneBook;
7 58
 }
59
+
60
+        }
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+

+ 66
- 0
src/test/java/com/zipcodewilmington/phonebook/ContactTest.java Datei anzeigen

@@ -0,0 +1,66 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import static junit.framework.TestCase.assertTrue;
8
+import static org.junit.Assert.assertEquals;
9
+
10
+public class ContactTest {
11
+
12
+    Contact contact;
13
+
14
+    @Before
15
+    public void setUp(){
16
+        contact = new Contact();
17
+
18
+    }
19
+    @Test
20
+    public void addNumberTest(){
21
+        //Given
22
+        String number = "872-345-2298";
23
+        contact.addNumber(number);
24
+        //When
25
+        boolean numberHasBeenAdded = contact.getPhoneNumbers().contains(number);
26
+        //Then
27
+        assertTrue(numberHasBeenAdded);
28
+
29
+    }
30
+    @Test
31
+    public void removeNumberTest(){
32
+        //Given
33
+        String number = "872-345-2298";
34
+        contact.addNumber(number);
35
+        //When
36
+        contact.removeNumber(number);
37
+        boolean numberHasBeenRemoved = !contact.getPhoneNumbers().contains(number);
38
+        //Then
39
+        assertTrue(numberHasBeenRemoved);
40
+    }
41
+    @Test
42
+    public void toStringTest(){
43
+        //Given
44
+        String newName = "April";
45
+        String newNumber = "7584339876";
46
+        Contact contactUnderTest = new Contact(newName, newNumber);
47
+
48
+        String expectedString = "April 7584339876";
49
+        String actualString = contactUnderTest.toString();
50
+        assertEquals(expectedString, actualString);
51
+
52
+    }
53
+    @Test
54
+    public void getListOfPhoneNumbersTest(){
55
+        String newName = "April";
56
+        String newNumber = "7584339876";
57
+        Contact contactUnderTest = new Contact(newName, newNumber);
58
+        contactUnderTest.addNumber("3457765489");
59
+        String expectedListOfNumbers = "7584339876 3457765489";
60
+        String actualListOfNumbers = contactUnderTest.getListOfPhoneNumbers();
61
+        assertEquals(expectedListOfNumbers, actualListOfNumbers);
62
+
63
+    }
64
+
65
+
66
+}

+ 82
- 1
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Datei anzeigen

@@ -1,7 +1,88 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.ArrayList;
8
+
9
+import static junit.framework.TestCase.assertTrue;
10
+import static org.junit.Assert.assertEquals;
11
+
12
+
3 13
 /**
4 14
  * Created by leon on 1/23/18.
5 15
  */
6
-public class PhoneBookTest {
16
+ public class PhoneBookTest {
17
+
18
+    PhoneBook phoneBook;
19
+
20
+    @Before
21
+    public void setUp() {
22
+        phoneBook = new PhoneBook();
23
+    }
24
+
25
+    @Test
26
+    public void addNewEntryTest(){
27
+        String name = "April";
28
+        String number = "6547783321";
29
+        phoneBook.addNewEntry(name, number);
30
+        boolean entryHasBeenCreated = phoneBook.getPhoneBook().containsKey(name);
31
+        assertTrue(entryHasBeenCreated);
32
+    }
33
+    @Test
34
+    public void removeEntryTest(){
35
+        String name = "April";
36
+        String number = "6547783321";
37
+        phoneBook.addNewEntry(name, number);
38
+        phoneBook.removeEntry(name);
39
+        boolean entryHasBeenRemoved = !phoneBook.getPhoneBook().containsKey(name);
40
+        assertTrue(entryHasBeenRemoved);
41
+    }
42
+    @Test
43
+    public void removeNumberTest(){
44
+        String name = "April";
45
+        String number = "6547783321";
46
+        phoneBook.addNewEntry(name, number);
47
+        phoneBook.removeNumber(name, number);
48
+        boolean numberHasBeenRemovedFromContact = !phoneBook.getPhoneBook().get(name).getPhoneNumbers().contains(number);
49
+        assertTrue(numberHasBeenRemovedFromContact);
50
+    }
51
+    @Test
52
+    public void lookUpContactPhoneNumbersTest(){
53
+        String name = "April";
54
+        String number = "6547783321";
55
+        phoneBook.addNewEntry(name, number);
56
+        ArrayList<String> actualPhoneList = phoneBook.lookUpContactPhoneNumbers(name);
57
+        ArrayList<String> expectedPhoneList = new ArrayList<>();
58
+        expectedPhoneList.add(number);
59
+        assertEquals(expectedPhoneList, actualPhoneList);
60
+    }
61
+    @Test
62
+    public void listAllNamesAndNumbersTest(){
63
+        String name = "April";
64
+        String number = "6547783321";
65
+        String secondName = "Genya";
66
+        String secondNumber = "8753349987";
67
+        phoneBook.addNewEntry(name, number);
68
+        phoneBook.addNewEntry(secondName, secondNumber);
69
+        String actual = phoneBook.toString();
70
+        String expected = "April 6547783321 Genya 8753349987";
71
+        assertEquals(expected, actual);
72
+        //Print all the things
73
+        phoneBook.listAllNamesAndNumbers();
74
+    }
75
+    @Test
76
+    public void reverseLookUpTest(){
77
+        String name = "April";
78
+        String number = "6547783321";
79
+        String secondName = "Genya";
80
+        String secondNumber = "8753349987";
81
+        phoneBook.addNewEntry(name, number);
82
+        phoneBook.addNewEntry(secondName, secondNumber);
83
+        String actual = phoneBook.reverseLookUp(number);
84
+        String expected = "April";
85
+        assertEquals(expected, actual);
86
+    }
87
+
7 88
 }