|
@@ -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
|
}
|