-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMyContactList.java
More file actions
425 lines (352 loc) · 11.9 KB
/
TestMyContactList.java
File metadata and controls
425 lines (352 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.util.Scanner;
/**
* AL = Alex, GR = Gabe, AK = Adina
* Creates object of class MyContactList and manupilates with that object. GR, AK
**/
public class TestMyContactList {
private static MyContactList theContactList;
private static File listFile;
private static FileOutputStream fileOut;
private static ObjectOutputStream objectOut;
private static FileInputStream fileIn;
private static ObjectInputStream objectIn;
public static void main(String[] args) throws ClassNotFoundException {
listFile = new File("ContactList.ser");
readExistingContactsFromDisk();
char usersChoice = ' ';
while (usersChoice != 'q') {
usersChoice = showMenu();
switch (usersChoice) {
case 'n':
// enter contact
theContactList.addContact(promptUser());
break;
case 'p':
// print contact list
System.out.println(theContactList.toString());
break;
case 's':
// search contact by last name
searchContactPrompt();
break;
case 'q':
saveContactsToDisk();
break;
default:
System.out.println("****Error, " + usersChoice + " is not a recognized command.****");
break;
}
}
} // End Main
/**
* AL, AK
* Takes care of all the user prompting and
* returns a contact object containing all the information
**/
private static Person promptUser() {
Person createContact = new Person();
Scanner input = new Scanner(System.in);
String command;
System.out.print("Enter first name: ");
createContact.setFirstName(input.nextLine());
do {
command = "";
System.out.print("Enter last name: ");
createContact.setLastName(input.nextLine());
if (createContact.getLastName().isEmpty()) {
System.out.println("---You must enter in last name---");
while (!command.equals("q") && !command.equals("c")) {
System.out.print("Please enter 'q' to quit or 'c' to contine\nYour Choice: ");
command = input.nextLine().trim();
if (command.equals("q"))
return createContact;
else if (command.equals("c"))
;
else
System.out.println("Command '" + command + "' not found");
}
}
} while (createContact.getLastName().isEmpty());
System.out.println("Street Address:");
System.out.print("\t" + "Enter street number: ");
createContact.getStreetAddress().setStreetNumber(input.nextLine());
System.out.print("\t" + "Enter street name: ");
createContact.getStreetAddress().setStreetName(input.nextLine());
System.out.print("\t" + "Enter city name: ");
createContact.getStreetAddress().setCityName(input.nextLine());
System.out.print("\t" + "Enter state name: ");
createContact.getStreetAddress().setStateName(input.nextLine());
System.out.print("\t" + "Enter zip code: ");
createContact.getStreetAddress().setZipCode(input.nextLine());
System.out.print("Enter email address: ");
createContact.setEmailAddress(input.nextLine());
System.out.print("Enter phone number: ");
createContact.setPhoneNumber(input.nextLine());
System.out.print("Enter any notes about this contact: ");
createContact.setNotes(input.nextLine());
System.out.println();
return createContact;
}
/**
* Prints menu, gets and returns user's choice
* AK
**/
private static char showMenu() {
Scanner scan = new Scanner(System.in);
System.out.println("\n**********MAIN MENU**********");
// number of contact
System.out.println("Enter: ");
System.out.println("- \"n\" to enter in a contact");
System.out.println("- \"p\" to print the contact list");
System.out.println("- \"s\" to search contacts by last name");
System.out.println("- \"q\" to quit the program");
System.out.print("Your choice: ");
char choice = scan.next().charAt(0);
String junk = scan.nextLine();
return choice;
}
/**
* Reads data from file and prints how many contacts are in that file
* AK, AL
**/
private static void readExistingContactsFromDisk() {
if (!listFile.exists()) {
theContactList = new MyContactList();
System.out.println("Zero contacts currently in list");
return;
}
try {
fileIn = new FileInputStream(listFile);
objectIn = new ObjectInputStream(fileIn);
theContactList = (MyContactList) objectIn.readObject();
objectIn.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("MyContactList class not found");
c.printStackTrace();
return;
}
System.out.println("Number of Contacts in List: "
+ theContactList.getListSize());
}
/**
* Saves data to file and prints message saying that data is succesfully daved
* AL, AK
**/
private static void saveContactsToDisk() {
if (listFile.exists())
listFile.delete();
try {
fileOut = new FileOutputStream(listFile);
objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(theContactList);
objectOut.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
System.out.println("--Program Quitted. Contacts saved to disk--");
}
/**
* Prompts user for contact's last name to search for contact
* AL, GR
**/
private static void searchContactPrompt() {
Scanner input = new Scanner(System.in);
System.out.println("\n---------Search contacts by last name--------");
System.out.print("Enter Last Name: ");
System.out.println(theContactList.searchContactByLastName(input
.nextLine()));
System.out.print("\nEnter any key to return to main menu...");
input.nextLine();
System.out.println();
}
}
/*-------------------------------------FINAL RUN--------------------------------------
Number of Contacts in List: 3
**********MAIN MENU**********
Enter:
- "n" to enter in a contact
- "p" to print the contact list
- "s" to search contacts by last name
- "q" to quit the program
Your choice: p
-------------Print Contact List------------
First Name: Ann
Last Name: Aiken
Street Address: 342 Holerith Rd, Los Angeles, CA, 86758
Email Address: ann.aiken@gmail.com
First Name: Howard
Last Name: Aiken
Street Address: 575 S Charleston ave, Los Alamos, CA, 95067
Email Address: howard.aiken@gmail.com
Phone Number: 6508796754
Notes: cs
First Name: Richard
Last Name: Feynman
Street Address: 111 N Rengstorff ave, Mountain View, CA, 94043
Email Address: bob.feynman@gmail.com
Phone Number: 6505375085
Notes: he is cool
**********MAIN MENU**********
Enter:
- "n" to enter in a contact
- "p" to print the contact list
- "s" to search contacts by last name
- "q" to quit the program
Your choice: s
---------Search contacts by last name--------
Enter Last Name: Aiken
-Results-
First Name: Ann
Last Name: Aiken
Street Address: 342 Holerith Rd, Los Angeles, CA, 86758
Email Address: ann.aiken@gmail.com
First Name: Howard
Last Name: Aiken
Street Address: 575 S Charleston ave, Los Alamos, CA, 95067
Email Address: howard.aiken@gmail.com
Phone Number: 6508796754
Notes: cs
Enter any key to return to main menu...
**********MAIN MENU**********
Enter:
- "n" to enter in a contact
- "p" to print the contact list
- "s" to search contacts by last name
- "q" to quit the program
Your choice: s
---------Search contacts by last name--------
Enter Last Name: Kosset
-Results-
Enter any key to return to main menu...
**********MAIN MENU**********
Enter:
- "n" to enter in a contact
- "p" to print the contact list
- "s" to search contacts by last name
- "q" to quit the program
Your choice: n
Enter first name:
Enter last name:
---You must enter in last name---
Please enter 'q' to quit or 'c' to contine
Your Choice: c
Enter last name: Kosset
Street Address:
Enter street number:
Enter street name:
Enter city name:
Enter state name:
Enter zip code:
Enter email address:
Enter phone number:
Enter any notes about this contact: sister
**********MAIN MENU**********
Enter:
- "n" to enter in a contact
- "p" to print the contact list
- "s" to search contacts by last name
- "q" to quit the program
Your choice: p
-------------Print Contact List------------
First Name: Ann
Last Name: Aiken
Street Address: 342 Holerith Rd, Los Angeles, CA, 86758
Email Address: ann.aiken@gmail.com
First Name: Howard
Last Name: Aiken
Street Address: 575 S Charleston ave, Los Alamos, CA, 95067
Email Address: howard.aiken@gmail.com
Phone Number: 6508796754
Notes: cs
First Name: Richard
Last Name: Feynman
Street Address: 111 N Rengstorff ave, Mountain View, CA, 94043
Email Address: bob.feynman@gmail.com
Phone Number: 6505375085
Notes: he is cool
Last Name: Kosset
Notes: sister
**********MAIN MENU**********
Enter:
- "n" to enter in a contact
- "p" to print the contact list
- "s" to search contacts by last name
- "q" to quit the program
Your choice: q
--Program Quitted. Contacts saved to disk--
--------------------------------------------------------------------------------- **/
/**
* Feedback from Alex This class is a work in progress, we still need to take
* care of file input and output. Since we are printing our contacts in a table,
* we must also print column names in main In the future I think this Tester
* class should have some other static methods that each take care of a specific
* job such as printing, scanning, or reading. That way the main method doesn't
* look so clutered. Also, lets rename the class from Tester to TestContactList
* or something
**/
/**
* Feedback from Adina Change the name of the class so that it matches the name
* of the .java file. Or, as Alex said, it would be even better to call the file
* TestMyContactList.java and call class TestMyContactList. I believe this would
* be more descriptive. Write JavaDoc format comments above this class that
* should start "This class tests..." Create an object of EACH class (both class
* Contact and class MyContactList) and call EACH method on these objects. COPY
* AND PASTE recording of run. /
*
* /** Feedback from Gabe to Adina on TestMyContactList Make Scanner private;
* More descriptive object name; I liked what you did when asking for last name,
* but i think the guideline wants us to just not add the contact if the last
* name wasn't entered. Maybe add it as a helper method and use it if allowed?;
* To both Alex + Adina: Does the use of the promptUser() method + the
* addContact(Contact person) makes the use of newContact(...) essentially
* useless? Should we get rid of newContact(...); on Contact() Instead of
* Assignments onthe constructor why don't we just call the setters thus
* minimizing code? Do we need that many tabs?
**/
/**
* Feedback fro Adina to Alex:
* Output does not match UI design,
* 1)the first line should say "-------------Print Contact List------------"
* 2)UI has ONE space(\n) above each printed contact, programs output does not have any before the first contact and has two between
* following contacts.
* 3)When list is printed, program does not have "Press Enter to return to main prompt: " as UI design does.
* 4)Also, Ui design and program output have different number of spaces between printed info. For exapmle,
* "First Name: Howard" in program output lacks spaces
*
* I tested the implementation of case 2 on my system and run I got is:
*
* __All Contacts__
First Name: Howard
Last Name: Aiken
Street Address: 555 S Charleston, Los Alamos, CA, 98543
Email Address: aiken.howard@gmail.com
Phone Number: 6509874567
Notes: he is a scientist
First Name: Ann
Last Name: Feynman
Street Address: 111 N Rengstorff ave, Mountain View, CA, 94043
Email Address: feynman.ann@gmail.com
Phone Number: 6505375084
Notes: cs2
First Name: Richard
Last Name: Feynman
Street Address: 111 N Rengstorff ave, Mountain View, CA, 94043
Email Address: feynman.bob@gmail.com
Phone Number: 6505375085
Notes: he is cool
First Name: John
Last Name: Nash
Street Address: 435 Showers Dr, Los Angeles, CA, 95043
Notes: math
**/