Example #1
0
        static void Main(string[] args)
        {
            var logger = GetLogger();

            using (var strategy = new LoggerFile("loggerFile.txt", false))
            {
                logger.ChangeStrategy(strategy);
                logger.Info("Logger was started");
                var addressBook = new AddressBook.AddressBook();
                addressBook.UserAdded   += HandleBookChanged;
                addressBook.UserRemoved += HandleBookChanged;
                TestAddressBook(addressBook);
                logger.Info("Logger finish work");
            }
        }
Example #2
0
        private static void TestAddressBook(AddressBook.AddressBook addressBook)
        {
            string id;
            User   user;
            var    logger = GetLogger();

            try
            {
                while (true)
                {
                    Console.WriteLine("Do you want to add or remove user? (a/r).");
                    var choise = Console.ReadLine();
                    switch (choise)
                    {
                    case "a":
                        logger.Info("User will be add...");
                        user = GetUser();
                        if (user == null)
                        {
                            logger.Warning("User is null");
                        }
                        addressBook.AddUser(user);
                        break;

                    case "r":
                        logger.Info("Remove user operation selected");
                        Console.Write("User id: ");
                        id = Console.ReadLine();
                        if (string.IsNullOrEmpty(id))
                        {
                            logger.Warning("User do not exist");
                        }
                        addressBook.RemoveUser(id);
                        break;

                    default:
                        logger.Error("Invalid operation");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
            }
        }
Example #3
0
 public Program()
 {
     book = new AddressBook();
 }
Example #4
0
        static void Main(string[] args)
        {
            AddressBook  ab = new AddressBook();
            List <Entry> _addressBookList = new List <Entry>();
            Entry        e = null;

            while (true)
            {
                try
                {
                    Console.WriteLine("Type 1 to add contact to address book");
                    Console.WriteLine("Type 2 to search contact in address book");
                    int input = int.Parse(Console.ReadLine());

                    switch (input)
                    {
                    case 1:

                        Console.WriteLine("type \"quit\" to exit");
                        Console.WriteLine("Please enter the first name: ");
                        string fn = Console.ReadLine();



                        Console.WriteLine("Please enter the last name: ");
                        string ln   = Console.ReadLine();
                        Name   name = new Name(fn, ln);


                        Console.WriteLine("Please enter street number: ");
                        int streetNumber = int.Parse(ab.ReplaceEmpty(Console.ReadLine()));


                        Console.WriteLine("Please enter the street name: ");
                        string streetName = ab.ReplaceEmpty(Console.ReadLine());


                        Console.WriteLine("Please the enter city: ");
                        string city = ab.ReplaceEmpty(Console.ReadLine());

                        Console.WriteLine("Please enter the country: ");
                        string  country = ab.ReplaceEmpty(Console.ReadLine());
                        Address address = new Address(streetNumber, streetName, city, country);

                        string phoneNumber = ab.GetPhoneNumber();


                        Console.WriteLine("Please enter email: ");
                        string email = Console.ReadLine() ?? "";
                        e = ab.GatherInfoForAddressBook(name, address, phoneNumber, email);
                        _addressBookList.Add(e);
                        break;



                    case 2:
                        //Console.WriteLine("list count" + _addressBookList.Count);

                        Console.WriteLine("Please enter the first name");

                        string firstName = Console.ReadLine();


                        ab.SearchContact(firstName, _addressBookList);
                        break;

                    default:
                        break;
                    }
                }

                catch (FormatException ex)
                {
                    Console.WriteLine("input is invalid");
                }
            }

            Console.ReadKey();
        }
        public static void Main(String[] args)
        {
            Console.WriteLine("Welcome in Address book System");
            Dictionary <string, AddressBook> abDict = new Dictionary <string, AddressBook>();
            bool ProgramIsRunning = true;

            Console.WriteLine("\nHow many address Book you want to create : ");
            int numAddressBooks = Convert.ToInt32(Console.ReadLine());

            for (int i = 1; i <= numAddressBooks; i++)
            {
                Console.WriteLine("Enter the name of address book " + i + ": ");
                string      bookName    = Console.ReadLine();
                AddressBook addressBook = new AddressBook();
                abDict.Add(bookName, addressBook);
            }
            Console.WriteLine("\nYou have created following Address Books : ");
            foreach (string k in abDict.Keys)
            {
                Console.WriteLine(k);
            }
            while (ProgramIsRunning)
            {
                Console.WriteLine("\nChoose option \n1.Add Contact \n2.Edit Contact \n3.Delete Contact  \n4.Display Contacts \n5.Search Person By City & State \n6.Display Contacts Same City \n7.Display Contacts Same State \n8.View number of contacts of city and state  \n9.Display Contacts in Sorted \n10.Display contact in sorted by state or by city \n11.File Operation \n12.Read Write Operation inCsv \n13.Read Write Operation in Json file \n14.Exit");
                int choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    Console.WriteLine("\nEnter Existing Address Book Name for adding contacts");
                    string contactName = Console.ReadLine();
                    if (abDict.ContainsKey(contactName))
                    {
                        Console.WriteLine("\nEnter the number of contacts you want to add in address book");
                        int numberOfContacts = Convert.ToInt32(Console.ReadLine());
                        for (int i = 1; i <= numberOfContacts; i++)
                        {
                            addContactBook(abDict[contactName]);
                        }
                        abDict[contactName].displayPerson();
                    }
                    else
                    {
                        Console.WriteLine("No AddressBook exist with name {0}", contactName);
                    }
                    break;

                case 2:
                    Console.WriteLine("Enter Address Book Name for edit contact");
                    string editcontactName = Console.ReadLine();
                    if (abDict.ContainsKey(editcontactName))
                    {
                        abDict[editcontactName].editPerson();
                        abDict[editcontactName].displayPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", editcontactName);
                    }
                    break;

                case 3:
                    Console.WriteLine("\nEnter Address Book Name for delete contact");
                    string deleteContact = Console.ReadLine();
                    if (abDict.ContainsKey(deleteContact))
                    {
                        abDict[deleteContact].deletePerson();
                        abDict[deleteContact].displayPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", deleteContact);
                    }
                    break;

                case 4:
                    Console.WriteLine("\nEnter Address Book Name for display contacts");
                    string displayContactsInAddressBook = Console.ReadLine();
                    abDict[displayContactsInAddressBook].displayPerson();
                    break;

                case 5:
                    Console.WriteLine("\n Enter address book name :");
                    string searchContacts = Console.ReadLine();
                    if (abDict.ContainsKey(searchContacts))
                    {
                        abDict[searchContacts].searchPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", searchContacts);
                    }
                    break;

                case 6:
                    Console.WriteLine("\n Enter address book name :");
                    string displayContacts = Console.ReadLine();
                    if (abDict.ContainsKey(displayContacts))
                    {
                        abDict[displayContacts].sameCityPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", displayContacts);
                    }
                    break;

                case 7:
                    Console.WriteLine("\n Enter address book name :");
                    string displayContacts2 = Console.ReadLine();
                    if (abDict.ContainsKey(displayContacts2))
                    {
                        abDict[displayContacts2].sameStatePerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", displayContacts2);
                    }
                    break;

                case 8:
                    Console.WriteLine("\n Enter address book name :For counting same city or state");
                    string displayContacts3 = Console.ReadLine();
                    if (abDict.ContainsKey(displayContacts3))
                    {
                        abDict[displayContacts3].findCountSameStateOrCityPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", displayContacts3);
                    }
                    break;

                case 9:
                    Console.WriteLine("\nEnter Address Book Name for display contacts in sorted order");
                    string nameAddressBook = Console.ReadLine();
                    abDict[nameAddressBook].displayPersonInOrder();
                    break;

                case 10:
                    Console.WriteLine("\nEnter Address Book Name for Sort contacts based on City or State");
                    string nameAddressBookforSorting = Console.ReadLine();
                    Console.WriteLine("\nChoose option for sorting \n1.By State \n2.By City");
                    int choiceSorting = Convert.ToInt32(Console.ReadLine());
                    switch (choiceSorting)
                    {
                    case 1:
                        abDict[nameAddressBookforSorting].displayPersonInOrderByCity();
                        break;

                    case 2:
                        abDict[nameAddressBookforSorting].displayPersonInOrderByState();
                        break;
                    }
                    break;

                case 11:
                    Console.WriteLine("chioce : \n1.Write Person detail in text file \n2 Read Person detail from text file");
                    int chooseOption = Convert.ToInt32(Console.ReadLine());
                    switch (chooseOption)
                    {
                    case 1:
                        Console.WriteLine("Enter Address Book name where you want to write person details");
                        string write = Console.ReadLine();
                        if (abDict.ContainsKey(write))
                        {
                            abDict[write].WritePersonDetailTextFile();
                        }
                        else
                        {
                            Console.WriteLine("No Address book exist with name {0} ", write);
                        }
                        break;

                    case 2:
                        Console.WriteLine("Enter Address Book name where you want to write person details");
                        string read = Console.ReadLine();
                        if (abDict.ContainsKey(read))
                        {
                            abDict[read].ReadPersonDetailTxtFile();
                        }
                        else
                        {
                            Console.WriteLine("No Address book exist with name {0} ", read);
                        }
                        break;

                    default:
                        Console.WriteLine("Please enter valid option only");
                        break;
                    }
                    break;

                case 12:
                    Console.WriteLine("chioce : \n1.Write Person detail in Csv file \n2 Read Person detail from Csv file");
                    int chooseOption2 = Convert.ToInt32(Console.ReadLine());
                    switch (chooseOption2)
                    {
                    case 1:
                        Console.WriteLine("Enter Address Book name where you want to write person details");
                        string write1 = Console.ReadLine();
                        if (abDict.ContainsKey(write1))
                        {
                            abDict[write1].WritePersonDetailCsvFile();
                        }
                        else
                        {
                            Console.WriteLine("No Address book exist with name {0} ", write1);
                        }
                        break;

                    case 2:
                        Console.WriteLine("Enter Address Book name where you want to write person details");
                        string read = Console.ReadLine();
                        if (abDict.ContainsKey(read))
                        {
                            abDict[read].ReadPersonDetailCsvFile();
                        }
                        else
                        {
                            Console.WriteLine("No Address book exist with name {0} ", read);
                        }
                        break;

                    default:
                        Console.WriteLine("Please enter valid option");
                        break;
                    }
                    break;

                case 13:
                    Console.WriteLine("chioce : \n1.Write Person detail in Json file \n2 Read Person detail from Json file");
                    int chooseOption3 = Convert.ToInt32(Console.ReadLine());
                    switch (chooseOption3)
                    {
                    case 1:
                        Console.WriteLine("Enter Address Book name where you want to write person details");
                        string write1 = Console.ReadLine();
                        if (abDict.ContainsKey(write1))
                        {
                            abDict[write1].WriteContactsInJSONFile();
                        }
                        else
                        {
                            Console.WriteLine("No Address book exist with name {0} ", write1);
                        }
                        break;

                    case 2:
                        Console.WriteLine("Enter Address Book name where you want to write person details");
                        string read = Console.ReadLine();
                        if (abDict.ContainsKey(read))
                        {
                            abDict[read].ReadContactsFronJSON();
                        }
                        else
                        {
                            Console.WriteLine("No Address book exist with name {0} ", read);
                        }
                        break;

                    default:
                        Console.WriteLine("Please enter valid option");
                        break;
                    }
                    break;

                case 14:
                    ProgramIsRunning = false;
                    break;

                default:
                    Console.WriteLine("Please enter valid option");
                    break;
                }
            }
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Address Book Program");
            AddressBook addressBook = new AddressBook();
            int         choice, choice2;
            string      bookName = "default";

            Console.WriteLine("Would You Like To \n1.Work on default AddressBook \n2.Create New AddressBook");
            choice2 = Convert.ToInt32(Console.ReadLine());
            switch (choice2)
            {
            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;

            default:
                Console.WriteLine("Invalid Input, Proceeding with default AddressBook");
                addressBook.AddAddressBook(bookName);
                break;
            }
            do
            {
                Console.WriteLine($"Working On {bookName} AddressBook\n");
                Console.WriteLine("Choose An Option \n1.Add New Contact \n2.Edit Existing Contact \n3.Delete A Contact \n4.View A Contact \n5.View All Contacts \n6.Add New AddressBook \n7.Switch AddressBook \n8.Search Contact by City/State \n9.Count by City/State \n10.Sort Entries \n11.Read/Write AddressBook to text file \n12.Read/Write AddressBook to csv file \n13.Read/Write AddressBook to JSON file \n0.Exit Application\n");
                choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    Console.WriteLine("Enter First Name :");
                    string firstName = Console.ReadLine();
                    Console.WriteLine("Enter Last Name :");
                    string  lastName = Console.ReadLine();
                    Contact temp     = new Contact()
                    {
                        FirstName = firstName,
                        LastName  = lastName
                    };
                    if (addressBook.CheckDuplicateEntry(temp, 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 = long.Parse(Console.ReadLine());
                    addressBook.AddContact(firstName, lastName, address, city, state, email, zip, phoneNumber, bookName);
                    break;

                case 2:
                    Console.WriteLine("Enter Full Name Of Contact To Edit :");
                    string nameToEdit = Console.ReadLine();
                    addressBook.EditContact(nameToEdit, bookName);
                    break;

                case 3:
                    Console.WriteLine("Enter Full Name Of Contact To Delete :");
                    string nameToDelete = Console.ReadLine();
                    addressBook.DeleteContact(nameToDelete, bookName);
                    break;

                case 4:
                    Console.WriteLine("Enter Full Name Of Contact To View :");
                    string nameToView = Console.ReadLine();
                    addressBook.ViewContact(nameToView, bookName);
                    break;

                case 5:
                    addressBook.ViewContact(bookName);
                    break;

                case 6:
                    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 7:
                    Console.WriteLine("Enter Name Of AddressBook From Below List");
                    foreach (KeyValuePair <string, AddressBook> 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 8:
                    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 9:
                    addressBook.DisplayCountByCityandState();
                    break;

                case 10:
                    Console.WriteLine("\n1.Sort By Name \n2.Sort By City \n3.Sort By State \n4.Sort By Zip");
                    int ch = Convert.ToInt32(Console.ReadLine());
                    switch (ch)
                    {
                    case 1:
                        addressBook.SortByName();
                        break;

                    case 2:
                        addressBook.SortByCity();
                        break;

                    case 3:
                        addressBook.SortByState();
                        break;

                    case 4:
                        addressBook.SortByZip();
                        break;

                    default:
                        Console.WriteLine("Invalid Choice");
                        break;
                    }
                    break;

                case 11:
                    FileIOOperation fileIO = new FileIOOperation();
                    fileIO.WriteToFile(addressBook.addressBookDictionary);
                    fileIO.ReadFromFile();
                    break;

                case 12:
                    CSVHandler handler = new CSVHandler();
                    handler.WriteToFile(addressBook.addressBookDictionary);
                    handler.ReadFromFile();
                    break;

                case 13:
                    JSONOperation json = new JSONOperation();
                    json.WriteToFile(addressBook.addressBookDictionary);
                    json.ReadFromFile();
                    break;

                case 0:
                    Console.WriteLine("Thank You For Using Address Book System.");
                    break;

                default:
                    Console.WriteLine("Invalid Entry. Enter value between 0 to 8");
                    break;
                }
            } while (choice != 0);
        }
Example #7
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Welcome in Address book System");

            ///create dictionary and
            ///Dict is name of dictionary
            Dictionary <string, AddressBook> abDict = new Dictionary <string, AddressBook>();//string is Tkey and AddressBook is TValue.
            bool ProgramIsRunning = true;

            Console.WriteLine("\nHow many address Book you want to create : ");
            int numAddressBooks = Convert.ToInt32(Console.ReadLine()); //store and convert into int using numAdressBooks variable.

            for (int i = 1; i <= numAddressBooks; i++)
            {
                Console.WriteLine("Enter the name of address book " + i + ": ");
                string      bookName    = Console.ReadLine();
                AddressBook addressBook = new AddressBook(); //creating object of AddressBook class
                abDict.Add(bookName, addressBook);           //add element in dictionary
            }
            Console.WriteLine("\nYou have created following Address Books : ");
            foreach (var item in abDict) //var is used and it is store any data type value.
            {
                Console.WriteLine("{0}", item.Key);
            }
            while (ProgramIsRunning)
            {
                Console.WriteLine("\nChoose option \n1.Add Contact \n2.Edit Contact \n3.Delete Contact  \n4.Display Contacts \n5.Search Person By City & State \n6.Display Contacts Same City \n7.Display Contacts Same State \n8.Exit");
                int choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    Console.WriteLine("\nEnter Existing Address Book Name for adding contacts");
                    string contactName = Console.ReadLine();
                    if (abDict.ContainsKey(contactName))
                    {
                        Console.WriteLine("\nEnter the number of contacts you want to add in address book");
                        int numberOfContacts = Convert.ToInt32(Console.ReadLine());
                        for (int i = 1; i <= numberOfContacts; i++)
                        {
                            addContactBook(abDict[contactName]);
                        }
                        abDict[contactName].displayPerson();
                    }
                    else
                    {
                        Console.WriteLine("No AddressBook exist with name {0}", contactName);
                    }
                    break;

                case 2:
                    Console.WriteLine("Enter Address Book Name for edit contact");
                    string editcontactName = Console.ReadLine();
                    if (abDict.ContainsKey(editcontactName))
                    {
                        abDict[editcontactName].editPerson();
                        abDict[editcontactName].displayPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", editcontactName);
                    }
                    break;

                case 3:
                    Console.WriteLine("\nEnter Address Book Name for delete contact");
                    string deleteContact = Console.ReadLine();
                    if (abDict.ContainsKey(deleteContact))
                    {
                        abDict[deleteContact].deletePerson();
                        abDict[deleteContact].displayPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", deleteContact);
                    }
                    break;

                case 4:
                    Console.WriteLine("\nEnter Address Book Name for display contacts");
                    string displayContactsInAddressBook = Console.ReadLine();
                    abDict[displayContactsInAddressBook].displayPerson();
                    break;

                case 5:
                    Console.WriteLine("\n Enter address book name :");
                    string searchContacts = Console.ReadLine();
                    if (abDict.ContainsKey(searchContacts))
                    {
                        abDict[searchContacts].searchPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", searchContacts);
                    }
                    break;

                case 6:
                    Console.WriteLine("\n Enter address book name :");
                    string displayContacts = Console.ReadLine();
                    if (abDict.ContainsKey(displayContacts))
                    {
                        abDict[displayContacts].sameCityPerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", displayContacts);
                    }
                    break;

                case 7:
                    Console.WriteLine("\n Enter address book name :");
                    string displayContacts2 = Console.ReadLine();
                    if (abDict.ContainsKey(displayContacts2))
                    {
                        abDict[displayContacts2].sameStatePerson();
                    }
                    else
                    {
                        Console.WriteLine("No Address book exist with name {0} ", displayContacts2);
                    }
                    break;

                case 8:
                    ProgramIsRunning = false;
                    break;

                default:
                    Console.WriteLine("Please enter valid option");
                    break;
                }
            }

            void addContactBook(AddressBook addressBook)
            {
                Console.WriteLine("Enter First Name : ");
                string firstName = Console.ReadLine();

                Console.WriteLine("Enter Last Name : ");
                string lastName = Console.ReadLine();

                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 Phone Number : ");
                long phoneNumber = Convert.ToInt64(Console.ReadLine());

                Console.WriteLine("Enter Email id :");
                string email = Console.ReadLine();

                addressBook.AddContact(firstName, lastName, address, city, state, phoneNumber, email);
            }
        }
        static void Main(string[] args)
        {
            AddressBook addressBook = new AddressBook();

            PromptUser();

            void Menu()
            {
                Console.WriteLine("TYPE:");
                Console.WriteLine("'Add' to add a contact: ");
                Console.WriteLine("'View' to view the list of contacts: ");
                Console.WriteLine("'Remove' to select and remove a contact: ");
                Console.WriteLine("'Update' to select and update a contact: ");
                Console.WriteLine("'Quit' at anytime to exit: ");
            }

            void UpdateAddressBook(string userInput)
            {
                string name    = "";
                string address = "";
                string zip     = "";
                string city    = "";
                string state   = "";
                string phone   = "";
                string email   = "";

                switch (userInput.ToLower())
                {
                case "add":
                    Console.Write("Enter a name: ");
                    name = Console.ReadLine().Trim();
                    Console.Write("Enter a zip: ");
                    name = Console.ReadLine().Trim();
                    Console.Write("Enter a city: ");
                    name = Console.ReadLine().Trim();
                    Console.Write("Enter a state: ");
                    name = Console.ReadLine().Trim();
                    Console.Write("Enter a phone: ");
                    name = Console.ReadLine().Trim();
                    Console.Write("Enter a email: ");
                    name = Console.ReadLine().Trim();


                    switch (name)
                    {
                    default:
                        break;
                    }
                    switch (zip)
                    {
                    default:
                        break;
                    }

                    switch (city)
                    {
                    case "quit":
                        break;


                    default:
                        Console.Write("Enter an address: ");
                        address = Console.ReadLine().Trim();
                        switch (address)
                        {
                        case "quit":
                            break;

                        default:
                            addressBook.AddEntry(name, address, zip, city, state, phone, email);
                            break;
                        }
                        break;
                    }
                    break;

                case "remove":
                    Console.Write("Enter a name to remove: ");
                    name = Console.ReadLine();
                    switch (name)
                    {
                    case "quit":
                        break;

                    default:
                        addressBook.RemoveEntry(name);
                        break;
                    }
                    break;

                case "view":
                    Console.WriteLine(addressBook.ViewContactsList());
                    break;

                case "update":
                    Console.WriteLine("Please enter the name of the Contact you wish to update");
                    name = Console.ReadLine();
                    addressBook.UpdateContact(name);
                    break;
                }
            }

            void PromptUser()
            {
                Menu();
                string userInput = "";

                while (userInput != "quit")
                {
                    Console.WriteLine("What would you like to do?");
                    userInput = Console.ReadLine().Trim();
                    UpdateAddressBook(userInput);
                }
            }
        }
Example #9
0
        static void Main(string[] args)
        {
            AddressBook.AddressBook Book = AddressBook.AddressBook.Instance(); //singleton
            object[] obj  = new object[] { "Vailiev", "Ivan", new DateTime(1999, 10, 11), new DateTime(2006, 10, 10), "Kyiv", "no address", "0854751145", "male", "*****@*****.**" };
            object[] obj2 = new object[] { "Shulga", "Dmutro", new DateTime(2001, 10, 11), new DateTime(2006, 10, 10), "Kyiv", "no address", "0854751145", "male", "*****@*****.**" };
            object[] obj3 = new object[] { "Kolesnik", "Anna", new DateTime(1993, 1, 11), new DateTime(2016, 5, 24), "Odessa", "no address", "0854751145", "female", "*****@*****.**" };

            Book.UserAdded   += new BookEvent(Added);
            Book.UserDeleted += new BookEvent(Deleted);

            Book.AddUser(obj);
            Book.AddUser(obj2);
            Book.AddUser(obj3);
            Book.AddUser("Ivanova", "Olga", new DateTime(1995, 1, 3), new DateTime(2016, 5, 28), "Lviv", "no address", "0954569874", "female", "*****@*****.**");
            Console.WriteLine(new string('-', 50));

            Book.SelectByEmail();
            Console.WriteLine(new string('-', 50));

            var AdultsFromKyiv = Book.SelectAdultsExt();

            foreach (var item in AdultsFromKyiv)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(new string('-', 50));

            Book.SelectByGender();
            Console.WriteLine(new string('-', 50));

            var SelectJanuaryTemp = Book.SelectByJanuary();

            foreach (var item in SelectJanuaryTemp)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(new string('-', 50));

            Dictionary <string, IEnumerable> dictionary = Book.SelectDictionary();
            var dictionaryTemp = dictionary["man"];

            foreach (var item in dictionaryTemp)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(new string('-', 50));

            var SelectRangeTemp = Book.SelectRange(1, 4);

            foreach (var item in SelectRangeTemp)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(new string('-', 50));

            Console.WriteLine("The number of people whose birthday is today: " + Book.SelectBirthdateCount("Kyiv"));
            Console.WriteLine(new string('-', 50));
            Book.RemoveUser((User)obj);
            Book.RemoveUser(0); // via index
            Console.ReadKey();
        }
Example #10
0
        /// <summary>
        /// Main program to call different methods
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Address Book Management System");
            //creating object for Address Book Class
            AddressBook addressBook = new AddressBook();
            bool        flag        = true;

            while (flag)
            {
                //Taking input for user to determine task to do
                //passing the input to switch case
                //Calling the methods from Address Book accordingly
                Console.WriteLine("\nEnter 1 to add New Address Book ");
                Console.WriteLine("Enter 2 to Add Contacts");
                Console.WriteLine("Enter 3 to Edit Contacts");
                Console.WriteLine("Enter 4 to Delete Contacts");
                Console.WriteLine("Enter 5 to display all the addressbooks and contact details");
                Console.WriteLine("Enter 6 to delete address book");
                Console.WriteLine("Enter 7 to Search Contact Details using City");
                Console.WriteLine("Enter 8 to search Contact Details using state");
                Console.WriteLine("Enter 9 to view contact details and count with city");
                Console.WriteLine("Enter 10 to view contact details and count with state");
                Console.WriteLine("Enter any other key to exit");


                string options = Console.ReadLine();
                switch (options)
                {
                case "1":
                    addressBook.AddAdressBook();
                    //addressBook.DisplayingAddressBooks();
                    break;

                case "2":
                    addressBook.AddContactsInAddressBook();
                    addressBook.DisplayingAddressBooks();
                    break;

                case "3":
                    addressBook.EditDetailsOfAddressBook();
                    addressBook.DisplayingAddressBooks();
                    break;

                case "4":
                    addressBook.DeleteContactsOfAddressBook();
                    addressBook.DisplayingAddressBooks();
                    break;

                case "5":
                    addressBook.DisplayingAddressBooks();
                    break;

                case "6":
                    addressBook.DeletingAddressBook();
                    break;

                case "7":
                    addressBook.SearchingByCity();
                    break;

                case "8":
                    addressBook.SearchingByState();
                    break;

                case "9":
                    addressBook.GettingCityNames();
                    addressBook.CreatingCityDictionary();
                    addressBook.ViewingCityDictionary();
                    break;

                case "10":
                    addressBook.GettingStateNames();
                    addressBook.CreatingStateDictionary();
                    addressBook.ViewingStateDictionary();
                    break;

                default:
                    flag = false;
                    break;
                }
            }
        }
        /*
         *  1. Add the required classes to make the following code compile.
         *  HINT: Use a Dictionary in the AddressBook class to store Contacts. They keys should be the contact email.
         *
         *  2. Run the program and observe the exception.
         *
         *  3. Add try/catch blocks in the appropriate locations to prevent the program from crashing
         *      Print meaningful error messages in the catch blocks.
         */

        static void Main(string[] args)
        {
            // Create a few contacts
            Contact bob = new Contact()
            {
                FirstName = "Bob",
                LastName  = "Smith",
                Email     = "*****@*****.**",
                Address   = "100 Some Ln, Testville, TN 11111"
            };
            Contact sue = new Contact()
            {
                FirstName = "Sue",
                LastName  = "Jones",
                Email     = "*****@*****.**",
                Address   = "322 Hard Way, Testville, TN 11111"
            };
            Contact juan = new Contact()
            {
                FirstName = "Juan",
                LastName  = "Lopez",
                Email     = "*****@*****.**",
                Address   = "888 Easy St, Testville, TN 11111"
            };


            // Create an AddressBook and add some contacts to it
            AddressBook addressBook = new AddressBook();

            addressBook.AddContact(bob);
            addressBook.AddContact(sue);
            addressBook.AddContact(juan);

            // Try adding a contact a second time
            addressBook.AddContact(sue);


            // Create a list of emails that match our Contacts
            List <string> emails = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            };

            // Insert an email that does NOT match a Contact
            emails.Insert(1, "*****@*****.**");

            //  Search the AddressBook by email and print the information about each Contact
            foreach (string email in emails)
            {
                // Try to print contact list by using GetByEmail method
                // Catch email that does not match contact and print custom error message
                try
                {
                    Contact contact = addressBook.GetByEmail(email);
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Name: {contact.FullName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                catch (KeyNotFoundException)
                {
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Employee with email {email} does not exist.");
                }
            }
        }
 public ABEngine()
 {
     this.renderer = new Renderer();
     this.book = new AddressBook();
     this.userInterface = new KeyboardInterface();
 }
Example #13
0
        static void Main(string[] args)
        {
            AddressBookBinder binder = new AddressBookBinder();

            Console.WriteLine("Welcome to Address Book Program");
            int result = 1;

            while (result == 1)
            {
                Console.WriteLine("Enter the name of the Address Book to be used");
                string      addrName = Console.ReadLine();
                AddressBook book     = new AddressBook();
                book.People = binder.AddAddrBook(addrName, book.People);
                int loop = 1;
                while (loop == 1)
                {
                    Console.WriteLine("\nSelect the option. \n1. Add new contact. \n2. Edit existing contact.\n3. Delete Contact \n4. Search By City \n5. Count citywise contacts \n6. Display Alphabetically \n7. Sort By Zipcode \n8. Sort By City \n9. Sort By State \n10. Exit.");
                    int option = int.Parse(Console.ReadLine());
                    switch (option)
                    {
                    case 1:
                        Console.WriteLine("Enter the person details to be added in the address book");
                        Console.WriteLine("First Name");
                        string FirstName = Console.ReadLine();
                        Console.WriteLine("Last Name");
                        string LastName = Console.ReadLine();
                        Console.WriteLine("Address");
                        string Address = Console.ReadLine();
                        Console.WriteLine("City");
                        string City = Console.ReadLine();
                        Console.WriteLine("State");
                        string State = Console.ReadLine();
                        Console.WriteLine("Zip code");
                        string ZipCode = Console.ReadLine();
                        Console.WriteLine("Phone Number");
                        string PhoneNumber = Console.ReadLine();
                        Console.WriteLine("Email");
                        string Email = Console.ReadLine();
                        if (book.AddContact(FirstName, LastName, Address, City, State, ZipCode, PhoneNumber, Email))
                        {
                            Console.WriteLine("Contact added successfully");
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Contact already exists");
                            break;
                        }

                    case 2:
                        Console.WriteLine("Enter the first name of the contact to be edited ");
                        string  name = Console.ReadLine();
                        Contact c    = book.FindContact(name);
                        if (c == null)
                        {
                            Console.WriteLine("Address for {0} count not be found.", name);
                            break;
                        }
                        else
                        {
                            Console.WriteLine("New Last Name");
                            c.LastName = Console.ReadLine();
                            Console.WriteLine("New Address");
                            c.Address = Console.ReadLine();
                            Console.WriteLine("New City");
                            c.City = Console.ReadLine();
                            Console.WriteLine("New State");
                            c.State = Console.ReadLine();
                            Console.WriteLine("New Zip code");
                            c.ZipCode = Console.ReadLine();
                            Console.WriteLine("New Phone Number");
                            c.PhoneNumber = Console.ReadLine();
                            Console.WriteLine("New Email");
                            c.Email = Console.ReadLine();
                            Console.WriteLine("Details updated for " + name);
                            break;
                        }

                    case 3:
                        Console.WriteLine("Enter the first name of the contact to be deleted ");
                        string name1 = Console.ReadLine();
                        if (book.RemoveContact(name1))
                        {
                            Console.WriteLine("Contact removed successfully");
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Contact not found");
                            break;
                        }

                    case 4:
                        binder.CreateDictionary();
                        Console.WriteLine("Enter city whose contacts need to be searched");
                        string city = Console.ReadLine();
                        foreach (Contact contact in binder.CityDictionary[city])
                        {
                            Console.WriteLine(contact.FirstName + "\t" + contact.LastName + "\t" + contact.Address + "\t" + contact.City + "\t" + contact.State + "\t" + contact.ZipCode + "\t" + contact.PhoneNumber + "\t" + contact.Email);
                        }
                        break;

                    case 5:
                        binder.CreateDictionary();
                        foreach (var key in binder.CityDictionary.Keys)
                        {
                            Console.WriteLine(key + "\t" + binder.CityDictionary[key].Count);
                        }
                        break;

                    case 6:
                        book.AlphabeticallyArrange();
                        break;

                    case 7:
                        book.SortByPincode();
                        break;

                    case 8:
                        book.SortByCity();
                        break;

                    case 9:
                        book.SortByState();
                        break;

                    case 10:
                        loop = 0;
                        break;
                    }
                    binder.Binder[addrName] = (book.People);
                }
                Console.WriteLine("Do you want to enter an address book. \n1. yes \n2. no");
                result = int.Parse(Console.ReadLine());
            }
            foreach (var key in binder.Binder.Keys)
            {
                foreach (Contact c in binder.Binder[key])
                {
                    data.Add(c);
                }
            }
            ReadWrite.WriteToJsonFile(data);
            ReadWrite.ReadJsonFile();
        }
Example #14
0
        /*
         *  1. Add the required classes to make the following code compile.
         *  HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address.
         *
         *  2. Run the program and observe the exception.
         *
         *  3. Add try/catch blocks in the appropriate locations to prevent the program from crashing
         *      Print meaningful error messages in the catch blocks.
         */

        static void Main(string[] args)
        {
            // Create a few contacts
            Contact Frank = new Contact()
            {
                FirstName = "Frank",
                LastName  = "Brown",
                Email     = "*****@*****.**",
                Address   = "100 BS Ln, Nashville, TN 15551"
            };
            Contact Mary = new Contact()
            {
                FirstName = "Maru",
                LastName  = "Jones",
                Email     = "*****@*****.**",
                Address   = "322 soft ln, Testville, TN 55555"
            };
            Contact Beevis = new Contact()
            {
                FirstName = "Beevis",
                LastName  = "Lopez",
                Email     = "*****@*****.**",
                Address   = "888 sleasy St, Cashville, TN 55555"
            };

            // Create an AddressBook and add some contacts to it
            AddressBook addressBook = new AddressBook();

            addressBook.AddContact(Frank);
            addressBook.AddContact(Mary);
            addressBook.AddContact(Beevis);

            // Try to addd a contact a second time
            addressBook.AddContact(Mary);
            addressBook.AddContact(Frank);
            addressBook.AddContact(Beevis);

            // Create a list of emails that match our Contacts
            List <string> emails = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            };

            // Insert an email that does NOT match a Contact
            emails.Insert(1, "*****@*****.**");

            //  Search the AddressBook by email and print the information about each Contact
            foreach (string email in emails)
            {
                try
                {
                    Contact contact = addressBook.getByEmail(email);
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Name: {contact.FullName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                catch (NullReferenceException)
                {
                    Console.WriteLine("Contact not in address book!!!!");
                }
            }
        }
Example #15
0
        /*
         *  1. Add the required classes to make the following code compile.
         *  HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address.
         *
         *  2. Run the program and observe the exception.
         *
         *  3. Add try/catch blocks in the appropriate locations to prevent the program from crashing
         *      Print meaningful error messages in the catch blocks.
         */

        static void Main(string[] args)
        {
            // Create a few contacts
            Contact bob = new Contact()
            {
                FirstName = "Bob", LastName = "Smith",
                Email     = "*****@*****.**",
                Address   = "100 Some Ln, Testville, TN 11111"
            };
            Contact sue = new Contact()
            {
                FirstName = "Sue", LastName = "Jones",
                Email     = "*****@*****.**",
                Address   = "322 Hard Way, Testville, TN 11111"
            };
            Contact juan = new Contact()
            {
                FirstName = "Juan", LastName = "Lopez",
                Email     = "*****@*****.**",
                Address   = "888 Easy St, Testville, TN 11111"
            };

            // Create an AddressBook and add some contacts to it
            AddressBook addressBook = new AddressBook();

            addressBook.AddContact(bob);
            addressBook.AddContact(sue);
            addressBook.AddContact(juan);

            // Try to add a contact a second time
            try {
                addressBook.AddContact(sue);
            }
            catch (ArgumentException ex) {
                Console.WriteLine(ex.Message);
            }

            // Create a list of emails that match our Contacts
            List <string> emails = new List <string> ()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            };

            // Insert an email that does NOT match a Contact
            emails.Insert(1, "*****@*****.**");

            //  Search the AddressBook by email and print the information about each Contact

            /*
             * foreach (string email in emails)
             * {
             *      Contact contact = addressBook.GetByEmail (email);
             *  if (email == contact.Email) {
             *      try {
             *          Console.WriteLine ("----------------------------");
             *          Console.WriteLine ($"Name: {contact.FullName}");
             *          Console.WriteLine ($"Email: {contact.Email}");
             *          Console.WriteLine ($"Address: {contact.Address}");
             *      } catch (NullReferenceException ex) {
             *          Console.WriteLine ($"No Contact Found: {ex.Message}");
             *      }
             *  }
             * }
             */
            foreach (string email in emails)
            {
                Console.WriteLine("----------------------------");
                try
                {
                    Contact contact = addressBook.GetByEmail(email);
                    Console.WriteLine($"Name: {contact.FullName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                catch (KeyNotFoundException ex)
                {
                    Console.WriteLine($"contact not found {ex.Message}");
                }
            }
        }
Example #16
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Address Book Management System");
            //contactPersonInformation.AddingContactDetails("Lakshay", "Garg", "a", "b", "d", 3,5, "lakshay.garg");
            ContactPersonInformation contactPersonInformation = new ContactPersonInformation();
            AddressBook addressBook = new AddressBook();
            bool        flag        = true;

            while (flag)
            {
                Console.WriteLine("\nEnter 1 to add New Address Book \nEnter 2 to Add Contacts \nEnter 3 to Edit Contacts \nEnter 4 to Delete Contacts\nEnter 5 to display all the addressbooks and contact details\nEnter 6 to delete address book\nEnter any other key to exit");
                string options = Console.ReadLine();
                switch (options)
                {
                case "1":
                    addressBook.AddAdressBook();
                    //addressBook.DisplayingAddressBooks();
                    break;

                case "2":
                    addressBook.AddContactsInAddressBook();
                    addressBook.DisplayingAddressBooks();
                    break;

                case "3":
                    addressBook.EditDetailsOfAddressBook();
                    addressBook.DisplayingAddressBooks();
                    break;

                case "4":
                    addressBook.DeleteContactsOfAddressBook();
                    addressBook.DisplayingAddressBooks();
                    break;

                case "5":
                    addressBook.DisplayingAddressBooks();
                    break;

                case "6":
                    addressBook.DeletingAddressBook();
                    break;

                default:
                    flag = false;
                    break;
                }
            }

            /*contactPersonInformation.AddingContactDetails();
             * contactPersonInformation.DisplayContactDetails();
             * // AddressBook addressBook = new AddressBook();
             * //addressBook.DisplayAddressBook();
             * Console.WriteLine("Do you want to edit any details, enter y to edit.");
             * string input = Console.ReadLine();
             * if(input.ToLower()=="y")
             * {
             *  contactPersonInformation.EditingContactDetails();
             *  contactPersonInformation.DisplayContactDetails();
             *
             * }
             * Console.WriteLine("Do you want to delete anything from contact details, press y for delete");
             * string inputForDelete = Console.ReadLine();
             * if(inputForDelete.ToLower()=="y"|| inputForDelete=="Y")
             * {
             *  contactPersonInformation.DeletingContactDetails();
             *  contactPersonInformation.DisplayContactDetails();
             * }*/
        }
Example #17
0
        /*
         *  1. Add the required classes to make the following code compile.
         *  HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address.
         *
         *  2. Run the program and observe the exception.
         *
         *  3. Add try/catch blocks in the appropriate locations to prevent the program from crashing
         *      Print meaningful error messages in the catch blocks.
         */

        static void Main(string[] args)
        {
            // Create a few contacts
            Contact bob = new Contact();

            bob.FirstName = "Bob";
            bob.LastName  = "Smith";
            bob.Email     = "*****@*****.**";
            bob.Address   = "100 Some Ln, Testville, TN 11111";

            Contact sue = new Contact();

            sue.FirstName = "Sue";
            sue.LastName  = "Jones";
            sue.Email     = "*****@*****.**";
            sue.Address   = "322 Hard Way, Testville, TN 11111";

            Contact juan = new Contact();

            juan.FirstName = "Juan";
            juan.LastName  = "Lopez";
            juan.Email     = "*****@*****.**";
            juan.Address   = "888 Easy St, Testville, TN 11111";


            // Create an AddressBook and add some contacts to it
            AddressBook addressBook = new AddressBook();

            addressBook.AddContact(bob);
            addressBook.AddContact(sue);
            addressBook.AddContact(juan);

            // Try to add a contact a second time
            try
            {
                addressBook.AddContact(sue);
            }
            catch
            {
                Console.WriteLine("There was a problem adding contacts to the address book.");
            }


            // Create a list of emails that match our Contacts
            List <string> emails = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            };

            // Insert an email that does NOT match a Contact
            emails.Insert(1, "*****@*****.**");

            //  Search the AddressBook by email and print the information about each Contact
            foreach (string email in emails)
            {
                try
                {
                    Contact contact = addressBook.GetByEmail(email);
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Name: {contact.FullName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                catch
                {
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Did not find a contact with an email of {email}.");
                }
            }
        }
        /*
         *  1. Add the required classes to make the following code compile.
         *  HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address.
         *
         *  2. Run the program and observe the exception.
         *
         *  3. Add try/catch blocks in the appropriate locations to prevent the program from crashing
         *      Print meaningful error messages in the catch blocks.
         */

        static void Main(string[] args)
        {
            // Create a few contacts
            List <Contact> contacts = new List <Contact>()
            {
                new Contact()
                {
                    FirstName = "Bob", LastName = "Smith",
                    Email     = "*****@*****.**",
                    Address   = "100 Some Ln, Testville, TN 11111"
                },
                new Contact()
                {
                    FirstName = "Sue", LastName = "Jones",
                    Email     = "*****@*****.**",
                    Address   = "322 Hard Way, Testville, TN 11111"
                },
                new Contact()
                {
                    FirstName = "Juan", LastName = "Lopez",
                    Email     = "*****@*****.**",
                    Address   = "888 Easy St, Testville, TN 11111"
                }
            };

            // Create an AddressBook and add some contacts to it

            Dictionary <string, double> AddressDictionary = new Dictionary <string, double>()
            {
                { "Old Fashioned", 20.30 }, { "Plain Glazed", 1000.00 }, { "Chocolate", 55.45 }
            };

            AddressBook addressBook = new AddressBook();

            addressBook.AddContact(bob);
            addressBook.AddContact(sue);
            addressBook.AddContact(juan);

            // Try to add a contact a second time
            addressBook.AddContact(sue);
            addressBook.AddContact(bob);

            // // Create a list of emails that match our Contacts
            // List<string> emails = new List<string>()
            // {
            //     "*****@*****.**",
            //     "*****@*****.**",
            //     "*****@*****.**",
            // };

            // // Insert an email that does NOT match a Contact
            // emails.Insert(1, "*****@*****.**");

            // //  Search the AddressBook by email and print the information about each Contact
            // foreach (string email in emails)
            // {
            //     Contact contact = addressBook.GetByEmail(email);
            //     Console.WriteLine("----------------------------");
            //     Console.WriteLine($"Name: {contact.FullName}");
            //     Console.WriteLine($"Email: {contact.Email}");
            //     Console.WriteLine($"Address: {contact.Address}");
            // }
        }
Example #19
0
        /*
         *  1. Add the required classes to make the following code compile.
         *  HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address.
         *
         *  2. Run the program and observe the exception.
         *
         *  3. Add try/catch blocks in the appropriate locations to prevent the program from crashing
         *      Print meaningful error messages in the catch blocks.
         */

        public static void Main(string[] args)
        {
            // Create a few contacts
            Contact bob = new Contact()
            {
                FirstName = "Bob", LastName = "Smith",
                Email     = "*****@*****.**",
                Address   = "100 Some Ln, Testville, TN 11111"
            };
            Contact sue = new Contact()
            {
                FirstName = "Sue", LastName = "Jones",
                Email     = "*****@*****.**",
                Address   = "322 Hard Way, Testville, TN 11111"
            };
            Contact juan = new Contact()
            {
                FirstName = "Juan", LastName = "Lopez",
                Email     = "*****@*****.**",
                Address   = "888 Easy St, Testville, TN 11111"
            };

            // Create an AddressBook and add some contacts to it
            AddressBook addressBook = new AddressBook();

            addressBook.AddContact(bob);
            addressBook.AddContact(sue);
            addressBook.AddContact(juan);

            // Try to add a contact a second time (try/catch)
            try
            {
                addressBook.AddContact(sue);
            }

            catch (ArgumentException ex)
            {
                Console.WriteLine("tried to add same contact twice");
            }

            //end of try/catch
            // Create a list of emails that match our Contacts
            List <string> emails = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            };

            // Insert an email that does NOT match a Contact
            // Try to add a contact a second time (try/catch)

            emails.Insert(1, "*****@*****.**");

            //  Search the AddressBook by email and print the information about each Contact
            foreach (string email in emails)
            {
                try
                {
                    Contact contact = addressBook.GetByEmail(email);

                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Name: {contact.FullName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                catch (KeyNotFoundException ex)
                {
                    Console.WriteLine("Email Does Not Match Contact");
                }
                //END OF SECOND ERROR TRY CATCH
            }
        }
Example #20
0
        /*
         *  1. Add the required classes to make the following code compile.
         *  HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address.
         *
         *  2. Run the program and observe the exception.
         *
         *  3. Add try/catch blocks in the appropriate locations to prevent the program from crashing
         *      Print meaningful error messages in the catch blocks.
         */

        static void Main(string[] args)
        {
            // Create a few contacts
            Contact bob = new Contact()
            {
                FirstName = "Bob",
                LastName  = "Smith",
                Email     = "*****@*****.**",
                Address   = "100 Some Ln, Testville, TN 11111"
            };

            Contact sue = new Contact()
            {
                FirstName = "Sue",
                LastName  = "Jones",
                Email     = "*****@*****.**",
                Address   = "322 Hard Way, Testville, TN 11111"
            };

            Contact juan = new Contact()
            {
                FirstName = "Juan",
                LastName  = "Lopez",
                Email     = "*****@*****.**",
                Address   = "888 Easy St, Testville, TN 11111"
            };


            // Create an AddressBook and add some contacts to it
            AddressBook addressBook = new AddressBook();

            addressBook.AddContact(bob);
            addressBook.AddContact(sue);
            addressBook.AddContact(juan);

            // Try to addd a contact a second time
            // try
            // {
            //     addressBook.AddContact(sue);
            // }
            // catch (ArgumentException ex)
            // {
            //     Console.WriteLine($"ERROR: Could not add contact a second time.{ex}");
            // }


            // Create a list of emails that match our Contacts
            List <string> emails = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            };

            foreach (String email in emails)
            {
                Console.WriteLine($"{email}");
            }

            // Insert an email that does NOT match a Contact
            try
            {
                emails.Insert(1, "*****@*****.**");
                Console.WriteLine("Success");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"ERROR: Unable to insert new email.{ex}");
            }

            foreach (string email in emails)
            {
                try
                {
                    Contact contact = addressBook.GetByEmail(email);
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Name: {contact.FirstName} {contact.LastName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                catch
                {
                    Console.WriteLine($"Unable to find contact data matching {email}");
                }
            }


            // //  Search the AddressBook by email and print the information about each Contact
            // foreach (string email in emails)
            // {
            //     Contact contact = addressBook.GetByEmail(email);
            //     Console.WriteLine("----------------------------");
            //     Console.WriteLine($"Name: {contact.FullName}");
            //     Console.WriteLine($"Email: {contact.Email}");
            //     Console.WriteLine($"Address: {contact.Address}");
            // }
        }
        /*
         *  1. Add the required classes to make the following code compile.
         *  HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address.
         *
         *  2. Run the program and observe the exception.
         *
         *  3. Add try/catch blocks in the appropriate locations to prevent the program from crashing
         *      Print meaningful error messages in the catch blocks.
         */

        static void Main(string[] args)
        {
            // Create a few contacts
            Contact bob = new Contact()
            {
                FirstName = "Bob",
                LastName  = "Smith",
                Email     = "*****@*****.**",
                Address   = "100 Some Ln, Testville, TN 11111"
            };
            Contact sue = new Contact()
            {
                FirstName = "Sue",
                LastName  = "Jones",
                Email     = "*****@*****.**",
                Address   = "322 Hard Way, Testville, TN 11111"
            };
            Contact juan = new Contact()
            {
                FirstName = "Juan",
                LastName  = "Lopez",
                Email     = "*****@*****.**",
                Address   = "888 Easy St, Testville, TN 11111"
            };


            // Create an AddressBook and add some contacts to it
            AddressBook addressBook = new AddressBook();

            addressBook.AddContact(bob);
            addressBook.AddContact(sue);
            addressBook.AddContact(juan);

            // Try to addd a contact a second time
            addressBook.AddContact(sue);


            // Create a list of emails that match our Contacts
            List <string> emails = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
            };

            // Insert an email that does NOT match a Contact
            emails.Insert(1, "*****@*****.**");


            //  Search the AddressBook by email and print the information about each Contact
            foreach (string email in emails)
            {
                Contact contact = addressBook.GetByEmail(email);
                if (contact != null)
                {
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Name: {contact.FullName}");
                    Console.WriteLine($"Email: {contact.Email}");
                    Console.WriteLine($"Address: {contact.Address}");
                }
                else
                {
                    Console.WriteLine("----------------------------");
                    Console.WriteLine($"Contact is null for {email}");
                }
            }
        }
Example #22
0
        static void Main(string[] args)
        {
            string path = @"C:\Users\RUMANA\source\repos\AddressBook\AddressBook\CSV\export.json";

            /*using (StreamWriter sw = new StreamWriter(path))
             * {
             *  string text = "name, address, city, state, zip, phoneNo, email";
             *  sw.WriteLine(text, path);
             * }*/
            int         choice = 0;
            AddressBook ab     = new AddressBook();

            do
            {
                Console.WriteLine("Enter your choice :");
                Console.WriteLine("1. Add Contact.");
                Console.WriteLine("2. View all Contacts.");
                Console.WriteLine("3.Edit existing contacts.");
                Console.WriteLine("4.Remove a contact.");
                Console.WriteLine("5.View AddressBook for a key name.");
                Console.WriteLine("6.Search person by city/state name.");
                Console.WriteLine("7.View persons by city.");
                Console.WriteLine("8.View persons by state");
                Console.WriteLine("9.Print all contacts from text file.");
                Console.WriteLine("10.Exit.");
                choice = Convert.ToInt32(Console.ReadLine());

                if (choice == 1)
                {
                    Console.WriteLine("Enter your Name : ");
                    String name = Console.ReadLine();
                    Regex  reg4 = new Regex(@"(^[a-z A-Z]*$)");
                    while (!reg4.IsMatch(name))
                    {
                        Console.WriteLine("Enter a valid name : ");
                        name = Console.ReadLine();
                    }
                    while (ab.UC7_CheckForDuplicateEntry(name))
                    {
                        Console.WriteLine("This name already exists in the address book.");
                        Console.WriteLine("Please enter a new name : ");
                        name = Console.ReadLine();
                        while (!reg4.IsMatch(name))
                        {
                            Console.WriteLine("Enter a valid name : ");
                            name = Console.ReadLine();
                        }
                    }
                    Console.WriteLine("Enter your address : ");
                    String address = Console.ReadLine();
                    Regex  reg5    = new Regex(@"(^[a-z A-Z]*$)");
                    while (!reg5.IsMatch(address))
                    {
                        Console.WriteLine("Enter a valid address : ");
                        address = Console.ReadLine();
                    }
                    Console.WriteLine("Enter your city : ");
                    String city = Console.ReadLine();
                    Regex  reg6 = new Regex(@"(^[a-z A-Z]*$)");
                    while (!reg6.IsMatch(city))
                    {
                        Console.WriteLine("Enter a valid city name : ");
                        city = Console.ReadLine();
                    }
                    Console.WriteLine("Enter your state : ");
                    String state = Console.ReadLine();
                    Regex  reg7  = new Regex(@"(^[a-z A-Z]*$)");
                    while (!reg7.IsMatch(state))
                    {
                        Console.WriteLine("Enter a valid state name : ");
                        state = Console.ReadLine();
                    }
                    Console.WriteLine("Enter your zip : ");
                    String zip = Console.ReadLine();
                    Regex  reg = new Regex(@"(^[0-9]{6}$)");
                    while (!reg.IsMatch(zip))
                    {
                        Console.WriteLine("Enter a valid zip code : ");
                        zip = Console.ReadLine();
                    }
                    Console.WriteLine("Enter your contact no. : ");
                    String contactNo = Console.ReadLine();
                    Regex  reg1      = new Regex(@"(^[7-9]{1}[0-9]{9}$)");
                    while (!reg1.IsMatch(contactNo))
                    {
                        Console.WriteLine("Enter a a valid mobile number : ");
                        contactNo = Console.ReadLine();
                    }
                    Console.WriteLine("Enter your email : ");
                    String mailID = Console.ReadLine();
                    Regex  reg2   = new Regex("^[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[a-z]{2,})$");
                    while (!reg2.IsMatch(mailID))
                    {
                        Console.WriteLine("Enter a a valid emailID : ");
                        mailID = Console.ReadLine();
                    }
                    Console.WriteLine("Enter the key name to be saved in the address book : ");
                    String keyname = Console.ReadLine();
                    Regex  reg3    = new Regex("^[A-Z a-z]*$");
                    while (!reg3.IsMatch(keyname))
                    {
                        Console.WriteLine("Enter a valid name : ");
                        keyname = Console.ReadLine();
                    }
                    Class1 c = new Class1(name, address, city.ToUpper(), state.ToUpper(), zip, contactNo, mailID);

                    ab.AddAddress(keyname, c);
                }
                else if (choice == 2)
                {
                    Console.WriteLine("1.Sort by Name");
                    Console.WriteLine("2.Sort by City");
                    Console.WriteLine("3.Sort by State");
                    Console.WriteLine("4.Sort by Zip");
                    int           input = Convert.ToInt32(Console.ReadLine());
                    List <Class1> li    = ab.ViewAddressBook(input);
                    if (li.Count == 0)
                    {
                        Console.WriteLine("The address book is empty.");
                    }
                    else
                    {
                        foreach (Class1 cc in li)
                        {
                            Console.WriteLine("Name : " + cc.name);
                            Console.WriteLine("Address : " + cc.address);
                            Console.WriteLine("City : " + cc.city);
                            Console.WriteLine("State : " + cc.state);
                            Console.WriteLine("zip : " + cc.zip);
                            Console.WriteLine("Contact No. : " + cc.phoneNo);
                            Console.WriteLine("Email ID : " + cc.email);
                        }
                    }
                }
                else if (choice == 3)
                {
                    Console.WriteLine("Enter the name :");
                    String ename = Console.ReadLine();
                    Console.WriteLine("Enter the new number for " + ename);
                    String newnumber = Console.ReadLine();
                    ab.EditNumber(ename, newnumber);
                }
                else if (choice == 4)
                {
                    Console.WriteLine("Enter the name :");
                    String rname = Console.ReadLine();
                    ab.RemoveContact(rname);
                }
                else if (choice == 5)
                {
                    Console.WriteLine("Enter the key name : ");
                    String kname = Console.ReadLine();
                    Class1 cc    = ab.ViewByKeyName(kname);
                    if (cc == null)
                    {
                        Console.WriteLine("No such key name found!!!");
                    }
                    else
                    {
                        Console.WriteLine("Name : " + cc.name);
                        Console.WriteLine("Address : " + cc.address);
                        Console.WriteLine("City : " + cc.city);
                        Console.WriteLine("State : " + cc.state);
                        Console.WriteLine("zip : " + cc.zip);
                        Console.WriteLine("Contact No. : " + cc.phoneNo);
                        Console.WriteLine("Email ID : " + cc.email);
                    }
                }
                else if (choice == 6)
                {
                    Console.WriteLine("Enter the name of the city/state : ");
                    string        location = Console.ReadLine();
                    List <Class1> li       = ab.UC8_SearchPeopleByCityOrState(location);
                    if (li.Count != 0)
                    {
                        Console.WriteLine("There are " + li.Count + " contacts with location " + location);
                        foreach (Class1 cc in li)
                        {
                            Console.WriteLine("Name : " + cc.name + "  Address : " + cc.address + "  ZIP : " + cc.zip + "  Contact No : " + cc.phoneNo + "  EmailID : " + cc.email);
                        }
                    }
                    else
                    {
                        Console.WriteLine("No contact found!!!");
                    }
                }
                else if (choice == 7)
                {
                    ab.AddressByCity();
                }
                else if (choice == 8)
                {
                    ab.AddressByState();
                }
                else if (choice == 9)
                {
                    if (File.Exists(path))
                    {
                        Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer();
                        using (StreamWriter sw = new StreamWriter(path))
                            using (JsonWriter writer = new JsonTextWriter(sw))
                            {
                                List <Class1> li = ab.ViewAddressBook(1);

                                ser.Serialize(writer, li);
                            }
                    }


                    ab.ReadAllText();
                }
                else
                {
                    if (File.Exists(path))
                    {
                        Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer();
                        using (StreamWriter sw = new StreamWriter(path))
                            using (JsonWriter writer = new JsonTextWriter(sw))
                            {
                                List <Class1> li = ab.ViewAddressBook(1);

                                ser.Serialize(writer, li);
                            }
                    }
                    break;
                }
            } while (choice != 10);
        }