-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressBookLauncher.java
370 lines (354 loc) · 12.4 KB
/
AddressBookLauncher.java
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;
class AddressBook
{
// class variables
//patterns for differnt fields
static String firstNamePattern = "^[a-zA-Z][a-zA-Z ]*$";
static String lastNamePattern = "^[a-zA-Z][a-zA-Z ]*$";
static String addressPattern = "^[a-zA-Z0-9-,. ]+$";
static String cityPattern = "^[a-zA-Z][a-zA-Z ]*$";
static String statePattern = "^[a-zA-Z][a-zA-Z ]*$";
static String zipPattern = "^\\d{6}$";
static String phoneNumberPattern = "^\\d{10}$";
//scanner variable is declared as static to use throughout the program
static final Scanner scanner = new Scanner(System.in);
//contains set of contacts which are empty(contact created and no details added)
static Set<String> emptyContacts = new HashSet<>();
//contains set of contacts which are non empty(contact created and details added)
static Set<String> nonEmptyContacts = new HashSet<>();
// instance methods
//creates empty contact
void createNewContact() throws Exception
{
System.out.print("Enter name of the contact:");
String contactName = scanner.nextLine();
File file = new File(contactName);
if (file.exists())
{
System.out.println("contact " + file.getName() + " already exists!");
}
else
{
if (file.createNewFile())
{
System.out.println("new contact " + file.getName() + " is created successfully");
emptyContacts.add(file.getName());
}
else
{
System.out.println("file creation failed!");
}
}
}
//writes given content into given file
void writeFile(String fileName,String content)throws Exception
{
FileWriter fw = new FileWriter(fileName);
fw.write(content);
fw.close();
nonEmptyContacts.add(fileName);
}
Boolean validate(String name, String regex)
{
return Pattern.matches(regex, name);
}
String takeInput(String field, String pattern)
{
String input;
do
{
System.out.print("enter " + field + ":");
input = scanner.nextLine();
} while (!validate(input, pattern));
return input;
}
//given fields are added into contact
void fillContactDetails()throws Exception
{
System.out.print("enter empty contact name which is going to be filed:");
String contactName = scanner.nextLine();
if (emptyContacts.contains(contactName))
{
String details = "";
details += takeInput("first name", firstNamePattern) + "\n";
details += takeInput("last name", lastNamePattern) + "\n";
details += takeInput("address", addressPattern) + "\n";
details += takeInput("city", cityPattern) + "\n";
details += takeInput("state", statePattern) + "\n";
details += takeInput("zip code", zipPattern) + "\n";
details += takeInput("phone number", phoneNumberPattern) + "\n";
writeFile(contactName, details);
emptyContacts.remove(contactName);
System.out.println("the given fields are successfully added in " + contactName);
}
else
{
System.out.println(contactName + " is not empty contact or it is not created");
System.out.println("use other option c to create new contact or option e to edit already created one");
}
}
//displays list of empty contacts and non empty contacts
void displayAllContacts()
{
boolean flag = false;
if (emptyContacts.size() != 0)
{
System.out.println("the empty contacts are:");
for (String contact : emptyContacts)
{
System.out.println(contact);
}
flag=true;
}
if (nonEmptyContacts.size() != 0)
{
System.out.println("the non empty contacts are:");
for (String contact : nonEmptyContacts)
{
System.out.println(contact);
}
flag=true;
}
if(!flag)
{
System.out.println("no contacts are created yet");
}
}
//prints the given file content
void readFile(String fileName)throws Exception
{
FileReader fileReader = new FileReader(fileName);
int character;
while ((character = fileReader.read()) != -1)
{
System.out.print((char) character);
}
fileReader.close();
}
//displays all fields in the given contact
void viewContactInfo()throws Exception
{
System.out.print("enter name of the contact to view:");
String contactName = scanner.nextLine();
if(emptyContacts.contains(contactName))
{
System.out.println("pleast fill the contact "+ contactName+ " before viewing it");
return;
}
else if(!nonEmptyContacts.contains(contactName))
{
System.out.println("please create the contact "+contactName+" before viewing it");
return;
}
System.out.println("the content of " + contactName + " is:");
readFile(contactName);
}
void editContactInfo()throws Exception
{
System.out.print("enter name of the contact to edit:");
String contactName = scanner.nextLine();
//if given file is empty
if(emptyContacts.contains(contactName))
{
System.out.println(contactName + " is empty!");
System.out.println("pleast fill the contact "+ contactName + " before editing it");
return;
}
//if given file is non empty
else if(!nonEmptyContacts.contains(contactName))
{
System.out.println(contactName + " does not exits!");
System.out.println("please create the contact "+ contactName +" before editing it");
return;
}
System.out.println("The content of " + contactName + " at present is:");
//array to store lines in a file
ArrayList<String> arrayList = new ArrayList<>();
//each line is printed and added to the array list
try (BufferedReader reader = new BufferedReader(new FileReader(contactName)))
{
while (reader.ready())
{
String line = reader.readLine();
System.out.println(line);
arrayList.add(line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("select a field to edit");
System.out.println("f for first name");
System.out.println("l for last name");
System.out.println("a for Address");
System.out.println("c for city");
System.out.println("s for state");
System.out.println("z for zip");
System.out.println("p for phonenumber");
System.out.print("enter field to edit:");
String choice = scanner.nextLine().trim().toLowerCase();
int field = 0;
String pattern;
switch (choice)
{
case "f":
field = 0;
pattern = firstNamePattern;
break;
case "l":
field = 1;
pattern = lastNamePattern;
break;
case "a":
field = 2;
pattern = addressPattern;
break;
case "c":
field = 3;
pattern = cityPattern;
break;
case "s":
field = 4;
pattern = statePattern;
break;
case "z":
field = 4;
pattern = zipPattern;
break;
case "p":
field = 6;
pattern = phoneNumberPattern;
break;
default:
System.out.println("please enter field correctly");
pattern = "";
break;
}
String newData = takeInput("new data of the field", pattern);
String newContent = "";
for (int i = 0; i < arrayList.size(); i++)
{
if (i == field)
{
arrayList.add(i, newData);
}
newContent += arrayList.get(i) + "\n";
}
String option;
do
{
System.out.println("enter... S for SAVE SA for SAVE AS C for CANCEL");
option = scanner.nextLine().trim().toLowerCase();
} while (!(option.equals("s") || option.equals("sa")) || option.equals("c"));
switch (option)
{
// the field is updated in the same file
case "s":
writeFile(contactName, newContent);
System.out.println(contactName + " is edited successfully");
break;
// the field is updated in givenfilenamewithoutextension.csv file
case "sa":
String extension = ".csv";
String newContactName = contactName.replaceFirst("[.][^.]+$", "") + extension;
writeFile(newContactName, newContent);
nonEmptyContacts.add(newContactName);
System.out.println("changes are saved successfully in " + newContactName + " file");
break;
case "c":
System.out.println("changes are not saved");
return;
default:
System.out.println("please select either S or SA or C");
break;
}
}
void deleteContact()
{
System.out.print("enter contact name to delete:");
String contactName = scanner.nextLine().trim();
File file = new File(contactName);
if(file.exists())
{
if(file.delete())
{
if(emptyContacts.contains(contactName))
{
emptyContacts.remove(contactName);
}
else
{
nonEmptyContacts.remove(contactName);
}
System.out.println(contactName + " is deleted succesfully");
}
else
{
System.out.println("file deletion failed");
}
}
else
{
System.out.println(contactName + "doesn't exists");
}
}
}
public class AddressBookLauncher
{
// class variable
static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws Exception
{
AddressBook addressBook = new AddressBook();
while (true)
{
System.out.println("--------------ENTER-------------");
System.out.println("c for creating a new contact");
System.out.println("f for filling contact details");
System.out.println("p for printing all contacts");
System.out.println("v for viewing contact information");
System.out.println("e for editing contact information");
System.out.println("d for deleting contact information");
System.out.println("q for quitting");
System.out.print("enter option:");
String option = scanner.nextLine().trim().toLowerCase();
switch (option)
{
case "c":
addressBook.createNewContact();
break;
case "f":
addressBook.fillContactDetails();
break;
case "p":
addressBook.displayAllContacts();
break;
case "v":
addressBook.viewContactInfo();
break;
case "e":
addressBook.editContactInfo();
break;
case "d":
addressBook.deleteContact();
break;
case "q":
System.out.println("quiting....!");
System.exit(0);
default:
System.out.println("please enter the correct choice");
break;
}
}
}
}