Browse Source

second iteration. still not done. thanks

Amy Gill 6 years ago
parent
commit
efb2d03770

+ 20
- 11
src/main/java/com/zipcodewilmington/phonebook/Person.java View File

@@ -1,6 +1,22 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3 3
 public class Person {
4
+
5
+    //variables
6
+
7
+    /*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
8
+     is that you want to "hide" how you store the information from the users.
9
+
10
+    */
11
+    private String name;
12
+    private String phoneNumber;
13
+
14
+    //constructor
15
+    public Person(String name, String number) {
16
+        this.name= name;
17
+        this.phoneNumber = number;
18
+    }
19
+    //"everything else"
4 20
     public String getName() {
5 21
         return name;
6 22
     }
@@ -9,20 +25,13 @@ public class Person {
9 25
         this.name = name;
10 26
     }
11 27
 
12
-    public String getNumber() {
13
-        return number;
28
+    public String getPhoneNumber() {
29
+        return phoneNumber;
14 30
     }
15 31
 
16
-    public void setNumber(String number) {
17
-        this.number = number;
32
+    public void setPhoneNumber(String number) {
33
+        this.phoneNumber = number;
18 34
     }
19 35
 
20
-    private String name;
21
-    private String number;
22
-
23
-    public Person(String name, String number) {
24
-        this.name= name;
25
-        this.number= number;
26
-    }
27 36
 
28 37
 }

+ 44
- 1
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java View File

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

+ 7
- 4
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java View File

@@ -20,11 +20,14 @@ public class PhoneBookTest {
20 20
 
21 21
 
22 22
     @Test
23
-    public void getName() {
23
+    public void testLookup() {
24 24
 
25
-        testPhonebook.add(john.getName(), john.getNumber());
25
+        testPhonebook.add(john.getName(), john.getPhoneNumber());
26 26
 
27
-        String testNumber = "111-555-1234";
28
-        Assert.assertTrue();
27
+        Person actualPerson = testPhonebook.lookup(john.getName());
28
+
29
+        Assert.assertEquals(john.getPhoneNumber(), actualPerson.getPhoneNumber());
29 30
 
30 31
     }
32
+
33
+}