/// <summary>
        /// Updates selected contact's data with new data
        /// </summary>
        /// <param name="peopleList">Contact's list</param>
        /// <returns>Updated contact's list</returns>
        public static List <Person> UpdateInfo(List <Person> peopleList, out string text)
        {
            List <Person> tempList = new List <Person>();

            Console.Clear();

            Person tempPerson = GetInfo();

            var temp = peopleList.Where(x => x.Name == tempPerson.Name && x.Surname == tempPerson.Surname).ToList <Person>();

            if (temp.Count > 0)
            {
                Console.WriteLine("Your information: \n");
                InOut.PrintToScreen(temp);

                Console.WriteLine("What do you want to update? (enter number of selected option)");
                Console.WriteLine("1. Update name");
                Console.WriteLine("2. Update last name");
                Console.WriteLine("3. Update phone number");
                Console.WriteLine("4. Update address");
                int choice = int.Parse(Console.ReadLine());

                if (choice < 1 || choice > 4)
                {
                    text = "Selected choice does not exist \n ";
                }

                else
                {
                    string updatedInfo = string.Empty;
                    text = string.Empty;
                    switch (choice)
                    {
                    case 1:
                    {
                        Console.WriteLine("Enter new name: ");
                        updatedInfo = Console.ReadLine();
                        peopleList  = peopleList.Where(x => x.Name == tempPerson.Name).Select(x => { x.Name = updatedInfo; return(x); }).ToList <Person>();
                        break;
                    }

                    case 2:
                    {
                        Console.WriteLine("Enter new last name: ");
                        updatedInfo = Console.ReadLine();
                        peopleList  = peopleList.Where(x => x.Surname == tempPerson.Surname).Select(x => { x.Surname = updatedInfo; return(x); }).ToList <Person>();
                        break;
                    }

                    case 3:
                    {
                        Console.WriteLine("Enter new phone number: ");
                        updatedInfo = Console.ReadLine();
                        var tempContact = new Person(tempPerson.Name, tempPerson.Surname, updatedInfo, tempPerson.Address);

                        if (CheckEligibility(tempContact, peopleList))
                        {
                            peopleList = peopleList.Where(x => x.PhoneNumber == tempPerson.PhoneNumber).Select(x => { x.PhoneNumber = updatedInfo; return(x); }).ToList <Person>();
                        }

                        else
                        {
                            text = "Phone number was not updated because its already exists \n";
                        }

                        break;
                    }

                    case 4:
                    {
                        Console.WriteLine("Enter new address: ");
                        peopleList = peopleList.Where(x => x.Address == tempPerson.Address).Select(x => { x.Address = updatedInfo; return(x); }).ToList <Person>();
                        break;
                    }
                    }
                }
            }

            else
            {
                throw new Exception("Contact was not found");
            }
            return(peopleList);
        }
Пример #2
0
        /// <summary>
        /// Main method which interacts with user and fulfill users selected options
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            bool          Exit       = false;
            List <Person> PeopleList = InOut.ReadContacts();

            while (!Exit)
            {
                int choice = 0;
                Console.WriteLine("Contact manager \n");
                Console.WriteLine("Choose what you want to do: (enter number of selected option)");
                Console.WriteLine("1. Add contact");
                Console.WriteLine("2. Update contact information");
                Console.WriteLine("3. Delete contact");
                Console.WriteLine("4. View all contacts");
                Console.WriteLine("5. Exit and save");

                try
                {
                    choice = int.Parse(Console.ReadLine());
                    if (choice < 1 || choice > 5)
                    {
                        throw new Exception();
                    }
                }

                catch
                {
                    Console.Clear();
                    Console.WriteLine("Selected choice does not exist \n ");
                }

                switch (choice)
                {
                case 1:
                {
                    try
                    {
                        var person = TaskUtils.CreatePerson();
                        if (TaskUtils.CheckEligibility(person, PeopleList))
                        {
                            PeopleList.Add(person);
                            Console.Clear();
                            Console.WriteLine("Contact was created successfuly! \n");
                        }

                        else
                        {
                            Console.Clear();
                            Console.WriteLine("Contact was not created because person with declared phone number already exists \n");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Clear();
                        Console.WriteLine(ex.Message);
                    }
                    break;
                }

                case 2:
                {
                    try
                    {
                        string text = string.Empty;
                        TaskUtils.UpdateInfo(PeopleList, out text);
                        Console.Clear();
                        if (text == string.Empty)
                        {
                            Console.WriteLine("Contact was updated successfuly! \n");
                        }

                        else
                        {
                            Console.WriteLine(text);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Clear();
                        Console.WriteLine(ex.Message);
                    }
                    break;
                }

                case 3:
                {
                    try
                    {
                        TaskUtils.DeleteInfo(PeopleList);
                        Console.Clear();
                        Console.WriteLine("Contact was deleted successfuly! \n");
                    }
                    catch (Exception ex)
                    {
                        Console.Clear();
                        Console.WriteLine(ex.Message);
                    }
                    break;
                }

                case 4:
                {
                    try
                    {
                        Console.Clear();
                        InOut.PrintToScreen(PeopleList);
                    }
                    catch (Exception ex)
                    {
                        Console.Clear();
                        Console.WriteLine(ex.Message);
                    }

                    break;
                }

                case 5:
                {
                    InOut.PrintToCsv(PeopleList);
                    Exit = true;
                    break;
                }
                }
            }
        }