Browse Source

saving changes

Rachelle 6 years ago
parent
commit
f93401ae14
4 changed files with 396 additions and 30 deletions
  1. 29
    5
      Entry.java
  2. 270
    13
      PhoneBook.java
  3. 59
    3
      PhoneBookTest.java
  4. 38
    9
      package.bluej

+ 29
- 5
Entry.java View File

@@ -2,7 +2,6 @@ import java.util.ArrayList;
2 2
 import java.util.List;
3 3
 public class Entry
4 4
 {
5
-    String name;
6 5
     String phoneNumber;
7 6
     List<String> entryArray = new ArrayList <String>();
8 7
 
@@ -12,12 +11,37 @@ public class Entry
12 11
 
13 12
     public String getPhoneNumber(){
14 13
         StringBuilder phoneNumber = new StringBuilder();
15
-        for(int i  = 0; i < entryArray.size(); i++){
16
-            phoneNumber.append(entryArray.get(i));
17
-            if(entryArray.size() > 1){
18
-                phoneNumber.append(", ");
14
+        if(entryArray.size()>1){
15
+            for(int i  = 0; i < entryArray.size()-1; i++){
16
+                phoneNumber.append(entryArray.get(i) + ", ");
19 17
             }
20 18
         }
19
+        phoneNumber.append(entryArray.get(entryArray.size()-1));
21 20
         return phoneNumber.toString();
22 21
     }
22
+
23
+    public void addPhoneNumber(String phoneNumber){
24
+        entryArray.add(phoneNumber);
25
+    }
26
+
27
+    public boolean entryArrayContainsNumber(String phonenumber){
28
+        boolean containsNumber = false;
29
+        if(entryArray.contains(phonenumber)){
30
+            containsNumber = true;
31
+        }
32
+        return containsNumber;
33
+    }
34
+
35
+    public int getNumberOfRecords(){
36
+        return entryArray.size();
37
+    }
38
+
39
+    public void removePhoneNumber(String phoneNumber){
40
+        for(String number:entryArray){
41
+            if(phoneNumber.equals(number)){
42
+                entryArray.remove(number);
43
+                break;
44
+            }
45
+        }
46
+    }
23 47
 } 

+ 270
- 13
PhoneBook.java View File

@@ -1,26 +1,283 @@
1
- import java.util.TreeMap;
2
- import java.util.Map;
3
- import java.util.ArrayList;
1
+import java.util.TreeMap;
2
+import java.util.Map;
3
+import java.util.ArrayList;
4
+import java.util.Scanner;
4 5
 
5 6
 /**
6
- * Created by leon on 1/23/18.
7
+ * Created by Rachelle
7 8
  */
8 9
 public class PhoneBook {
9 10
     Map<String,Entry> phonebook = new TreeMap<String,Entry>();
11
+    Scanner scanner = new Scanner(System.in);
10 12
     public PhoneBook(){       
11
-        
13
+
14
+    }
15
+
16
+    public void main(){       
17
+        System.out.println("Welcome!"); 
18
+        boolean runningProgram = true;
19
+        while(runningProgram){
20
+            System.out.println("\nSelect an option from the menu.\n"
21
+                + "(Add): to add a new entry or to an existing entry\n(Lookup): to lookup a number\n"
22
+                + "(Reverse): to perform a reverse lookup\n(Display) to print phonebook\n"
23
+                + "(Delete): to remove a record or a phone number\n(Exit): to exit the phonebook");
24
+            switch(getUserInputNormalized()){
25
+                case "add": 
26
+                addEntry(getNameFromUser(),getNumberFromUser());
27
+                break;
28
+
29
+                case "lookup":
30
+                String name = getNameFromUser();
31
+                if(hasEntry(name)){
32
+                    System.out.println("\n" + name + ": " + lookupNumber(name));   
33
+                }else{
34
+                    System.out.println("\n**Name not found**");
35
+                }
36
+                continueMessage();
37
+                break;
38
+
39
+                case "reverse":
40
+                String phoneNumber = getNumberFromUser();
41
+                if(reverseLookup(phoneNumber).equals("no match")){
42
+                    System.out.println("\n**Number not found.**");
43
+                }else{
44
+                    System.out.println("This is " + reverseLookup(phoneNumber) + "'s number.");
45
+                }
46
+                continueMessage();
47
+                break;
48
+
49
+                case "display":
50
+                if(phonebook.isEmpty()){
51
+                    System.out.println("\n**Nothing to display. Please add an entry first.**");   
52
+                }else{
53
+                    System.out.println(displayPhonebook());
54
+                }
55
+                continueMessage();
56
+                break;
57
+
58
+                case "delete":
59
+                deleteRecordOrNumberPrompt();
60
+                break;
61
+
62
+                case "exit": runningProgram = false;
63
+                break;
64
+
65
+                default: System.out.println("\n**Command not found.**\n");
66
+                break;
67
+            }
68
+        }
69
+    }
70
+
71
+    //Input
72
+    public String getUserInputNormalized(){
73
+        Scanner scanner = new Scanner(System.in);
74
+        return scanner.nextLine().toLowerCase().trim();
75
+    }
76
+
77
+    public String getNumberFromUser(){
78
+        System.out.println("Enter phone number.");
79
+        return scanner.nextLine();
12 80
     }
13
-    
81
+
82
+    public String getNameFromUser(){
83
+        System.out.println("Enter name.");
84
+        return scanner.nextLine();
85
+    }
86
+
87
+    //Additions
14 88
     public void addEntry(String name, String phoneNumber){
15
-        Entry newEntry = new Entry(phoneNumber);
16
-        phonebook.put(name,newEntry);        
89
+        if(hasEntry(name)){
90
+            duplicateEntry(name, phoneNumber);
91
+        }else{
92
+            Entry newEntry = new Entry(phoneNumber);
93
+            phonebook.put(name,newEntry);
94
+            recordUpdatedMessage(name, phoneNumber);
95
+        }
96
+    }
97
+
98
+    public boolean hasEntry(String name){
99
+        return phonebook.containsKey(name);
100
+    }
101
+
102
+    public void duplicateEntry(String name, String phoneNumber){
103
+        boolean duplicateError = false;
104
+        do{    
105
+            System.out.println("\nDuplicate name found. Add to existing entry? (y/n). Enter (exit) to go back.");
106
+            String userInput = getUserInputNormalized();
107
+            if(userInput.equals("n")||userInput.equals("no")){
108
+                addEntry(getNameFromUser(),phoneNumber);
109
+            }else if(userInput.equals("y")||userInput.equals("yes")){
110
+                addAdditionalNumbers(name, phoneNumber);
111
+            }else if(userInput.equals("exit")){
112
+                break;
113
+            }else{
114
+                System.out.println("\n**Command not recognized.**\n");
115
+                duplicateError = true;
116
+            }
117
+        }while(duplicateError);
118
+    }
119
+
120
+    public void addAdditionalNumbers(String name, String phoneNumber){
121
+        boolean duplicateNumber = false;
122
+        do{
123
+            if(getEntry(name).entryArrayContainsNumber(phoneNumber)){
124
+                System.out.println("\nThis phone number is already listed for " + name + ". Enter a new number or enter (exit) to go back.");
125
+                System.out.println("Record found: " + displayRecord(name));
126
+                String userInput = getUserInputNormalized();
127
+                if(getEntry(name).entryArrayContainsNumber(userInput)){
128
+                    duplicateNumber = true;
129
+                }else if(!userInput.equals("exit")){
130
+                    getEntry(name).addPhoneNumber(userInput);
131
+                    recordUpdatedMessage(name,userInput);
132
+                    duplicateNumber = false;
133
+                }
134
+            }else{
135
+                getEntry(name).addPhoneNumber(phoneNumber);
136
+                recordUpdatedMessage(name, phoneNumber);
137
+            }
138
+        }while(duplicateNumber);
17 139
     }
18
-    
140
+
19 141
     public Entry getEntry(String name){
20 142
         return phonebook.get(name);
21 143
     }
22
-    
23
-    
24
-    
25
-    
144
+
145
+    //Lookups
146
+    public String lookupNumber(String name){
147
+        Entry entryToLookUp = phonebook.get(name);
148
+        return entryToLookUp.getPhoneNumber();
149
+    }
150
+
151
+    public String reverseLookup(String number){
152
+        String nameForAGivenNumber = "no match";
153
+        for(String name: phonebook.keySet()){
154
+            Entry entry = phonebook.get(name);
155
+            String phoneNumber = entry.getPhoneNumber();
156
+            if (phoneNumber.equals(number)){
157
+                nameForAGivenNumber = name;
158
+            }
159
+        }
160
+        return nameForAGivenNumber;
161
+    }
162
+
163
+    //Deletion
164
+    public void deleteRecordOrNumberPrompt(){
165
+        boolean error = false;
166
+        do{
167
+            if(phonebook.size()<=0){
168
+                System.out.println("\n**No records to delete.**\n");
169
+                break;
170
+            }else{
171
+                System.out.println("\nRemove an entire (record) or remove a phone (number)? Enter (exit) to go back.");
172
+                String userInput = getUserInputNormalized();
173
+                if(userInput.equals("record")){
174
+                    removeRecord();
175
+                }else if(userInput.equals("number")){
176
+                    removeNumber();
177
+                }else if(userInput.equals("exit")){
178
+                    break;
179
+                }else{
180
+                    System.out.println("\n**Command not recognized.**");
181
+                    error = true;
182
+                }
183
+            }
184
+        }while(error);
185
+    }
186
+
187
+    public void removeRecord(){
188
+        String recordToRemove = getNameFromUser();
189
+        if(hasEntry(recordToRemove)){
190
+            confirmRemoveEntry(recordToRemove);
191
+        }else{
192
+            System.out.println("\n**Name not found.**\n");
193
+        }
194
+    }
195
+
196
+    public void  confirmRemoveEntry(String name){
197
+        boolean continueWithDelete = true;
198
+        while(continueWithDelete){  
199
+            System.out.println("\n" + displayRecord(name) + "\nContinue with deleting this listing? (y/n)");
200
+            String userInput = getUserInputNormalized();
201
+            if(userInput.equals("n")||userInput.equals("no")){
202
+                System.out.println("\n**Delete cancelled.**\n");
203
+                continueWithDelete = false;
204
+            }else if(userInput.equals("y")||userInput.equals("yes")){
205
+                removeEntry(name);
206
+                continueWithDelete = false;
207
+            }else{
208
+                System.out.println("\n**Command not recognized.**");
209
+            }
210
+        }
211
+    }
212
+
213
+    public void removeEntry(String name){
214
+        phonebook.remove(name);
215
+        System.out.println("\n**" + name + " deleted.**\n");
216
+    }
217
+
218
+    public void removeNumber(){
219
+        String name = getNameFromUser();
220
+        if(hasEntry(name) && getEntry(name).getNumberOfRecords()>1){
221
+            deleteWhereMultipleEntries(name);
222
+        }else if(hasEntry(name) && getEntry(name).getNumberOfRecords()==1){
223
+            deleteWhereSingleEntry(name);
224
+        }else{
225
+            System.out.println("\n**Name not found.**\n");
226
+        }
227
+    }
228
+
229
+    public void deleteWhereMultipleEntries(String name){
230
+        System.out.println("\nPhone numbers listed for: " + displayRecord(name));
231
+        String phoneNumber = getNumberFromUser();
232
+        getEntry(name).removePhoneNumber(phoneNumber);
233
+        System.out.println("\n**Deleted " + phoneNumber + ".**\n**Record updated to: " + displayRecord(name) + ".**");
234
+        continueMessage();
235
+    }
236
+
237
+    public void deleteWhereSingleEntry(String name){
238
+        System.out.println("\nThere is only one number listed for " + displayRecord(name) + ".");
239
+        boolean continueToPrompt = true;
240
+        while(continueToPrompt){
241
+            System.out.println("Continue with deleting this record? (y/n)");
242
+            String userInput = getUserInputNormalized();
243
+            if(userInput.equals("n")||userInput.equals("no")){
244
+                System.out.println("\n**Delete cancelled.**\n");
245
+                continueToPrompt = false;
246
+            }else if(userInput.equals("y")||userInput.equals("yes")){
247
+                removeEntry(name);
248
+                continueToPrompt = false;
249
+            }else{
250
+                System.out.println("\n**Command not recognized.**\n");
251
+            }
252
+        }
253
+    }
254
+
255
+    //Display
256
+    public String displayPhonebook(){
257
+        StringBuilder stringToDisplay = new StringBuilder();
258
+        stringToDisplay.append("\n**Listings:**\n");
259
+        for(String name: phonebook.keySet()){
260
+            Entry entry = phonebook.get(name);
261
+            String phoneNumber = entry.getPhoneNumber();
262
+            stringToDisplay.append(name + ": " + phoneNumber + "\n");
263
+        }
264
+        return stringToDisplay.toString();
265
+    }
266
+
267
+    public String displayRecord(String name){
268
+        StringBuilder stringToDisplay = new StringBuilder();
269
+        String phoneNumber = phonebook.get(name).getPhoneNumber();
270
+        stringToDisplay.append(name + ": " + phoneNumber);
271
+        return stringToDisplay.toString();
272
+    }
273
+
274
+    //Messaging
275
+    public void continueMessage(){
276
+        System.out.println("Press enter to continue.");
277
+        scanner.nextLine();
278
+    }
279
+
280
+    public void recordUpdatedMessage(String name, String phoneNumber){
281
+        System.out.println("\n**" + phoneNumber + " added to " + name + "**\n");
282
+    }
26 283
 }

+ 59
- 3
PhoneBookTest.java View File

@@ -4,16 +4,72 @@ import java.util.ArrayList;
4 4
 
5 5
 public class PhoneBookTest
6 6
 {   
7
+    PhoneBook newEntry = new PhoneBook();
7 8
     @Test 
8
-    public void addEntryTest(){
9
-        PhoneBook newEntry = new PhoneBook();
9
+    public void addEntryTest(){      
10 10
         newEntry.addEntry("Rachelle","123-4567");
11 11
         Entry actual = newEntry.getEntry("Rachelle");
12 12
         assertEquals("123-4567",actual.getPhoneNumber());
13 13
     }
14 14
 
15
+    @Test 
16
+    public void addNumberToExistingEntryTest(){      
17
+        newEntry.addEntry("Rachelle","123-4567");
18
+        Entry actual = newEntry.getEntry("Rachelle");
19
+        actual.addPhoneNumber("987-6543");
20
+        assertEquals("123-4567, 987-6543",actual.getPhoneNumber());
21
+    }
22
+
23
+    @Test 
24
+    public void addNumberToExistingEntryTest2(){      
25
+        newEntry.addEntry("Rachelle","123-4567");
26
+        newEntry.addAdditionalNumbers("Rachelle","987-6543");
27
+        assertEquals("123-4567, 987-6543",newEntry.lookupNumber("Rachelle"));
28
+    }
29
+
15 30
     @Test
16 31
     public void hasEntryTest(){
17
-        
32
+        newEntry.addEntry("Rachelle","123-4567");
33
+        assertTrue(newEntry.hasEntry("Rachelle"));
34
+    }
35
+
36
+    @Test
37
+    public void removeEntryTest(){
38
+        newEntry.addEntry("Rachelle","123-4567");
39
+        newEntry.removeEntry("Rachelle");
40
+        assertFalse(newEntry.hasEntry("Rachelle"));
41
+    }
42
+
43
+    @Test public void lookupNumberTest(){
44
+        newEntry.addEntry("Rachelle","123-4567");
45
+        assertEquals("123-4567",newEntry.lookupNumber("Rachelle"));
46
+    }
47
+
48
+    @Test public void reverseLookupTest(){
49
+        newEntry.addEntry("Rachelle","123-4567");
50
+        assertEquals("Rachelle",newEntry.reverseLookup("123-4567"));
51
+    }
52
+
53
+    @Test public void reverseLookupTest2(){
54
+        newEntry.addEntry("Rachelle","123-4567");
55
+        assertEquals("no match",newEntry.reverseLookup("987-5432"));
56
+    }
57
+
58
+    @Test public void displayPhonebookTest(){
59
+        newEntry.addEntry("Rachelle","123-4567");
60
+        newEntry.addEntry("Kris","987-6543");
61
+        assertEquals("Kris 987-6543\nRachelle 123-4567\n", newEntry.displayPhonebook());
62
+    }
63
+    
64
+    @Test public void entryArrayContainsNumberTest(){
65
+        newEntry.addEntry("Rachelle","123-4567");
66
+        Entry actual = newEntry.getEntry("Rachelle");
67
+        assertTrue(actual.entryArrayContainsNumber("123-4567"));
68
+    }
69
+    
70
+    @Test public void entryArrayContainsNumberTest2(){
71
+        newEntry.addEntry("Rachelle","123-4567");
72
+        Entry actual = newEntry.getEntry("Rachelle");
73
+        assertFalse(actual.entryArrayContainsNumber("987-6543"));
18 74
     }
19 75
 }

+ 38
- 9
package.bluej View File

@@ -1,20 +1,29 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=0
3
-editor.fx.0.width=0
4
-editor.fx.0.x=0
5
-editor.fx.0.y=0
2
+dependency1.from=PhoneBook
3
+dependency1.to=Entry
4
+dependency1.type=UsesDependency
5
+dependency2.from=PhoneBookTest
6
+dependency2.to=PhoneBook
7
+dependency2.type=UsesDependency
8
+dependency3.from=PhoneBookTest
9
+dependency3.to=Entry
10
+dependency3.type=UsesDependency
11
+editor.fx.0.height=709
12
+editor.fx.0.width=800
13
+editor.fx.0.x=205
14
+editor.fx.0.y=23
6 15
 objectbench.height=101
7 16
 objectbench.width=461
8 17
 package.divider.horizontal=0.6
9 18
 package.divider.vertical=0.8007380073800738
10 19
 package.editor.height=427
11 20
 package.editor.width=674
12
-package.editor.x=427
13
-package.editor.y=143
21
+package.editor.x=300
22
+package.editor.y=38
14 23
 package.frame.height=600
15 24
 package.frame.width=800
16
-package.numDependencies=0
17
-package.numTargets=1
25
+package.numDependencies=3
26
+package.numTargets=3
18 27
 package.showExtends=true
19 28
 package.showUses=true
20 29
 project.charset=UTF-8
@@ -23,4 +32,24 @@ readme.name=@README
23 32
 readme.width=47
24 33
 readme.x=10
25 34
 readme.y=10
26
-
35
+target1.height=50
36
+target1.name=PhoneBookTest
37
+target1.showInterface=false
38
+target1.type=UnitTestTargetJunit4
39
+target1.width=120
40
+target1.x=120
41
+target1.y=30
42
+target2.height=50
43
+target2.name=Entry
44
+target2.showInterface=false
45
+target2.type=ClassTarget
46
+target2.width=80
47
+target2.x=400
48
+target2.y=120
49
+target3.height=50
50
+target3.name=PhoneBook
51
+target3.showInterface=false
52
+target3.type=ClassTarget
53
+target3.width=100
54
+target3.x=80
55
+target3.y=280