示例#1
0
        /// <summary>
        /// Checks wether number is owned by other contacts or not
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static bool CheckIfNumberAlreadyExists(string number)
        {
            List <Contact> contacts = InOut.GetAllContacts();

            int quantity = (from contact in contacts where contact.PhoneNumber.Equals(number) select contact).Count();

            return(quantity < 1 ? false : true);
        }
示例#2
0
        /// <summary>
        /// Lets get the specific line from a data file and update it
        /// </summary>
        /// <param name="index"></param>
        /// <param name="filename"></param>
        public static void UpdateContact(int index, string filename)
        {
            if (InOut.GetAllContacts().Count != 0)
            {
                string[] lines = File.ReadAllLines(filename);

                if (index - 1 > lines.Length || index - 1 < 0)
                {
                    Console.WriteLine("Incorrect input. A contact with this typed number does not exists.");
                    return;
                }

                Console.WriteLine("Retype updated contact in format \"First_Name, Last_Name, Phone_Number, Address\"");

                try
                {
                    string   updateContact = Console.ReadLine();
                    string[] values        = updateContact.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string   phoneNumber   = values[2];


                    if (lines[index - 1].Contains(phoneNumber))
                    {
                        lines[index - 1] = updateContact;

                        File.WriteAllLines(filename, lines);
                        Console.WriteLine("A contact was successfully updated.");
                    }
                    else
                    {
                        if (CheckIfNumberAlreadyExists(phoneNumber))
                        {
                            Console.WriteLine("A contact with same phone number already exists.");
                            return;
                        }
                        else
                        {
                            lines[index - 1] = updateContact;

                            File.WriteAllLines(filename, lines);
                        }
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Error. Incorrect input.");
                }
            }
        }
示例#3
0
        /// <summary>
        /// Removes a contact by specific property
        /// </summary>
        /// <param name="number"></param>
        /// <param name="filename"></param>
        public static void RemoveContact(string number, string filename)
        {
            if (InOut.GetAllContacts().Count != 0)
            {
                if (number.Equals(""))
                {
                    Console.WriteLine("Error. Incorrect input.");
                    return;
                }

                int      linesLenghtBefore = InOut.GetAllContacts().Count;
                string[] lines             = File.ReadAllLines(filename).Where(line => !line.Contains(number)).ToArray();

                if (linesLenghtBefore > lines.Length)
                {
                    File.WriteAllLines(filename, lines);
                    Console.WriteLine("A contact was successfully deleted.");
                }
                else
                {
                    Console.WriteLine("A contact with typed number does not exists.");
                }
            }
        }