Exemplo n.º 1
0
        /// <summary>
        /// The function is written to populate the dictionary from the data stores in the json files
        /// </summary>
        /// <param name="addressDict">the address book to be operated</param>
        public static void PopulateDictionary(Dictionary <string, List <ContactClass> > addressDict)
        {
            string path = Directory.GetCurrentDirectory();

            string[] files = Directory.GetFiles(path, "*.json");
            foreach (var i in files)
            {
                if (i.Contains(" "))
                {
                    List <ContactClass>  tempList    = new List <ContactClass>();
                    IList <ContactClass> contactData = JsonConvert.DeserializeObject <IList <ContactClass> >(File.ReadAllText(i));
                    using (StreamReader sr = new StreamReader(i))
                        foreach (ContactClass items in contactData)
                        {
                            string       first   = items.First;
                            string       last    = items.Last;
                            string       address = items.Address;
                            string       city    = items.City;
                            string       state   = items.State;
                            string       phone   = items.Phone;
                            string       email   = items.Email;
                            int          zip     = items.Zip;
                            ContactClass contact = new ContactClass()
                            {
                                First   = first,
                                Last    = last,
                                Address = address,
                                City    = city,
                                State   = state,
                                Phone   = phone,
                                Email   = email,
                                Zip     = zip
                            };
                            tempList.Add(contact);
                        }
                    string fileName = i.Replace(path + "\\", "");
                    fileName = fileName.Replace(".json", "");
                    if (!addressDict.ContainsKey(fileName))
                    {
                        addressDict.Add(fileName, tempList);
                    }
                }
            }
        }
        /// <summary>
        /// The function is written to add contacts in the contact list
        /// </summary>
        /// <param name="list">the contact list that has been selected by the user</param>
        /// <param name="personcity">a dictionary to store the person name along with the city in which the person resides</param>
        /// <param name="personState">a dictionary to store the person name along with the state in which the person resides</param>
        public static void AddContact(List <ContactClass> list, Dictionary <string, string> personcity, Dictionary <string, string> personState)
        {
            while (true)
            {
                Console.WriteLine("Enter first name, last name, address, city, state, zip, phone number and email in sequence");
                string first   = Console.ReadLine();
                string last    = Console.ReadLine();
                string address = Console.ReadLine();
                string city    = Console.ReadLine();
                string state   = Console.ReadLine();
                int    zip     = Convert.ToInt32(Console.ReadLine());
                string phone   = Console.ReadLine();
                string email   = Console.ReadLine();

                //Create a new contact template
                var varContact = new ContactClass()
                {
                    First = first, Last = last, Address = address, City = city, State = state, Zip = zip, Phone = phone, Email = email
                };

                if (list.Contains(varContact))//If address book already has this contact, then discard it and ask the user to enter again
                {
                    Console.WriteLine("List already has this Contact template. Try again.");
                    Console.WriteLine("-------------------------");
                    continue;
                }

                list.Add(varContact);

                personcity.Add(first + " " + last, city);
                personState.Add(first + " " + last, state);
                Console.WriteLine("-------------------------");
                Console.WriteLine("The Contact has been added succesfully");
                Console.WriteLine("-------------------------");

                Console.WriteLine("Enter 1 to add one more and 0 to exit");
                int check = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("-------------------------");
                if (check == 0)//If user does not want to add  another contact
                {
                    break;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The function is written to store default values in the variabes which will be later used in the program
        /// </summary>
        public static void SetUpDefaultValues()
        {
            List <ContactClass> ContactClassList = new List <ContactClass>();                                        //store contacts in an address book
            Dictionary <string, List <ContactClass> > addressDict = new Dictionary <string, List <ContactClass> >(); //store address books
            Dictionary <string, string> personCity  = new Dictionary <string, string>();                             //maintain a dictionary of a person and his city
            Dictionary <string, string> personState = new Dictionary <string, string>();                             //maintain a dictionary of a person and his state


            ContactClass varContactClass = new ContactClass()//Default data that will be present in the address book at the start of the program
            {
                First     = "First",
                Last      = "Last",
                Address   = "Address",
                City      = "City",
                State     = "State",
                Phone     = "phone",
                Email     = "email",
                Zip       = 10066,
                DateAdded = Convert.ToDateTime("01/01/2020")
            };

            ContactClassList.Add(varContactClass);
            personCity.Add("First Last", "City");
            personState.Add("First Last", "State");

            ContactClass varContactClass2 = new ContactClass()//Default data that will be present in the address book at the start of the program
            {
                First     = "First2",
                Last      = "Last2",
                Address   = "Address2",
                City      = "City2",
                State     = "State2",
                Phone     = "phone2",
                Email     = "email2",
                Zip       = 100667,
                DateAdded = Convert.ToDateTime("01/02/2020")
            };

            ContactClassList.Add(varContactClass2);
            personCity.Add("First2 Last2", "City2");
            personState.Add("First2 Last2", "State2");

            ContactClass varContactClass3 = new ContactClass()//Default data that will be present in the address book at the start of the program
            {
                First     = "First3",
                Last      = "Last3",
                Address   = "Address3",
                City      = "City3",
                State     = "State3",
                Phone     = "phone3",
                Email     = "email3",
                Zip       = 100668,
                DateAdded = Convert.ToDateTime("01/03/2020")
            };

            ContactClassList.Add(varContactClass3);
            personCity.Add("First3 Last3", "City3");
            personState.Add("First3 Last3", "State3");

            addressDict.Add("Address_Book_1", ContactClassList);
        }