public void AddAdressBook() { Console.WriteLine("Enter the title of the address book"); string addressBookName = Console.ReadLine(); AddressBookMain ContactDetails = new AddressBookMain(addressBookName); addressBook.Add(addressBookName, ContactDetails); Console.WriteLine("\nAddress Book " + addressBookName + " added successfully"); Console.WriteLine("Updated Address Book List:"); foreach (var kvp in addressBook) { Console.WriteLine("Key: {0}, Value: {1}", kvp.Key); } }
/// <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\akash\OneDrive\Desktop\Standard Input and output\AddressBookProgram\{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 adressBook = 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 (addressBook.Count != 0) { bool flag = false; foreach (var kvp in addressBook) { 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\akash\OneDrive\Desktop\Standard Input and output\AddressBookProgram\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(); }
static void Main(string[] args) { AdressBook adressBook = new AdressBook(); Console.WriteLine("Welcome to Address Book Program"); bool flag = true; while (flag) { 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: adressBook.AddAdressBook(); break; case 2: currentAddressBookName = adressBook.ExistingAddressBook(); break; case 3: adressBook.SearchPersonByCityOrState(); break; case 4: AddressBookMain.ViewPeopleByCityOrState(); break; case 5: AddressBookMain.GetCountByCityOrState(); break; case 6: flag = 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: AdressBook.addressBook[currentAddressBookName].AddContact(); break; case 2: AdressBook.addressBook[currentAddressBookName].EditContact(); break; case 3: AdressBook.addressBook[currentAddressBookName].SearchByName(); break; case 4: AdressBook.addressBook[currentAddressBookName].DeleteContact(); break; case 5: AdressBook.addressBook[currentAddressBookName].SortByName(); break; case 6: AdressBook.addressBook[currentAddressBookName].SortByCityStateOrZip(); break; case 7: FileIOStream.WriteFileStream(AdressBook.addressBook[currentAddressBookName]); break; case 8: Console.WriteLine("press\n1-To Write\n2-To Read"); int options = Convert.ToInt32(Console.ReadLine()); if (options == 1) { FileIOStream.CSVFileWriting(AdressBook.addressBook[currentAddressBookName]); Console.WriteLine("File created/Details added successfully"); } else if (options == 2) { FileIOStream.CSVFileReading(AdressBook.addressBook[currentAddressBookName]); } break; case 9: Console.WriteLine("press\n1-To Write\n2-To Read"); int opt = Convert.ToInt32(Console.ReadLine()); if (opt == 1) { FileIOStream.JSONFileWriting(AdressBook.addressBook[currentAddressBookName]); Console.WriteLine("File created/Details added successfully"); } else if (opt == 2) { FileIOStream.JSONFileReading(AdressBook.addressBook[currentAddressBookName]); } break; case 10: flag2 = false; break; } } } } }