Quellcode durchsuchen

78th iteration thanks to vg

Amy Gill vor 6 Jahren
Ursprung
Commit
b04e62fafa

+ 14
- 24
src/main/java/com/zipcodewilmington/phonebook/Person.java Datei anzeigen

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

+ 25
- 53
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Datei anzeigen

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

+ 96
- 0
src/main/java/com/zipcodewilmington/phonebook/oldPhonebookClass.java Datei anzeigen

@@ -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
+*/

+ 3
- 5
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Datei anzeigen

@@ -14,8 +14,6 @@ public class PhoneBookTest {
14 14
 
15 15
     PhoneBook testPhonebook = new PhoneBook();
16 16
 
17
-    TreeMap<String, String> personMapTest= new TreeMap<String, String>();
18
-
19 17
     @Before
20 18
 
21 19
     public void setup(){
@@ -27,10 +25,10 @@ public class PhoneBookTest {
27 25
     @Test
28 26
     public void testLookup() {
29 27
 
30
-        personMapTest.put("JohnDoe", "1123");
28
+        testPhonebook.add("JohnDoe", "1123");
31 29
 
32
-        String expected = "JohnDoe";
33
-        String actual = testPhonebook.lookup(expected);
30
+        String expected = "1123";
31
+        String actual = testPhonebook.lookup("JohnDoe");
34 32
 
35 33
         Assert.assertEquals(expected, actual);
36 34