-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressBookSystem.cs
More file actions
424 lines (357 loc) · 15.1 KB
/
AddressBookSystem.cs
File metadata and controls
424 lines (357 loc) · 15.1 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
using System;
using System.Collections.Generic;
namespace addressBooksystem
{
internal class Program
{
private static void Welcome()
{
Console.WriteLine("Welcome to Address Book System");
}
static void Main(string[] args)
{
Welcome(); // welcome note
AddressBookSystem addressBookSystem = new AddressBookSystem(); // Initialize AddressBookSystem
// Loop to show menu options
bool exit = false;
while (!exit)
{
DisplayMainMenu();
Console.Write("Enter your choice (1-4): ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
// Add a new address book
addressBookSystem.AddNewAddressBook();
break;
case "2":
// Operate on an existing address book
addressBookSystem.OperateOnAddressBook();
break;
case "3":
// Display all address books
addressBookSystem.DisplayAllAddressBooks();
break;
case "4":
// Exit the program
exit = true;
break;
default:
Console.WriteLine("Invalid choice. Please enter a valid option (1-4).");
break;
}
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
private static void DisplayMainMenu()
{
Console.WriteLine("\nMain Menu:");
Console.WriteLine("1. Add a new address book");
Console.WriteLine("2. Operate on an existing address book");
Console.WriteLine("3. Display all address books");
Console.WriteLine("4. Exit");
}
}
class AddressBookSystem
{
private Dictionary<string, AddressBook> addressBooks = new Dictionary<string, AddressBook>();
public void AddNewAddressBook()
{
Console.Write("Enter the name of the new address book: ");
string newBookName = Console.ReadLine();
if (!addressBooks.ContainsKey(newBookName))
{
AddressBook newAddressBook = new AddressBook();
addressBooks.Add(newBookName, newAddressBook);
Console.WriteLine($"Address Book '{newBookName}' added successfully.");
}
else
{
Console.WriteLine($"Address Book with the name '{newBookName}' already exists.");
}
}
public void OperateOnAddressBook()
{
Console.Write("Enter the name of the address book: ");
string bookName = Console.ReadLine();
if (addressBooks.TryGetValue(bookName, out AddressBook selectedAddressBook))
{
// Operate on the selected address book
bool exitAddressBook = false;
while (!exitAddressBook)
{
DisplayAddressBookMenu(bookName);
Console.Write($"Enter your choice for '{bookName}' Address Book (1-6): ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
// Create a new contact in the selected address book
CreateNewContact(selectedAddressBook);
break;
case "2":
// Edit an existing contact
EditExistingContact(selectedAddressBook);
break;
case "3":
// Display all contacts in the address book
selectedAddressBook.SortContactsByName();
Console.WriteLine($"\nAll Contacts in '{bookName}' Address Book (Sorted by Name):");
selectedAddressBook.DisplayAllContacts();
break;
case "4":
// Search for a person in a city
SearchPersonInCity();
break;
case "5":
// Search for a person in a state
SearchPersonInState();
break;
case "6":
// Exit the address book menu
exitAddressBook = true;
break;
default:
Console.WriteLine("Invalid choice. Please enter a valid option (1-6).");
break;
}
}
}
else
{
Console.WriteLine($"Address Book with the name '{bookName}' not found.");
}
}
private static void DisplayAddressBookMenu(string bookName)
{
Console.WriteLine($"\nAddress Book Menu for '{bookName}':");
Console.WriteLine("1. Create a new contact");
Console.WriteLine("2. Edit an existing contact");
Console.WriteLine("3. Display all contacts");
Console.WriteLine("4. Search for a person in a city");
Console.WriteLine("5. Search for a person in a state");
Console.WriteLine("6. Exit");
}
public void DisplayAllAddressBooks()
{
Console.WriteLine("\nAll Address Books:");
foreach (var bookName in addressBooks.Keys)
{
Console.WriteLine($"- {bookName}");
}
}
private static void CreateNewContact(AddressBook addressBook)
{
// Getting contact details from the user
ContactPerson newContact = GetContactDetailsFromUser();
// Adding the new contact to the address book
addressBook.AddContact(newContact);
Console.WriteLine("Contact Added:");
DisplayContact(newContact); // Call DisplayContact method here
}
private static ContactPerson GetContactDetailsFromUser()
{
Console.Write("Enter First Name: ");
string firstName = Console.ReadLine();
Console.Write("Enter Last Name: ");
string lastName = Console.ReadLine();
Console.Write("Enter Address: ");
string address = Console.ReadLine();
Console.Write("Enter City: ");
string city = Console.ReadLine();
Console.Write("Enter State: ");
string state = Console.ReadLine();
Console.Write("Enter Zip Code: ");
string zip = Console.ReadLine();
Console.Write("Enter Phone Number: ");
string phoneNumber = Console.ReadLine();
Console.Write("Enter Email: ");
string email = Console.ReadLine();
return new ContactPerson
{
FirstName = firstName,
LastName = lastName,
Address = address,
City = city,
State = state,
Zip = zip,
PhoneNumber = phoneNumber,
Email = email
};
}
private static void EditExistingContact(AddressBook addressBook)
{
Console.Write("Enter the First Name of the contact to edit: ");
string editFirstName = Console.ReadLine();
Console.Write("Enter the Last Name of the contact to edit: ");
string editLastName = Console.ReadLine();
// Find the contact in the address book
ContactPerson existingContact = addressBook.FindContact(editFirstName, editLastName);
if (existingContact != null)
{
// Display existing contact details
Console.WriteLine("\nExisting Contact Details:");
DisplayContact(existingContact); // Call DisplayContact method here
// Get updated details from the user
ContactPerson updatedContact = GetContactDetailsFromUser();
// Update the existing contact
existingContact.UpdateContact(updatedContact);
Console.WriteLine("Contact Updated:");
DisplayContact(existingContact); // Call DisplayContact method here
}
else
{
Console.WriteLine("Contact not found. Unable to edit.");
}
}
private void SearchPersonInCity()
{
Console.Write("Enter the city name to search for: ");
string city = Console.ReadLine();
List<ContactPerson> foundContacts = new List<ContactPerson>();
foreach (var addressBook in addressBooks.Values)
{
foundContacts.AddRange(addressBook.FindContactsInCity(city));
}
if (foundContacts.Count > 0)
{
Console.WriteLine($"Found {foundContacts.Count} person(s) in {city}:");
foreach (var contact in foundContacts)
{
Console.WriteLine($"Name: {contact.FirstName} {contact.LastName}");
Console.WriteLine($"Address: {contact.Address}, {contact.City}, {contact.State} {contact.Zip}");
Console.WriteLine($"Phone Number: {contact.PhoneNumber}");
Console.WriteLine($"Email: {contact.Email}\n");
}
}
else
{
Console.WriteLine($"No person found in {city}.");
}
}
private void SearchPersonInState()
{
Console.Write("Enter the state name to search for: ");
string state = Console.ReadLine();
List<ContactPerson> foundContacts = new List<ContactPerson>();
foreach (var addressBook in addressBooks.Values)
{
foundContacts.AddRange(addressBook.FindContactsInState(state));
}
if (foundContacts.Count > 0)
{
Console.WriteLine($"Found {foundContacts.Count} person(s) in {state}:");
foreach (var contact in foundContacts)
{
Console.WriteLine($"Name: {contact.FirstName} {contact.LastName}");
Console.WriteLine($"Address: {contact.Address}, {contact.City}, {contact.State} {contact.Zip}");
Console.WriteLine($"Phone Number: {contact.PhoneNumber}");
Console.WriteLine($"Email: {contact.Email}\n");
}
}
else
{
Console.WriteLine($"No person found in {state}.");
}
}
private static void DisplayContact(ContactPerson contact)
{
Console.WriteLine($"Name: {contact.FirstName} {contact.LastName}");
Console.WriteLine($"Address: {contact.Address}, {contact.City}, {contact.State} {contact.Zip}");
Console.WriteLine($"Phone Number: {contact.PhoneNumber}");
Console.WriteLine($"Email: {contact.Email}\n");
}
}
class ContactPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public void UpdateContact(ContactPerson updatedContact)
{
FirstName = updatedContact.FirstName;
LastName = updatedContact.LastName;
Address = updatedContact.Address;
City = updatedContact.City;
State = updatedContact.State;
Zip = updatedContact.Zip;
PhoneNumber = updatedContact.PhoneNumber;
Email = updatedContact.Email;
}
}
class AddressBook
{
private List<ContactPerson> contacts;
private Dictionary<string, List<ContactPerson>> cityPersonDictionary;
private Dictionary<string, List<ContactPerson>> statePersonDictionary;
public AddressBook()
{
contacts = new List<ContactPerson>();
cityPersonDictionary = new Dictionary<string, List<ContactPerson>>();
statePersonDictionary = new Dictionary<string, List<ContactPerson>>();
}
public void AddContact(ContactPerson contact)
{
contacts.Add(contact);
// Update city-person dictionary
if (!cityPersonDictionary.ContainsKey(contact.City))
{
cityPersonDictionary[contact.City] = new List<ContactPerson>();
}
cityPersonDictionary[contact.City].Add(contact);
// Update state-person dictionary
if (!statePersonDictionary.ContainsKey(contact.State))
{
statePersonDictionary[contact.State] = new List<ContactPerson>();
}
statePersonDictionary[contact.State].Add(contact);
}
public int GetContactCountByCity(string city)
{
return cityPersonDictionary.ContainsKey(city) ? cityPersonDictionary[city].Count : 0;
}
public int GetContactCountByState(string state)
{
return statePersonDictionary.ContainsKey(state) ? statePersonDictionary[state].Count : 0;
}
public ContactPerson FindContact(string firstName, string lastName)
{
return contacts.Find(c =>
string.Equals(c.FirstName, firstName, StringComparison.OrdinalIgnoreCase) &&
string.Equals(c.LastName, lastName, StringComparison.OrdinalIgnoreCase));
}
public List<ContactPerson> FindContactsInCity(string city)
{
return cityPersonDictionary.ContainsKey(city) ? cityPersonDictionary[city] : new List<ContactPerson>();
}
public List<ContactPerson> FindContactsInState(string state)
{
return statePersonDictionary.ContainsKey(state) ? statePersonDictionary[state] : new List<ContactPerson>();
}
public void DisplayAllContacts()
{
foreach (var contact in contacts)
{
DisplayContact(contact);
}
}
private void DisplayContact(ContactPerson contact)
{
Console.WriteLine($"Name: {contact.FirstName} {contact.LastName}");
Console.WriteLine($"Address: {contact.Address}, {contact.City}, {contact.State} {contact.Zip}");
Console.WriteLine($"Phone Number: {contact.PhoneNumber}");
Console.WriteLine($"Email: {contact.Email}\n");
}
public void SortContactsByName()
{
contacts.Sort((x, y) => string.Compare(x.FirstName + x.LastName, y.FirstName + y.LastName));
}
}
}