예제 #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.");
                }
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            const string filename = "contacts.csv";
            bool         run      = true;

            while (run)
            {
                Console.WriteLine("Choose a function by number: ");
                Console.WriteLine("1 - Add contact");
                Console.WriteLine("2 - Delete contact");
                Console.WriteLine("3 - Update contact information");
                Console.WriteLine("4 - View all contacts");

                bool correctInput = int.TryParse(Console.ReadLine(), out int choiceInput);

                if (!correctInput || !(choiceInput < 5 && choiceInput > 0))
                {
                    Console.WriteLine("Error. Incorrect input. Please exit application and try again.");
                    break;
                }

                run = false;

                switch (choiceInput)
                {
                case 1:
                    Console.Clear();
                    Console.WriteLine("ADD CONTACT");
                    Console.WriteLine("A contact format has to be: \"First_Name, Last_Name, Phone_Number, Address\". Address field is optional.");

                    string contactInput = Console.ReadLine();
                    TaskUtils.AddAndSave(filename, contactInput);

                    BackProcess(ref run);

                    break;

                case 2:
                    Console.Clear();
                    Console.WriteLine("REMOVE CONTACT");
                    InOut.PrintAllContacts("Type a phone number of a contact to be removed.");

                    string deleteByNumber = Console.ReadLine();
                    TaskUtils.RemoveContact(deleteByNumber, filename);

                    BackProcess(ref run);

                    break;

                case 3:
                    Console.Clear();
                    Console.WriteLine("UPDATE CONTACT");
                    InOut.PrintAllContacts("Type a number of a contact to be updated.");

                    bool contactIndex = int.TryParse(Console.ReadLine(), out int index);

                    if (!contactIndex)
                    {
                        Console.WriteLine("Error. Incorrect input.");
                        BackProcess(ref run);
                        break;
                    }

                    TaskUtils.UpdateContact(index, filename);

                    BackProcess(ref run);

                    break;

                case 4:
                    Console.Clear();
                    Console.WriteLine("VIEW ALL CONTACTS");
                    InOut.PrintAllContacts("");

                    BackProcess(ref run);
                    break;
                }
            }
            Console.ReadKey();
        }