Rachelle 6 years ago
parent
commit
4617bcd5ae
2 changed files with 16 additions and 6 deletions
  1. 11
    0
      Entry.java
  2. 5
    6
      PhoneBook.java

+ 11
- 0
Entry.java View File

@@ -19,6 +19,17 @@ public class Entry
19 19
         phoneNumber.append(entryArray.get(entryArray.size()-1));
20 20
         return phoneNumber.toString();
21 21
     }
22
+    
23
+    public boolean hasPhoneNumber(String phoneNumber){
24
+        boolean hasPhoneNumber = false;
25
+            for(String number:entryArray){
26
+            if(phoneNumber.equals(number)){
27
+                hasPhoneNumber = true;
28
+                break;
29
+            }
30
+        }  
31
+        return hasPhoneNumber;
32
+    }
22 33
 
23 34
     public void addPhoneNumber(String phoneNumber){
24 35
         entryArray.add(phoneNumber);

+ 5
- 6
PhoneBook.java View File

@@ -41,7 +41,7 @@ public class PhoneBook {
41 41
                 if(reverseLookup(phoneNumber).equals("no match")){
42 42
                     System.out.println("\n**Number not found.**");
43 43
                 }else{
44
-                    System.out.println("This is " + reverseLookup(phoneNumber) + "'s number.");
44
+                    System.out.println("\n**" + phoneNumber + " listed to:**\n" + reverseLookup(phoneNumber));
45 45
                 }
46 46
                 continueMessage();
47 47
                 break;
@@ -149,15 +149,14 @@ public class PhoneBook {
149 149
     }
150 150
 
151 151
     public String reverseLookup(String number){
152
-        String nameForAGivenNumber = "no match";
152
+        StringBuilder personsWithGivenNumber = new StringBuilder();
153 153
         for(String name: phonebook.keySet()){
154 154
             Entry entry = phonebook.get(name);
155
-            String phoneNumber = entry.getPhoneNumber();
156
-            if (phoneNumber.equals(number)){
157
-                nameForAGivenNumber = name;
155
+            if (entry.hasPhoneNumber(number)){
156
+                personsWithGivenNumber.append(name + "\n");
158 157
             }
159 158
         }
160
-        return nameForAGivenNumber;
159
+        return personsWithGivenNumber.toString();
161 160
     }
162 161
 
163 162
     //Deletion