Kaynağa Gözat

Final Changes

Elliott Stansbury 6 yıl önce
ebeveyn
işleme
f92c6dd678

+ 38
- 0
src/main/java/com/zipcodewilmington/phonebook/Main.java Dosyayı Görüntüle

@@ -0,0 +1,38 @@
1
+package com.zipcodewilmington.phonebook;
2
+
3
+
4
+import java.util.Scanner;
5
+
6
+public class Main {
7
+    public static void main(String[] args) {
8
+
9
+
10
+        PhoneBook phoneBook = new PhoneBook();
11
+        phoneBook.getContacts().put("", "");
12
+        phoneBook.getContacts().put("Reggie", "215...");
13
+        phoneBook.getContacts().put("Rick", "302...");
14
+        phoneBook.getContacts().put("Ronald", "215...");
15
+        phoneBook.getContacts().put("Riley Home", "302...");
16
+        phoneBook.getContacts().put("Riley Work", "484...");
17
+
18
+        phoneBook.getContacts().put("Richard", "215...");
19
+        phoneBook.getContacts().put("Randall", "302...");
20
+        phoneBook.getContacts().put("Ralph", "302..");
21
+//        System.out.println(phoneBook.getContacts().get("Reggie"));
22
+//        phoneBook.getContacts().put("Reggie", "302...");
23
+//        phoneBook.removeEntry("Reggie", "215..");
24
+        //System.out.println(getContacts());
25
+        phoneBook.removeEntry("Ronald");
26
+        phoneBook.addEntry("Sarah","484...");
27
+        phoneBook.addEntry("Sabrina","484...");
28
+        phoneBook.addEntry("Selena","484...");
29
+        phoneBook.addEntry("Shannon","484...");
30
+        phoneBook.addEntry("Skylar","484...");
31
+        //phoneBook.listNamesAndNumbers();
32
+        phoneBook.reverseLookUp("215...");
33
+        System.out.println(phoneBook.reverseLookUp("215..."));
34
+        //System.out.println(phoneBook.listNamesAndNumbers());
35
+    }
36
+
37
+
38
+}

+ 99
- 0
src/main/java/com/zipcodewilmington/phonebook/PhoneBook.java Dosyayı Görüntüle

@@ -1,7 +1,106 @@
1 1
 package com.zipcodewilmington.phonebook;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.Collections;
6
+import java.util.HashMap;
7
+import java.util.List;
8
+
3 9
 /**
4 10
  * Created by leon on 1/23/18.
5 11
  */
6 12
 public class PhoneBook {
13
+
14
+
15
+    public HashMap<String, String> contacts;
16
+
17
+    public PhoneBook (){
18
+
19
+        contacts = new HashMap<>();
20
+    }
21
+
22
+    //public void setContacts(HashMap<String, String> contacts) {
23
+    public void setContacts(String name, String number){
24
+        //this.contacts = contacts;
25
+        this.contacts.put(name, number);
26
+        System.out.println(this.contacts);
27
+    }
28
+
29
+    public void addEntry(String name, String phoneNumber){
30
+
31
+        contacts.put(name,phoneNumber);
32
+    }
33
+
34
+
35
+    public boolean hasName(String name){
36
+        if(contacts.containsKey(name)){
37
+        return true;
38
+    } else {
39
+        return false;
40
+        }
41
+    }
42
+
43
+    public boolean hasNumber(String contactNumber){
44
+        if(contacts.containsValue(contactNumber)){
45
+            return true;
46
+        } else{
47
+            return false;
48
+        }
49
+
50
+    }
51
+
52
+    //public void addNumber()
53
+
54
+    public HashMap<String, String> getContacts() {
55
+
56
+        return contacts;
57
+    }
58
+
59
+    public boolean removeEntry(String name, String phoneNumber){
60
+
61
+        return contacts.remove(name, phoneNumber);
62
+    }
63
+
64
+    public String removeEntry(String name){
65
+        return contacts.remove(name);
66
+
67
+    }
68
+
69
+    public String reverseLookUp(String phoneNumber) {
70
+
71
+        for (String name : contacts.keySet()) {
72
+//            contacts.get(name);
73
+            if (contacts.get(name).equals(phoneNumber)) {
74
+                return name;
75
+            }
76
+        }
77
+        return null;
78
+//        for(int i = 0; i < contacts.size(); i++){
79
+//            if(contacts.containsValue(phoneNumber)){
80
+//                return contacts.get()
81
+//            }
82
+//        }
83
+//        return contacts.get(getContacts(phoneNumber));
84
+    }
85
+
86
+    public String listNamesAndNumbers(){
87
+        List<String> contactList = new ArrayList(contacts.keySet());
88
+        //System.out.println(contacts + "HERE'S THE CONTACTS");
89
+        Collections.sort(contactList);
90
+
91
+        String sortedNamesWithNumbers = " ";
92
+        for(String contactName : contactList){
93
+
94
+
95
+            sortedNamesWithNumbers += contactName + "   ";
96
+            sortedNamesWithNumbers += contacts.get(contactName);
97
+
98
+            sortedNamesWithNumbers += " \n";
99
+
100
+
101
+        }
102
+        System.out.println(sortedNamesWithNumbers);
103
+
104
+        return sortedNamesWithNumbers;
105
+    }
7 106
 }

+ 62
- 0
src/test/java/com/zipcodewilmington/phonebook/PhoneBookTest.java Dosyayı Görüntüle

@@ -1,7 +1,69 @@
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 static junit.framework.TestCase.assertTrue;
8
+
3 9
 /**
4 10
  * Created by leon on 1/23/18.
5 11
  */
6 12
 public class PhoneBookTest {
13
+    private PhoneBook phoneBook;
14
+
15
+
16
+    @Before
17
+    public void setUp() {
18
+        phoneBook = new PhoneBook();
19
+    }
20
+
21
+    @Test
22
+    public void addTest() {
23
+        phoneBook.addEntry("Reggie", "215...");
24
+        assertTrue(phoneBook.getContacts().get("Reggie") != null);
25
+        ;
26
+    }
27
+
28
+    @Test
29
+    public void hasEntryTest() {
30
+        phoneBook.addEntry("Reggie", "215...");
31
+        assertTrue(phoneBook.hasName("Reggie"));
32
+    }
33
+
34
+    @Test
35
+    public void addNumberTest() {
36
+        phoneBook.addEntry("Reggie", "302...");
37
+        //assertTrue(phoneBook.hasEntry());
38
+    }
39
+
40
+    @Test
41
+    public void lookupTest() {
42
+        phoneBook.addEntry("Reggie", "215...");
43
+        assertTrue(phoneBook.hasNumber("215..."));
44
+    }
45
+
46
+    @Test
47
+    public void removeEntryTest() {
48
+        phoneBook.addEntry("Reggie", "215...");
49
+        assertTrue(phoneBook.removeEntry("Reggie", "215..."));
50
+    }
51
+
52
+    @Test
53
+    public void reverseLookupTest() {
54
+        phoneBook.addEntry("Reggie", "215...");
55
+        //assertTrue(phoneBook.hasNumber("215..."));
56
+        Assert.assertEquals("Reggie", phoneBook.reverseLookUp("215..."));
57
+
58
+    }
59
+    @Test
60
+    public void listNameAndNumbersTest(){
61
+            String contactToBeRemoved = "Eugene";
62
+        phoneBook.addEntry("Reggie", "215...");
63
+
64
+        //assertTrue(phoneBook.listNamesAndNumbers());
65
+            assertTrue(phoneBook.getContacts().get(contactToBeRemoved) == null);
66
+    }
7 67
 }
68
+
69
+