Пример #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to address book program");

            ContactsCollection contactsCollection = new ContactsCollection();
            Contacts           contacts           = new Contacts(contactsCollection);
            CSVHandler         c = new CSVHandler(contactsCollection);
            ReadCsvWriteJSON   readCsvWriteJSON = new ReadCsvWriteJSON(contactsCollection);

            while (true)
            {
                Console.WriteLine("Enter Preference:");
                Console.WriteLine("1.Create Address Book \n 2.Search Person in same city or state \n 3.view Person in city or state \n4.View Person in alphabetic order\n 5.View Person by city state or Zip\n 6.Read data in File\n 7.Write in Csv File \n 8.Read CSV file\n 9.Writing JSON File \n 10.Exit");
                int preference = Convert.ToInt32(Console.ReadLine());
                switch (preference)
                {
                case 1:
                    Console.WriteLine("Enter Address Book Name: ");
                    string name = Console.ReadLine();
                    contactsCollection.CreateAddressBook(name);
                    List <Person> list = contactsCollection.GetAddressBook(name);
                    while (true)
                    {
                        Console.WriteLine("Enter Choice:");
                        Console.WriteLine("1.Add Contact \n 2.Edit Contact \n 3.Delete Contact \n 4.Exit");
                        int choice = Convert.ToInt32(Console.ReadLine());

                        switch (choice)
                        {
                        case 1:
                            contacts.AddContact();
                            break;

                        case 2:
                            contacts.Edit();
                            break;

                        case 3:
                            contacts.Delete();
                            break;

                        case 4:
                            break;
                        }
                        if (choice == 4)
                        {
                            break;
                        }
                    }
                    break;

                case 2:
                    contactsCollection.SearchPersonInSameCityOrState();

                    break;

                case 3:
                    contactsCollection.ViewPersonByCityOrState();
                    break;

                case 4:
                    contactsCollection.PrintNameInAlphabeticalOrder();
                    break;

                case 5:
                    contactsCollection.SortByCityStateOrZip();
                    break;

                case 6:
                    FileOperations.ReadFromStreamReader();
                    FileOperations.WriteUsingStreamWriter();
                    break;

                case 7:
                    c.WriteInCsvFile();
                    break;

                case 8:
                    c.ReadFromCsv();

                    break;

                case 9:
                    readCsvWriteJSON.WriteJson();
                    break;

                case 10:
                    break;
                }
                if (preference == 10)
                {
                    break;
                }
            }
        }
        public static void edit_data(List <Contacts> contacts)
        {
            int    choice = 0;
            string bname  = "";

            while (choice != 8)
            {
                List <Contacts> list = new List <Contacts>();     //here contact obj is stored temporarly, changes when edited and deleted
                int             flag = 0;
                Console.WriteLine("Enter the following choice");
                Console.WriteLine("1. Add Contacts");
                Console.WriteLine("2. Edit Contacts");
                Console.WriteLine("3. Delete Contacts");
                Console.WriteLine("4. Display Contacts(Sorted By Name)");
                Console.WriteLine("5. Display Contacts(Sorted By City)");
                Console.WriteLine("6. Display Contacts(Sorted By State)");
                Console.WriteLine("7. Display Contacts(Sorted By Zip)");
                Console.WriteLine("8. Exit");
                Console.WriteLine("Enter your choice: ");
                choice = Convert.ToInt32(Console.ReadLine());

                switch (choice)
                {
                case 1:
                    Console.WriteLine("Add New Contacts: ");
                    Console.WriteLine("Enter the firstname: ");
                    string first_name = Console.ReadLine();
                    Console.WriteLine("Enter the lastname: ");
                    string last_name = Console.ReadLine();
                    int    flag1     = 0;
                    foreach (Contacts ct in contacts)
                    {
                        if (ct.first_name.ToLower().Equals(first_name.ToLower()) && ct.last_name.ToLower().Equals(last_name.ToLower()))
                        {
                            Console.WriteLine("Entry of this name is already present. Please enter a new Name");
                            flag1 = 1;
                            break;
                        }
                    }
                    if (flag1 == 0)     //Allows if New Name is entered
                    {
                        Console.WriteLine("Enter the address: ");
                        string address = Console.ReadLine();
                        Console.WriteLine("Enter the city: ");
                        string city = Console.ReadLine();
                        Console.WriteLine("Enter the state: ");
                        string state = Console.ReadLine();
                        Console.WriteLine("Enter the zip: ");
                        int zip = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Enter the phone number");
                        long phone = Convert.ToInt64(Console.ReadLine());
                        Console.WriteLine("Enter the email: ");
                        string email = Console.ReadLine();

                        Contacts ct1 = new Contacts(first_name, last_name, address, city, state, zip, phone, email);
                        list.Add(ct1);
                        Console.WriteLine("Contact Added Successfully");
                    }
                    break;

                case 2:
                    Console.WriteLine("Enter the first name of the person: ");
                    string first = Console.ReadLine();
                    foreach (Contacts c in contacts)
                    {
                        if (c.first_name.Equals(first))
                        {
                            int n = 0;
                            while (n != 9)
                            {
                                Console.WriteLine("Enter the following choice");
                                Console.WriteLine("1. Edit First Name");
                                Console.WriteLine("2. Edit Last Name");
                                Console.WriteLine("3. Edit Address");
                                Console.WriteLine("4. Edit City");
                                Console.WriteLine("5. Edit State");
                                Console.WriteLine("6. Edit Zip");
                                Console.WriteLine("7. Edit Phone Number");
                                Console.WriteLine("8. Edit E-mail");
                                Console.WriteLine("9. Exit");
                                Console.WriteLine("Enter your choice: ");
                                n = Convert.ToInt32(Console.ReadLine());

                                switch (n)
                                {
                                case 1:
                                    Console.WriteLine("1. Edit First Name");
                                    string fname = Console.ReadLine();
                                    c.first_name = fname;
                                    Console.WriteLine("Edited Successfully");
                                    break;

                                case 2:
                                    Console.WriteLine("1. Edit Last Name");
                                    string lname = Console.ReadLine();
                                    c.last_name = lname;
                                    Console.WriteLine("Edited Successfully");
                                    break;

                                case 3:
                                    Console.WriteLine("1. Edit Address Name");
                                    string adrss = Console.ReadLine();
                                    c.address = adrss;
                                    Console.WriteLine("Edited Successfully");
                                    break;

                                case 4:
                                    Console.WriteLine("1. Edit City Name");
                                    string cty = Console.ReadLine();
                                    c.city = cty;
                                    Console.WriteLine("Edited Successfully");
                                    break;

                                case 5:
                                    Console.WriteLine("1. Edit State");
                                    string ste = Console.ReadLine();
                                    c.state = ste;
                                    Console.WriteLine("Edited Successfully");
                                    break;

                                case 6:
                                    Console.WriteLine("1. Edit Zip");
                                    int zp = Convert.ToInt32(Console.ReadLine());
                                    c.zip = zp;
                                    Console.WriteLine("Edited Successfully");
                                    break;

                                case 7:
                                    Console.WriteLine("1. Edit Phone Number");
                                    long no = Convert.ToInt64(Console.ReadLine());
                                    c.phone = no;
                                    Console.WriteLine("Edited Successfully");
                                    break;

                                case 8:
                                    Console.WriteLine("1. Edit Email");
                                    string mail = Console.ReadLine();
                                    c.first_name = mail;
                                    Console.WriteLine("Edited Successfully");
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("Enter a valid name");
                        }
                    }
                    break;

                case 3:
                    Console.WriteLine("Enter the first name of the person: ");
                    string          fst = Console.ReadLine();
                    List <Contacts> lst = new List <Contacts>();
                    foreach (Contacts c in contacts)
                    {
                        if (c.first_name.Equals(fst))
                        {
                            lst.Add(c);                 //we can't delete the object while iterating through the list, it leads to exception
                        }
                    }
                    contacts.RemoveAll(i => lst.Contains(i));
                    Console.WriteLine("Contact Removed Successfully");
                    break;

                case 4:
                    Contacts cts1 = new Contacts();
                    cts1.SortByName(contacts);

                    foreach (Contacts c in contacts)
                    {
                        Console.WriteLine(c);
                    }
                    break;

                case 5:
                    Contacts cts2 = new Contacts();
                    cts2.SortByCity(contacts);

                    foreach (Contacts c in contacts)
                    {
                        Console.WriteLine(c);
                    }
                    break;

                case 6:
                    Contacts cts3 = new Contacts();
                    cts3.SortByState(contacts);

                    foreach (Contacts c in contacts)
                    {
                        Console.WriteLine(c);
                    }
                    break;

                case 7:
                    Contacts cts4 = new Contacts();
                    cts4.SortByZip(contacts);

                    foreach (Contacts c in contacts)
                    {
                        Console.WriteLine(c);
                    }
                    break;
                }
                contacts.AddRange(list);    //first obj gets added into list in every iteration of while loop, stores into contacts list, then modifies content in case 2,3 through contacts list
            }
        }
Пример #3
0
        public static void Main(string[] args)
        {
            int               option, option1;
            string            bookName    = "Default";
            AddressBookBuider addressBook = new AddressBookBuider();

            Console.WriteLine("1. Would you like to work on existing addressbook ? if yes press 1");
            Console.WriteLine("2. Would you like to add  new addressbook ? if yes press 2");
            option1 = Convert.ToInt32(Console.ReadLine());
            switch (option1)
            {
            case 1:
                addressBook.AddAddressBook(bookName);
                break;

            case 2:
                Console.WriteLine("Enter Name Of New Addressbook You want to create : ");
                bookName = Console.ReadLine();
                addressBook.AddAddressBook(bookName);
                break;
            }
            do
            {
                Console.WriteLine($"you are working on {bookName} addressbook ");
                Console.WriteLine("Enter your option :");
                Console.WriteLine("1. Add new contact ");
                Console.WriteLine("2. Display the contacts");
                Console.WriteLine("3. Edit  contacts");
                Console.WriteLine("4. Delete  contacts");
                Console.WriteLine("5. Add new addressbook");
                Console.WriteLine("6. Switch Addressbook");
                Console.WriteLine("7. Search person in a city or State");
                Console.WriteLine("8. Get count of  persons by city or State");
                Console.WriteLine("9. Sort Entries by Person name");
                Console.WriteLine("10.Read or write addressbook contacts using File IO");
                Console.WriteLine("11.Read or write addressbook contacts using CSV file");
                Console.WriteLine("12.Read or write addressbook contacts using Json file");
                option = Convert.ToInt32(Console.ReadLine());

                switch (option)
                {
                case 1:
                    Console.WriteLine("Enter First Name :");
                    string firstName = Console.ReadLine();
                    Console.WriteLine("Enter Last Name :");
                    string   lastName       = Console.ReadLine();
                    Contacts duplicateCheck = new Contacts(firstName, lastName, null, null, null, null, 0, 0);
                    if (addressBook.CheckFor_Duplicate(duplicateCheck, bookName))
                    {
                        break;
                    }
                    Console.WriteLine("Enter Address :");
                    string address = Console.ReadLine();
                    Console.WriteLine("Enter City :");
                    string city = Console.ReadLine();
                    Console.WriteLine("Enter State :");
                    string state = Console.ReadLine();
                    Console.WriteLine("Enter Email :");
                    string email = Console.ReadLine();
                    Console.WriteLine("Enter Zip :");
                    int zip = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter Phone Number :");
                    long phoneNumber = Convert.ToInt64(Console.ReadLine());
                    addressBook.AddContact(firstName, lastName, address, city, state, email, zip, phoneNumber, bookName);
                    break;

                case 2:
                    addressBook.DisplayContact(bookName);
                    break;

                case 3:
                    Console.WriteLine("Enter Full Name of contact which to be edited");
                    string NameToEdit = Console.ReadLine();
                    addressBook.EditContact(NameToEdit, bookName);
                    break;

                case 4:
                    Console.WriteLine("Enter Full Name of contact which to be deleted");
                    string NameToDelete = Console.ReadLine();
                    addressBook.DeleteContact(NameToDelete, bookName);
                    break;

                case 5:
                    Console.WriteLine("Enter Name For New AddressBook");
                    string newAddressBook = Console.ReadLine();
                    addressBook.AddAddressBook(newAddressBook);
                    Console.WriteLine("Would you like to Switch to " + newAddressBook);
                    Console.WriteLine("1.Yes \n2.No");
                    int c = Convert.ToInt32(Console.ReadLine());
                    if (c == 1)
                    {
                        bookName = newAddressBook;
                    }
                    break;

                case 6:
                    Console.WriteLine("Enter Name Of AddressBook From Below List");
                    foreach (KeyValuePair <string, AddressBookBuider> item in addressBook.GetAddressBook())
                    {
                        Console.WriteLine(item.Key);
                    }
                    while (true)
                    {
                        bookName = Console.ReadLine();
                        if (addressBook.GetAddressBook().ContainsKey(bookName))
                        {
                            break;
                        }
                        else
                        {
                            Console.WriteLine("No such AddressBook found. Try Again.");
                        }
                    }
                    break;

                case 7:
                    Console.WriteLine("Would You Like To \n1.Search by city \n2.Search by state");
                    int opt = Convert.ToInt32(Console.ReadLine());
                    switch (opt)
                    {
                    case 1:
                        Console.WriteLine("Enter name of city :");
                        addressBook.SearchPersonByCity(Console.ReadLine());
                        break;

                    case 2:
                        Console.WriteLine("Enter name of state :");
                        addressBook.SearchPersonByState(Console.ReadLine());
                        break;

                    default:
                        Console.WriteLine("Invalid Input.Enter 1 or 2");
                        break;
                    }
                    break;

                case 8: addressBook.GetCountByCityOrState(bookName);
                    break;

                case 9: addressBook.SortEntryByName();
                    break;

                case 10:
                    ReadWriteFileIO fileIO = new ReadWriteFileIO();
                    fileIO.WriteToFile(addressBook.addressBookDictionary);
                    fileIO.ReadFromFile();
                    break;

                case 11:
                    CSVHandler handler = new CSVHandler();
                    handler.WriteToCsv(addressBook.addressBookDictionary);
                    handler.ReadFromCSV();
                    break;

                case 12: ReadWriteJson json = new ReadWriteJson();
                    json.WriteToFile(addressBook.addressBookDictionary);
                    json.ReadFromFile();
                    break;

                default:
                    Console.WriteLine("wrong input");
                    break;
                }
                Console.WriteLine("Do you want to continue to The Main Menu?  press 1 if yes,press 0 for Exit");
                option = Convert.ToInt32(Console.ReadLine());
            }while (option != 0);
        }
Пример #4
0
        public void ReadFromFile()
        {
            Contacts contact = JsonConvert.DeserializeObject <Contacts>(File.ReadAllText(filePath));

            Console.WriteLine(contact.ToString());
        }
        /// <summary>
        /// This method is used to edit person's details using their first name
        /// </summary>
        /// <param name="firstName"></param>
        public void Edit(string firstName)
        {
            int check = 0;

            for (int i = 0; i < this.list.Count; i++)
            {
                if (this.list[i].FirstName.Equals(firstName))
                {
                    while (check == 0)
                    {
                        Contacts addressBook = this.list[i];
                        Console.WriteLine(addressBook);
                        Console.WriteLine("Enter your choice for editing: ");
                        Console.WriteLine("1.Address 2.City 3.State 4.Zip Code 5.Phone Number 6.Email ID 7.Exit");
                        string choice = Console.ReadLine();
                        int    ch     = Convert.ToInt32(choice);
                        switch (ch)
                        {
                        case 1:
                            Console.WriteLine("Please enter new address : ");
                            string address = Console.ReadLine();
                            addressBook.Address = address;
                            break;

                        case 2:
                            Console.WriteLine("Please enter new city : ");
                            string city = Console.ReadLine();
                            addressBook.City = city;
                            break;

                        case 3:
                            Console.WriteLine("Please enter new state : ");
                            string state = Console.ReadLine();
                            addressBook.State = state;
                            break;

                        case 4:
                            Console.WriteLine("Please enter new zip code : ");
                            int zipCode = Convert.ToInt32(Console.ReadLine());
                            addressBook.ZipCode = zipCode;
                            break;

                        case 5:
                            Console.WriteLine("Please enter new mobile number : ");
                            long phoneNumber = Convert.ToInt64(Console.ReadLine());
                            addressBook.MobileNumber = phoneNumber;
                            break;

                        case 6:
                            Console.WriteLine("Please enter new email id  : ");
                            string emailID = Console.ReadLine();
                            addressBook.EmailID = emailID;
                            break;

                        case 7:
                            check = 1;
                            break;
                        }
                    }
                }
            }
        }