-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBook.java
More file actions
732 lines (636 loc) · 23.7 KB
/
Copy pathPhoneBook.java
File metadata and controls
732 lines (636 loc) · 23.7 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//Normal Imports
import java.util.*;
import java.util.Scanner;
import java.util.HashMap;
import java.util.LinkedList;
import javax.swing.*;
//File Imports
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PhoneBook {
private static Scanner x;
public static void main(String[] args) {
HashMap<String, Card> phonebook = new HashMap<String, Card>();
Trie phoneNumbers = new Trie();
LinkedList<ContactList> contacts = new LinkedList<>();
String choice = "";
String name = "";
String number = "";
String address = "";
String addressToSave = "";
String line = "";
String filepath = "contacts.txt";
String houseNumber = "";
String street = "";
String apt = "";
String aptNum = "";
String city = "";
String state = "";
String zip = "";
String areaCode = "";
String location = "";
Scanner main = new Scanner(System.in);
System.out.println("Show menu? (y/n): ");
String run = main.nextLine();
//------------------------------WELCOME HEADER----------------------------------//
do {
System.out.println("+---------------------------------------+");
System.out.println("| ** WELCOME TO XLOOKUP ** |");
System.out.println("| Press 1. to Add Contact |");
System.out.println("| Press 2. to Display Contacts |");
System.out.println("| Press 3. to Find Contact |");
System.out.println("| Press 4. to Update Contact |");
System.out.println("| Press 5. to Delete Contact |");
System.out.println("| Press 6. to Display Contact List |");
System.out.println("+---------------------------------------+");
Scanner option = new Scanner(System.in);
System.out.print("Please enter your choice: ");
choice = option.nextLine();
//------------------------------------------------------------------------------//
//-------------------------QUESTIONS---------------------------------------------//
switch (choice)
{
case "1":
/**
* Ask user for their name to add in phonebook
* with assciotation to their number
*/
Scanner nam = new Scanner(System.in);
System.out.println("What is your name?: " + "\n");
name = nam.nextLine();
contacts.add(new ContactList(name));
/**
* Ask user for a phone number to attribute to their name
*/
Scanner n = new Scanner(System.in);
System.out.println("\nWhat is your number?: ");
System.out.println("\t (NO DASHES, SPACES, OR PARENTHESIS!) " + "\n");
number = n.nextLine();
phoneNumbers.add(number); //adds to the Trie
/**
* Ask user if they live in apartment or not
* user answer determines the format of the address
*/
System.out.println("+--------------------------------+");
System.out.println("| ADDRESS INFORMATION |");
System.out.println("+--------------------------------+");
Scanner a = new Scanner(System.in);
System.out.print("\nDo you live in Apartment? (y/n): ");
String addy = a.nextLine();
if (addy.equals("y")) {
//Gets House Number
Scanner l1 = new Scanner(System.in);
System.out.println("\nHouse Number: ");
System.out.println("-------------\n");
houseNumber = l1.nextLine();
//Gets Street Name
Scanner l2 = new Scanner(System.in);
System.out.println("\nStreet: ");
System.out.println("-------\n");
street = l2.nextLine();
//Gets Apartment Number
Scanner l3 = new Scanner(System.in);
System.out.println("\nApartment Number: ");
System.out.println("-----------------\n");
aptNum = l3.nextLine();
//Gets City Name
Scanner l4 = new Scanner(System.in);
System.out.println("\nCity: ");
System.out.println("-----\n");
city = l4.nextLine();
//Gets State Name
Scanner l5 = new Scanner(System.in);
System.out.println("\nState: ");
System.out.println("------\n");
state = l5.nextLine();
//Gets Zip Code
Scanner l6 = new Scanner(System.in);
System.out.println("\nZip Code: ");
System.out.println("---------\n");
zip = l6.nextLine();
//Adds all address information
addressToSave = houseNumber + "," + street + "," + "Apt," + aptNum + "," + city + "," + state + "," + zip;
address = houseNumber + " " + street + "\n\t" + "Apt " + aptNum + "\n\t" + city + ", " + state + " " + zip;
}else if (addy.equals("n")) {
//Gets House Number
Scanner l1 = new Scanner(System.in);
System.out.println("\nHouse number: ");
System.out.println("-------------\n");
houseNumber = l1.nextLine();
//Gets Street Name
Scanner l2 = new Scanner(System.in);
System.out.println("\nStreet: ");
System.out.println("-------\n");
street = l2.nextLine();
//Gets City Name
Scanner l4 = new Scanner(System.in);
System.out.println("\nCity: ");
System.out.println("-----\n");
city = l4.nextLine();
//Gets State Name
Scanner l5 = new Scanner(System.in);
System.out.println("\nState: ");
System.out.println("------\n");
state = l5.nextLine();
//Gets Zip Code
Scanner l6 = new Scanner(System.in);
System.out.println("\nZip Code: ");
System.out.println("---------\n");
zip = l6.nextLine();
//Adds all address information
addressToSave = houseNumber + "," + street + "," + city + "," + state + "," + zip;
address = houseNumber + " " + street + "\n\t" + city + ", " + state + " " + zip;
} else{
addressToSave = "No Address Information";
address = "No Addres Information";
}
System.out.println("\n----------------------------------");
System.out.println("\n----------------------------------\n");
//----------------------------------------------------------------------------------------//
/**
* Reads in file and puts data into String[]
* Checks if first three numbers of phone number
* equals an area code in the csv file and then it grabs
* the area code and the location.
*/
String path = "/Users/xavieragostino/Desktop/Data Structures/AC.csv";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
while((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int i = 0; i < values.length; i++) {
if(number.substring(0, 3).equals(values[i])){
areaCode = values[i];
location = city + ", " + values[i+2];
}
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
System.out.println("Something went wrong in the try");
}
//add items into Hashmap
phonebook.put(number, new Card(name, number, address, areaCode, location));
saveRecord(name, number,addressToSave, filepath);
//----------------------------------------------------------------------------------------//
break;
case "2":
//-------------------------DISPLAYS DIRECTORY---------------------------------------------//
/**
* Looks through contact file to check if its empty
* if not then it adds all the info into hashmap, trie, and linkedList
* then it displays the phonebook with all the contacts information
* which is stored in a linkedList
*/
String pathDisplay = "/Users/xavieragostino/Desktop/Data Structures/Data Structure's Final Project/contacts.txt";
try
{
BufferedReader brD = new BufferedReader(new FileReader(pathDisplay));
while((line = brD.readLine()) != null) {
String[] display = line.split(",");
int i = 0;
if(display[i] != null && display[i].length() > 0)
{
if (display.length > 7){
name = display[i];
number = display[i+1];
houseNumber = display[i+2];
street = display[i+3];
apt = display[i+4];
aptNum = display[i+5];
city = display[i+6];
state = display[i+7];
zip = display[i+8];
address = houseNumber + " " + street + "\n\t" + apt + " " + aptNum + "\n\t" + city + ", " + state + " " + zip;
}else if (display.length < 8){
name = display[i];
number = display[i+1];
houseNumber = display[i+2];
street = display[i+3];
city = display[i+4];
state = display[i+5];
zip = display[i+6];
address = houseNumber + " " + street + "\n\t" + city + ", " + state + " " + zip;
}else {
address = "No Address Information";
}
String pathDisplay2 = "/Users/xavieragostino/Desktop/Data Structures/AC.csv";
try
{
BufferedReader brD1 = new BufferedReader(new FileReader(pathDisplay2));
while((line = brD1.readLine()) != null) {
String[] acs = line.split(",");
for (int j = 0; j < acs.length; j++) {
if(display[i+1].substring(0, 3).equals(acs[j])){
areaCode = acs[j];
location = city + ", " + acs[i+2];
}
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}else{
System.out.println("File Empty");
}
phonebook.put(number, new Card(name, number, address, areaCode, location));
contacts.add(new ContactList(name));
phoneNumbers.add(number);
}
}
catch (Exception e)
{
System.out.println(e);
}
displayDirectory(phonebook);
break;
//----------------------------------------------------------------------------------------//
case "3":
//-------------------------FINDS CONTACT---------------------------------------------//
/**
* Ask user do they want to find a contact
* @return contacts card
*/
String tryLookUpAgain = "n";
boolean found = false;
do{
Scanner fc = new Scanner(System.in);
System.out.println("What's the contact's phone number?: ");
String contactsNumber = fc.nextLine();
String pathFind = "/Users/xavieragostino/Desktop/Data Structures/Data Structure's Final Project/contacts.txt";
try {
BufferedReader br1 = new BufferedReader(new FileReader(pathFind));
while((line = br1.readLine()) != null) {
String[] items = line.split(",");
int i = 0;
if (items.length > 7){
name = items[i];
number = items[i+1];
houseNumber = items[i+2];
street = items[i+3];
apt = items[i+4];
aptNum = items[i+5];
city = items[i+6];
state = items[i+7];
zip = items[i+8];
address = houseNumber + " " + street + "\n\t" + apt + " " + aptNum + "\n\t" + city + ", " + state + " " + zip;
}else if (items.length < 8){
name = items[i];
number = items[i+1];
houseNumber = items[i+2];
street = items[i+3];
city = items[i+4];
state = items[i+5];
zip = items[i+6];
address = houseNumber + " " + street + "\n\t" + city + ", " + state + " " + zip;
}else {
address = "No Address Information";
}
try
{
String pathFindContactAC = "/Users/xavieragostino/Desktop/Data Structures/AC.csv";
BufferedReader br2 = new BufferedReader(new FileReader(pathFindContactAC));
while((line = br2.readLine()) != null)
{
String[] valuesF = line.split(",");
for (int j = 0; j < valuesF.length; j++)
{
if(contactsNumber.substring(0, 3).equals(valuesF[j]))
{
areaCode = valuesF[j];
location = city + ", " + valuesF[j+2];
}
}
}
}
catch (Exception e)
{
System.out.println("No Prior Location/Area Code Informaton");
System.out.println(e);
}
if (number.equals(contactsNumber))
{
Card c2 = new Card(name, number, address, areaCode, location);
System.out.println(c2.cardDisplay());
found = true;
}
}//ends while
if (found == false) {
System.out.println("Person Doesn't Already Exist in Directory");
System.out.println("The contact you entered does not live in directory.");
Scanner runBack = new Scanner(System.in);
System.out.println("Do you want to try again? (y/n): ");
tryLookUpAgain = runBack.nextLine();
}//ends else
}
catch (Exception e)
{
System.out.println(e);
}
} while (tryLookUpAgain.equals("y"));
//----------------------------------------------------------------------------------------//
break;
case "4":
//-----------------------------------------UPDATE CONTACT---------------------------------------------//
/**
* Ask user for their old phone number
* Finds number by checking if number exist
* inside of contacts(Hashmap, Trie, and conacts file)
* if it exist it then deletes it from the hashmap, trie, linkedlist
* and also it deletes the record of the person in the file
* Once the deletion of old number information happens then
* it prompts user to eneter their new number and adds the record to
* phonebook directory while also still keeping all address information the same.
*/
Scanner up = new Scanner(System.in);
System.out.println("What was your original phone number?: ");
String updateResponse = up.nextLine();
String updatePathFile = "/Users/xavieragostino/Desktop/Data Structures/Data Structure's Final Project/contacts.txt";
try {
BufferedReader br5 = new BufferedReader(new FileReader(updatePathFile));
while((line = br5.readLine()) != null) {
String[] uitems = line.split(",");
int i = 0;
if (uitems.length > 7){
name = uitems[i];
number = uitems[i+1];
houseNumber = uitems[i+2];
street = uitems[i+3];
apt = uitems[i+4];
aptNum = uitems[i+5];
city = uitems[i+6];
state = uitems[i+7];
zip = uitems[i+8];
addressToSave = houseNumber + "," + street + "," + "Apt," + aptNum + "," + city + "," + state + "," + zip;
address = houseNumber + " " + street + "\n\t" + apt + " " + aptNum + "\n\t" + city + ", " + state + " " + zip;
}else if (uitems.length < 8){
name = uitems[i];
number = uitems[i+1];
houseNumber = uitems[i+2];
street = uitems[i+3];
city = uitems[i+4];
state = uitems[i+5];
zip = uitems[i+6];
address = houseNumber + " " + street + "\n\t" + city + ", " + state + " " + zip;
addressToSave = houseNumber + "," + street + "," + city + "," + state + "," + zip;
}else {
address = "No Address Information";
addressToSave = "No Address Information";
}
if (number.equals(updateResponse))
{
contacts.remove(name);
phoneNumbers.delete(number);
phonebook.remove(number);
deleteRecord(filepath, updateResponse, 2, ",");
Scanner nn = new Scanner(System.in);
System.out.println("Enter your new number: ");
String newNumber = nn.nextLine();
phoneNumbers.add(newNumber);
String path6 = "/Users/xavieragostino/Desktop/Data Structures/AC.csv";
try {
BufferedReader br3 = new BufferedReader(new FileReader(path6));
while((line = br3.readLine()) != null) {
String[] valuesU = line.split(",");
for (int j = 0; j < valuesU.length; j++) {
if(newNumber.substring(0, 3).equals(valuesU[j])){
areaCode = valuesU[j];
location = city + ", " + valuesU[j+2];
}
}
}
}
catch (Exception e)
{
System.out.println(e);
}
phonebook.put(newNumber, new Card(name, newNumber, address, areaCode, location));
updateRecord(name, newNumber, addressToSave, filepath);
contacts.add(new ContactList(name));
}
}
}
catch (Exception e)
{
//System.out.println("Error trying to Update Record");
System.out.println(e);
}
//----------------------------------------------------------------------------------------------------//
break;
case "5":
//-----------------------------------------REMOVES CONTACT---------------------------------------------//
/**
* Ask User Who they want to remove
* then find the person they want to remove.
* remove from hashmap
* remove from Linked List
*
*/
Scanner askWho = new Scanner(System.in);
System.out.println("What is the phone number of the contact?: ");
String removePerson = askWho.nextLine();
String pathDelete = "/Users/xavieragostino/Desktop/Data Structures/Data Structure's Final Project/contacts.txt";
try {
BufferedReader br4 = new BufferedReader(new FileReader(pathDelete));
while((line = br4.readLine()) != null) {
String[] records = line.split(",");
int i = 0;
if (records[i + 1].equals(removePerson))
{
contacts.remove(records[i]);
phoneNumbers.delete(records[i+1]);
deleteRecord(filepath, removePerson, 2, ",");
}
}
} catch (Exception e){
//System.out.println("Error trying to Delete Record");
System.out.println(e);
}
phonebook.remove(removePerson);
//------------------------------------------------------------------------------------------------------//
break;
case "6":
//-------------------------DISPLAYS LIST OF CONTACTS---------------------------------------------//
/**
* Looks if contact file is empty
* If not then it adds all information into Hashmap, Trie, and LinkedList
* Then it displays list view of all contact names in directory.
*/
String pathCL = "/Users/xavieragostino/Desktop/Data Structures/Data Structure's Final Project/contacts.txt";
try
{
BufferedReader brCL = new BufferedReader(new FileReader(pathCL));
while((line = brCL.readLine()) != null) {
String[] cL = line.split(",");
int i = 0;
if(cL[i] != null && cL[i].length() > 0)
{
if (cL.length > 7){
name = cL[i];
number = cL[i+1];
houseNumber = cL[i+2];
street = cL[i+3];
apt = cL[i+4];
aptNum = cL[i+5];
city = cL[i+6];
state = cL[i+7];
zip = cL[i+8];
address = houseNumber + " " + street + "\n\t" + apt + " " + aptNum + "\n\t" + city + ", " + state + " " + zip;
}else if (cL.length < 8){
name = cL[i];
number = cL[i+1];
houseNumber = cL[i+2];
street = cL[i+3];
city = cL[i+4];
state = cL[i+5];
zip = cL[i+6];
address = houseNumber + " " + street + "\n\t" + city + ", " + state + " " + zip;
}else {
address = "No Address Information";
}
String pathCL2 = "/Users/xavieragostino/Desktop/Data Structures/AC.csv";
try
{
BufferedReader brCL2 = new BufferedReader(new FileReader(pathCL2));
while((line = brCL2.readLine()) != null) {
String[] areaCL = line.split(",");
for (int j = 0; j < areaCL.length; j++) {
if(cL[i+1].substring(0, 3).equals(areaCL[j])){
areaCode = areaCL[j];
location = city + ", " + areaCL[i+2];
}
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}else{
System.out.println("File Empty");
}
phonebook.put(number, new Card(name, number, address, areaCode, location));
contacts.add(new ContactList(name));
phoneNumbers.add(number);
}
}
catch (Exception e)
{
System.out.println(e);
}
displayListOfContacts(contacts);
break;
//---------------------------------------------------------------------------------------//
}
} while (run.equals("y"));
} //ends main
//-------------------------SAVES RECORD OF CONTACT---------------------------------------------//
public static void saveRecord(String name, String number,String address, String filepath)
{
try
{
FileWriter fw = new FileWriter(filepath, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println(name + "," + number + "," + address);
pw.flush(); //makes sure all data is written to file
pw.close();
JOptionPane.showMessageDialog(null, "Contact saved in Directory!");
}
catch(Exception E)
{
JOptionPane.showMessageDialog(null, "Contact NOT SAVED!!");
}
}
//-------------------------UPDATES RECORD OF CONTACT---------------------------------------------//
public static void updateRecord(String name, String number,String address, String filepath)
{
try
{
FileWriter fw = new FileWriter(filepath, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println(name + "," + number + "," + address);
pw.flush(); //makes sure all data is written to file
pw.close();
JOptionPane.showMessageDialog(null, "Contact was updated!");
}
catch(Exception E)
{
JOptionPane.showMessageDialog(null, "Contact was NOT UPDATED!!");
}
}
//----------------------------------------------------------------------------------------------//
//-------------------------DELETES RECORD OF CONTACT------------------------------------------------------//
public static void deleteRecord(String filepath, String removeTerm, int positionOfTerm, String delimiter)
{
int position = positionOfTerm - 1;
String tempFile = "temp.txt";
File oldFile = new File (filepath);
File newFile = new File (tempFile);
String currentLine;
String data[];
try
{
FileWriter fw = new FileWriter(tempFile,true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
FileReader fr = new FileReader(filepath);
BufferedReader br = new BufferedReader (fr);
while ((currentLine = br.readLine()) != null)
{
data = currentLine.split(",");
if (!(data[position].equalsIgnoreCase(removeTerm)))
{
pw.println(currentLine);
}
}
pw.flush();
pw.close();
fr.close();
br.close();
bw.close();
fw.close();
oldFile.delete();
File dump = new File(filepath);
newFile.renameTo(dump);
JOptionPane.showMessageDialog(null, "Contact was deleted from Directory!");
}
catch (Exception e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null, "Contact was not Deleted!!");
}
}
//---------------------------------------------------------------------------------------------------------//
//-------------------------PRINTS WHOLE PHONEBOOK DIRECTORY---------------------------------------------//
public static void displayDirectory(HashMap<String, Card> p) {
LinkedList<Object> contactCards = new LinkedList<>(p.values());
System.out.println(contactCards.toString());
}
//-----------------------------------------------------------------------------------------------------//
//-------------------------PRINTS LIST OF CONTACTS IN DIRECTORY---------------------------------------------//
public static void displayListOfContacts(LinkedList<ContactList> c) {
System.out.println("\n+--------------------------------+" + "\n" + "| CONTACTS |" + "\n" + "+--------------------------------+");
System.out.println(c.toString());
}
//----------------------------------------------------------------------------------------------------------//
}//end class
class phonebookD {}