/// <summary> /// UC 14 : Read the CSV file contents if existing otherwise throw exception /// </summary> /// <param name="addressBook"></param> public static void CSVFileReading(AddressBookMain addressBook) { try { string csvFilePath = @$ "C:\Users\hp\source\repos\AddressBookIO\AddressBookIO\{addressBook.addressBookName}ADDRESSBOOK.csv"; /// Initialize a new instance of the StreamReader class var reader = new StreamReader(csvFilePath); /// Creates an new CSV reader using the given TextReader var csv = new CsvReader(reader, CultureInfo.InvariantCulture); /// Store into the list the details which we get by GetRecords() method var records = csv.GetRecords <Contact>().ToList(); foreach (Contact contact in records) { Console.WriteLine("\nFullName: " + contact.firstName + " " + contact.lastName + "\nAddress: " + contact.address + "\nCity: " + contact.city + "\nState: " + contact.state + "\nZip: " + contact.zip + "\nPhoneNumber: " + contact.phoneNumber + "\nEmail: " + contact.email + "\n"); } /// Close the object so others can use the file residing at the path reader.Close(); }
/// <summary> /// UC 8 : SEARCHES FOR THE PERSON IN A PARTICULAR STATE OR CITY ACROSS ALL ADDRESS BOOKS AND DISPLAYS THE DETAILS /// </summary> public void SearchPersonByCityOrState() { AddressBookMain addressBook = new AddressBookMain(); Console.WriteLine("Enter either a city or state name below to search for people:"); string cityOrStateName = Console.ReadLine().ToUpper(); Console.WriteLine("Enter first name of the person you are searching"); string firstName = Console.ReadLine().ToUpper(); Console.WriteLine("Enter last name"); string lastName = Console.ReadLine().ToUpper(); if (addressBookListDictionary.Count != 0) { bool flag = false; foreach (var kvp in addressBookListDictionary) { for (int i = 0; i < kvp.Value.contactList.Count; i++) { if (kvp.Value.contactList[i].city == cityOrStateName || kvp.Value.contactList[i].state == cityOrStateName) { if (kvp.Value.contactList[i].firstName == firstName && kvp.Value.contactList[i].lastName == lastName) { /// IF BOTH NAME AND CITY/STATE OF A CONTACT ACROSS ANY ADDRESS BOOK MATCHES THE ENTERED DETAILS Console.WriteLine("\nContact found inside address book: " + kvp.Key); Console.WriteLine("FullName: " + kvp.Value.contactList[i].firstName + " " + kvp.Value.contactList[i].lastName + "\nAddress: " + kvp.Value.contactList[i].address + "\nCity: " + kvp.Value.contactList[i].city + "\nState: " + kvp.Value.contactList[i].state + "\nZip: " + kvp.Value.contactList[i].zip + "\nPhoneNumber: " + kvp.Value.contactList[i].phoneNumber + "\nEmail: " + kvp.Value.contactList[i].email + "\n"); flag = true; break; } } } } /// IF flag VALUE DOES NOT CHANGE MEANS NO MATCH FOR THE ENTERED DETAILS WAS FOUND if (flag == false) { Console.WriteLine("\nNo such contact found"); } } else { Console.WriteLine("\nNo stored address book found, please add one"); } }
/// <summary> /// UC 13 : Writes into the file present at the path the details of the passed address book. /// </summary> /// <param name="addressBook">The address book.</param> public static void WriteFileStream(AddressBookMain addressBook) { string path = @"C:\Users\hp\source\repos\AddressBookIO\AddressBookIO\ContactList.txt"; if (File.Exists(path)) { using (StreamWriter sr = File.AppendText(path)) { /// Writes the entered string into the file sr.Write("\nCONTACT DETAILS IN ADDRESSBOOK: {0}=>\n", addressBook.addressBookName); for (int i = 0; i < addressBook.contactList.Count; i++) { string line = "\n" + (i + 1) + ".\tFullName: " + addressBook.contactList[i].firstName + " " + addressBook.contactList[i].lastName + "\n\tAddress: " + addressBook.contactList[i].address + "\n\tCity: " + addressBook.contactList[i].city + "\n\tState: " + addressBook.contactList[i].state + "\n\tZip: " + addressBook.contactList[i].zip + "\n\tPhoneNumber: " + addressBook.contactList[i].phoneNumber + "\n\tEmail: " + addressBook.contactList[i].email + "\n"; sr.Write(line); } } } ReadFilestream(); }
/// <summary> /// UC 6 : ADDS A NEW ADDRESS BOOK WITH UNIQUE NAME TO THE SYSTEM /// </summary> public void AddNewAddressBook() { Console.WriteLine("Enter the name of address book:"); string addressBookName = Console.ReadLine().ToUpper(); AddressBookMain addressBook = new AddressBookMain(addressBookName); /// HANDLES THE EXCEPTION WHEN THE USER TRIES TO ENTER DUPLICATE ADDRESS BOOK NAME try { addressBookListDictionary.Add(addressBookName, addressBook); Console.WriteLine("\nAddress Book " + addressBookName + " added successfully"); Console.WriteLine("Updated Address Book List:"); foreach (var kvp in addressBookListDictionary) { Console.WriteLine(kvp.Key); } } catch (Exception) { Console.WriteLine("Address Book {0} already exists, please access it or add a new address book with different name!", addressBookName); } }
public static void MainMenuOperation() { AddressBookList addressBookList = new AddressBookList(); bool flag1 = true; while (flag1) { string currentAddressBookName = ""; Console.WriteLine("\nEnter:\n1-To add a new address Book\n2-To access an existing address book" + "\n3-To search person in a state or city across multiple address books\n4-View all persons of a city or state" + "\n5-To get count of contacts present at a city or state\n6-To exit"); int options1 = Convert.ToInt32(Console.ReadLine()); switch (options1) { case 1: addressBookList.AddNewAddressBook(); break; case 2: currentAddressBookName = addressBookList.ExistingAddressBook(); break; case 3: addressBookList.SearchPersonByCityOrState(); break; case 4: AddressBookMain.ViewPeopleByCityOrState(); break; case 5: AddressBookMain.GetCountByCityOrState(); break; case 6: flag1 = false; break; } if (currentAddressBookName != "") { bool flag2 = true; while (flag2) { Console.WriteLine("\nCurrent address book:" + currentAddressBookName); Console.WriteLine("Enter:\n1-To add a new contact\n2-To edit an existing contact\n3-To search for an existing contact\n4-To delete a contact\n5-To display all contacts in the address book sorted by Name\n6-To display contacts sorted by city,state or zip\n7-To Write and then Read all contacts into a txt file\n8-To Write/Read contact details into the addressBook CSV file\n9- To Write/Read contact details into the addressBook json file\n10-To return to main menu"); int options2 = Convert.ToInt32(Console.ReadLine()); switch (options2) { case 1: addressBookList.addressBookListDictionary[currentAddressBookName].AddNewContact(); break; case 2: addressBookList.addressBookListDictionary[currentAddressBookName].EditContact(); break; case 3: addressBookList.addressBookListDictionary[currentAddressBookName].SearchContactByName(); break; case 4: addressBookList.addressBookListDictionary[currentAddressBookName].DeleteContact(); break; case 5: addressBookList.addressBookListDictionary[currentAddressBookName].SortByName(); break; case 6: addressBookList.addressBookListDictionary[currentAddressBookName].SortByCityStateOrZip(); break; case 7: FileIOStream.WriteFileStream(addressBookList.addressBookListDictionary[currentAddressBookName]); break; case 8: Console.WriteLine("press\n1-To Write\n2-To Read"); int options = Convert.ToInt32(Console.ReadLine()); if (options == 1) { FileIOStream.CSVFileWriting(addressBookList.addressBookListDictionary[currentAddressBookName]); Console.WriteLine("File created/Details added successfully"); } else if (options == 2) { FileIOStream.CSVFileReading(addressBookList.addressBookListDictionary[currentAddressBookName]); } break; case 9: Console.WriteLine("press\n1-To Write\n2-To Read"); int opt = Convert.ToInt32(Console.ReadLine()); if (opt == 1) { FileIOStream.JSONFileWriting(addressBookList.addressBookListDictionary[currentAddressBookName]); Console.WriteLine("File created/Details added successfully"); } else if (opt == 2) { FileIOStream.JSONFileReading(addressBookList.addressBookListDictionary[currentAddressBookName]); } break; case 10: flag2 = false; break; } } } } }