Bladeren bron

Merge 5735e5c4c7c3e028aa50b14f31e914ccd66fb5e1 into 74a9673e36c7d25c28315cb54247a91792f8fbc0

amygdal 6 jaren geleden
bovenliggende
commit
9ee8ccce47
Geen account gekoppeld aan de committers e-mail

+ 10
- 0
pom.xml Bestand weergeven

@@ -7,6 +7,16 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>phonebok</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+            <scope>test</scope>
17
+        </dependency>
18
+
19
+    </dependencies>
10 20
 
11 21
 
12 22
 </project>

+ 43
- 0
src/main/java/com/zipcodewilmington/phonebook/Person.java Bestand weergeven

@@ -0,0 +1,43 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+
4
+import java.util.ArrayList;
5
+
6
+public class Person {
7
+
8
+    //variables
9
+
10
+    /**
11
+     * we made these private bc we don't want the user to be able to change the data. Another reason we want to do this
12
+     * is that you want to "hide" how you store the information from the users.
13
+     */
14
+
15
+
16
+    private String name;
17
+    private ArrayList<String> phoneNumber;
18
+
19
+    public Person (String entryName, String entryNumber) {
20
+        this.name = entryName;
21
+        this.phoneNumber = new ArrayList<String>();
22
+        this.phoneNumber.add(entryNumber);
23
+
24
+    }
25
+
26
+    public String getNumbers(){
27
+
28
+        return phoneNumber.toString();
29
+    }
30
+
31
+    public void removeSingleNumber (String number){
32
+
33
+        phoneNumber.remove(number);
34
+    }
35
+
36
+    public void addAdditionalNumber(String number){
37
+
38
+        phoneNumber.add(number);
39
+    }
40
+}
41
+
42
+
43
+

+ 92
- 4
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Bestand weergeven

@@ -1,7 +1,95 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
3
+import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
4
+
5
+import java.util.ArrayList;
6
+import java.util.Map;
7
+import java.util.Set;
8
+import java.util.TreeMap;
9
+
10
+//phoneBook class is like the cover of the book.
6 11
 public class PhoneBook {
7
-}
12
+
13
+    public static void main(String[] args) {
14
+
15
+        PhoneBook myPhoneBookActualObjectInstance= new PhoneBook();
16
+        //adding two people:
17
+        myPhoneBookActualObjectInstance.add("vince", "1123456789");
18
+        myPhoneBookActualObjectInstance.add("amy", "29387429");
19
+        //asking the program to get a person using it's name and adding an additional number to it:
20
+        myPhoneBookActualObjectInstance.addAdditionalNumberToEntry("amy", "2344");
21
+        //printing out the entire phonebook
22
+        System.out.println(myPhoneBookActualObjectInstance.displayEntirePhoneBookContents());
23
+    }
24
+
25
+
26
+    /*the treeMap is like the t structure with key/values. key is person's name in this example, value is the arraylist
27
+    which can hold multiple numbers, fields etc.
28
+    */
29
+    TreeMap<String, Person> personTreeMap = new TreeMap<String, Person>();
30
+
31
+    public PhoneBook() {
32
+
33
+    }
34
+
35
+    public void add(String name, String number) {
36
+        Person phoneBookEntry = new Person(name, number);
37
+        personTreeMap.put(name, phoneBookEntry);
38
+    }
39
+
40
+
41
+    public void removeEntireEntry (String name) {
42
+
43
+        personTreeMap.remove(name);
44
+    }
45
+
46
+    //newly added thanks to "the falcon"
47
+    public void addAdditionalNumberToEntry (String name, String number){
48
+        personTreeMap.get(name).addAdditionalNumber(number);
49
+
50
+    }
51
+
52
+    public String lookup(String name) {
53
+
54
+        return personTreeMap.get(name).getNumbers();
55
+
56
+    }
57
+
58
+    public String reverseLookup(String number) {
59
+
60
+        for (Map.Entry<String, Person> phoneBookEntry : personTreeMap.entrySet()){
61
+            if (phoneBookEntry.getValue().getNumbers().contains(number)){
62
+                String foundName = phoneBookEntry.getKey();
63
+                return foundName;
64
+            }
65
+        }
66
+
67
+        return "We're sorry. We could not find that number. Have a nice day.";
68
+
69
+    }
70
+
71
+    public String showNamesOnly() {
72
+
73
+        String phoneListOfNamesOnly= "";
74
+        Set<String> nameTreeKeys = personTreeMap.keySet();
75
+        for (String personsName : nameTreeKeys) {
76
+            phoneListOfNamesOnly += (personsName + "\n");
77
+            }
78
+
79
+        return phoneListOfNamesOnly;
80
+
81
+        }
82
+
83
+    public String displayEntirePhoneBookContents() {
84
+
85
+        String entirePhoneBook = "";
86
+        Set<String> nameTreeKeys = personTreeMap.keySet();
87
+        for (String personsName : nameTreeKeys) {
88
+             entirePhoneBook += (personsName + "'s phone number(s): " +  personTreeMap.get(personsName).getNumbers() + "\n");
89
+        }
90
+
91
+        return entirePhoneBook;
92
+
93
+        }
94
+
95
+    }

+ 96
- 0
src/main/java/com/zipcodewilmington/phonebook/oldPhonebookClass.java Bestand weergeven

@@ -0,0 +1,96 @@
1
+
2
+/*
3
+package com.zipcodewilmington.phonebook;
4
+
5
+        import java.util.ArrayList;
6
+        import java.util.Map;
7
+        import java.util.Set;
8
+        import java.util.TreeMap;
9
+
10
+public class oldPhonebookClass {
11
+
12
+    //the below defines what kind of attribute you have, which in this case is your "contacts"
13
+    TreeMap<String, String> personMap;
14
+
15
+    String name = "";
16
+
17
+    //this is your constructor. whenever you see public + the classname, that is your constructor
18
+    //
19
+    // (this phoneBook constructor is like the "cover" of your phonebook (the binding and the spine). it creates an empty book.)
20
+
21
+    //this can make as many phonebook objects as you want
22
+    public PhoneBook() {
23
+
24
+        //when we make our phonebooks, this is the form they will take (below)
25
+        //
26
+        // this treeMap initializes the "blank pages of the book". the treemap is the structure of how your book will hold info
27
+
28
+        //if you were to not set this, your person map would be null(default setting)
29
+        this.personMap = new TreeMap<String, String>();
30
+    }
31
+
32
+    // this is our method below. it describes what we can do with this phonebook
33
+
34
+    //so later, when you call "phonebook.add", that's like someone literally writing a new name and number in your phonebook object
35
+
36
+
37
+    public void add(String name, String number) {
38
+        Person JohnDoe = new Person(name, number);
39
+        personMap.put(name, number);
40
+    }
41
+
42
+
43
+
44
+    /*
45
+    //this method would really be better for if you need more information on your person objects (like address, etc).
46
+
47
+    public void add(Person person){
48
+        this.personMap.put(person.getName(), person);
49
+    }
50
+
51
+
52
+
53
+    public void remove(String name, String number) {
54
+
55
+        personMap.remove(name);
56
+    }
57
+
58
+    public String lookup(String name) {
59
+
60
+        return personMap.get(name);
61
+
62
+    }
63
+
64
+   /* public String reverseLookup (String phoneNumber){
65
+
66
+        //this will "grab" all the listings in your book. it does NOT print them.
67
+        Set<Map.Entry<String, String> = personMap.entrySet();
68
+
69
+        for (Map.Entry<String, String> phoneBookEntry : entries) {
70
+
71
+            Person person = phoneBookEntry.getValue();
72
+
73
+            if (person.getPhoneNumber().equals(phoneNumber)){
74
+                return phoneBookEntry.getKey();
75
+            }
76
+        }
77
+
78
+       return "We're sorry. The requested phone number could not be found. Have fun when you code!!!!!!!!";
79
+
80
+    }
81
+
82
+
83
+
84
+    public void displayAllEntries() {
85
+
86
+        for (Map.Entry<String, String> entry : personMap.entrySet()) {
87
+            String name = entry.getKey();
88
+            String number = entry.getValue();
89
+        }
90
+
91
+    }
92
+
93
+
94
+}
95
+
96
+*/

+ 20
- 0
src/test/java/com/zipcodewilmington/phonebook/PersonTest.java Bestand weergeven

@@ -0,0 +1,20 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class PersonTest {
8
+
9
+    @Test
10
+    public void getNumbers() {
11
+    }
12
+
13
+    @Test
14
+    public void removeSingleNumber() {
15
+    }
16
+
17
+    @Test
18
+    public void addAdditionalNumber() {
19
+    }
20
+}

+ 186
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Bestand weergeven

@@ -1,7 +1,193 @@
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.Map;
8
+import java.util.TreeMap;
9
+
3 10
 /**
4 11
  * Created by leon on 1/23/18.
5 12
  */
6 13
 public class PhoneBookTest {
14
+
15
+//Start off here. google "how to write a test for a map"
16
+//    PhoneBook testPhonebook = new PhoneBook(); <=why didn't this work?
17
+    PhoneBook testPhonebook;
18
+
19
+    @Before
20
+    public void setup() {
21
+            testPhonebook = new PhoneBook();
22
+    }
23
+
24
+    @Test
25
+    public void addEntryTestCase1(){
26
+        //Given
27
+            //a person object
28
+            // a name and a number object
29
+        new Person("Addison", "1234");
30
+
31
+
32
+        //When
33
+            //we add the person to the phonebook
34
+        testPhonebook.add("Addison", "1234");
35
+        String testNumber = testPhonebook.lookup("Addison");
36
+
37
+        //Then
38
+            //we expect to see that person object with assigned name and number
39
+
40
+        Assert.assertTrue(testNumber.equals("[1234]"));
41
+    }
42
+
43
+    @Test
44
+    public void addEntryTestCase2(){
45
+        new Person("AdDy", "123.4");
46
+        testPhonebook.add("AdDy", "123.4");
47
+        String testNumber = testPhonebook.lookup("AdDy");
48
+        Assert.assertTrue(testNumber.equals("[123.4]"));
49
+    }
50
+
51
+//    @Test
52
+//    public void removeEntireEntryTest1(){
53
+//        testPhonebook.add("Allessandra", "4321");
54
+//        testPhonebook.add("Essarella", "4421");
55
+//        testPhonebook.removeEntireEntry("Allessandra");
56
+//        String testNumber = testPhonebook.lookup("Essarella");
57
+//        Assert.assertNotEquals(null, testNumber);
58
+//    }
59
+
60
+    @Test
61
+    public void removeEntireEntryTest2(){
62
+        //Given
63
+            //2 person objects
64
+            // 2 names and 2 phone numbers
65
+
66
+        testPhonebook.add("Andrea", "8888");
67
+        testPhonebook.add("Alexei", "8877");
68
+
69
+        //When
70
+            //a user/dev removes one entry from the phonebook
71
+        testPhonebook.removeEntireEntry("Alexei");
72
+        String testList = testPhonebook.showNamesOnly();
73
+
74
+        //Then
75
+            //we expect to see that entry gone. we expect only to see all the other entries.
76
+        Assert.assertTrue(testList.equals("Andrea\n"));
77
+
78
+    }
79
+
80
+        //newly added thanks to "the falcon"
81
+    @Test
82
+    public void addAddtionalNumberToEntryTest() {
83
+        //Given
84
+            //A name and a number
85
+        testPhonebook.add("test", "123");
86
+
87
+
88
+        //When
89
+            //we add additional number
90
+        testPhonebook.addAdditionalNumberToEntry("test", "200");
91
+
92
+
93
+        //Then
94
+            //we expect expect to see that additional number added to the entry
95
+            String expected = "[123, 200]";
96
+            String actual = testPhonebook.lookup("test");
97
+            Assert.assertEquals(expected,actual);
98
+    }
99
+
100
+    @Test
101
+    public void testLookup() {
102
+
103
+        //Given
104
+            //a person object with name and number
105
+
106
+        testPhonebook.add("JohnDoe", "1123");
107
+
108
+        //When
109
+            //we try and look up a person's phone number by using their name as the search thing
110
+        String expected = "[1123]";
111
+        String actual = testPhonebook.lookup("JohnDoe");
112
+
113
+        //Then
114
+            //we expect to see their number
115
+
116
+        Assert.assertEquals(expected, actual);
117
+
118
+    }
119
+
120
+    @Test
121
+    public void testListNamesOnly1 (){
122
+        //Given
123
+            //a bunch of people with names and numbers
124
+        testPhonebook.add("Andrea", "8888");
125
+        testPhonebook.add("Alexei", "8877");
126
+        testPhonebook.add("Addison", "1234");
127
+        testPhonebook.add("AdDy", "1234");
128
+
129
+        //When
130
+            //we ask the program to display just the names
131
+        String actual = testPhonebook.showNamesOnly();
132
+        String expected = "AdDy\nAddison\nAlexei\nAndrea\n";
133
+
134
+        //Then
135
+            //we expect to see just the names
136
+        Assert.assertEquals(expected, actual);
137
+    }
138
+
139
+    @Test
140
+    public void displayEntirePhoneBookContentsTest(){
141
+        //Given
142
+            //a list of peoples name and numbers
143
+        testPhonebook.add("Andrea", "8888");
144
+        testPhonebook.add("Alexei", "8877");
145
+        //When
146
+            //we ask to see all entries with name and numbers
147
+        String actual = testPhonebook.displayEntirePhoneBookContents();
148
+        String expected = "Alexei's phone number(s): [8877]\nAndrea's phone number(s): [8888]\n";
149
+        //Then
150
+            //we expect to see all entries with name and numbers
151
+        Assert.assertEquals(expected, actual);
152
+    }
153
+
154
+    @Test
155
+    public void addAdditionalNumberTest(){
156
+        //Given
157
+            //a person object with an existing number
158
+        testPhonebook.add("Andrea", "8888");
159
+        //When
160
+            // we add an addtional number to that person object
161
+        testPhonebook.personTreeMap.get("Andrea").addAdditionalNumber("1111");
162
+
163
+        //Then
164
+            // we expect to see that number added
165
+        String actual= testPhonebook.displayEntirePhoneBookContents();
166
+        String expected = "Andrea's phone number(s): [8888, 1111]\n";
167
+//        String actual = testPhonebook.lookup("Andrea");
168
+//        String expected = "[8888, 1111]";
169
+        Assert.assertEquals(expected, actual);
170
+
171
+    }
172
+    @Test
173
+    public void removeSingleNumberTest(){
174
+        //Given
175
+            //an entry with more than 1 number
176
+        testPhonebook.add("Andrea", "8888");
177
+        testPhonebook.personTreeMap.get("Andrea").addAdditionalNumber("1111");
178
+        testPhonebook.personTreeMap.get("Andrea").addAdditionalNumber("2222");
179
+
180
+        //When
181
+            //we try to remove a number
182
+        testPhonebook.personTreeMap.get("Andrea").removeSingleNumber("1111");
183
+
184
+        //Then
185
+            //we expect to see the original entry with all of its numbers except the one we removed
186
+        String actual = testPhonebook.displayEntirePhoneBookContents();
187
+        String expected = "Andrea's phone number(s): [8888, 2222]\n";
188
+        Assert.assertEquals(expected, actual);
189
+    }
190
+
191
+
192
+
7 193
 }