示例#1
0
        public void DeleteContact(byte[] Key, byte[] IV)
        {
            ContactList.Clear();
            Console.WriteLine("Enter a name");
            string      selectedName = Console.ReadLine().ToUpper();
            Boolean     exists       = false;
            ProcessFile processFile  = new ProcessFile();
            string      Final        = processFile.ReadFromFile("friends.txt", Key, IV);

            string[] result = Final.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < result.Length; i++)
            {
                string[] values = result[i].Split(',');
                if (values[0] == selectedName)
                {
                    exists = true;
                }
            }
            string tempFile = Path.GetTempFileName();

            if (exists)
            {
                Console.WriteLine("\nWould you like to delete {0}'s details? (Y/N)", selectedName);
                string deleteChoice = Console.ReadLine().ToUpper();
                if (deleteChoice == "Y")
                {
                    for (int i = 0; i < result.Length; i++)
                    {
                        string[] values = result[i].Split(',');
                        if (values[0] != selectedName)
                        {
                            //processFile.WriteToFile(result[i], tempFile,Key,IV);
                            Contact contact = new Contact
                            {
                                Name         = values[0],
                                AddressLine1 = values[1],
                                AddressLine2 = values[2],
                                AddressLine3 = values[3],
                                PhoneNumber  = values[4]
                            };
                            ContactList.Add(contact);
                        }
                    }
                    processFile.WriteToFile(ContactList, tempFile, Key, IV);
                    File.Delete("friends.txt");
                    File.Move(tempFile, "friends.txt");
                }
            }
            else
            {
                Console.WriteLine("A record for {0} could not be found", selectedName);
            }
        }
示例#2
0
        public void CreateContact(byte[] Key, byte[] IV)
        {
            Console.WriteLine("Enter new friends name");
            string newName = Console.ReadLine().ToUpper();

            Console.WriteLine("Enter new friends address line 1");
            string AddressLine1 = Console.ReadLine().ToUpper();

            Console.WriteLine("Enter new friends address line 2");
            string AddressLine2 = Console.ReadLine().ToUpper();

            Console.WriteLine("Enter new friends address line 3");
            string AddressLine3 = Console.ReadLine().ToUpper();

            Console.WriteLine("Enter new friends number");
            string  newNumber  = Console.ReadLine().ToUpper();
            Contact newContact = new Contact(newName, AddressLine1, AddressLine2, AddressLine3, newNumber);

            ContactList.Add(newContact);
            ProcessFile processFile = new ProcessFile();

            //processFile.WriteToFile(newContact.ToString(), "friends.txt", Key, IV);
            processFile.WriteToFile(ContactList, "friends.txt", Key, IV);
        }
示例#3
0
        public void UpdateContact(byte[] Key, byte[] IV)
        {
            ContactList.Clear();
            Console.WriteLine("Enter a name");
            String      selectedName = Console.ReadLine().ToUpper();
            Boolean     exists       = false;
            ProcessFile processFile  = new ProcessFile();
            string      Final        = processFile.ReadFromFile("friends.txt", Key, IV);

            string[] result = Final.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < result.Length; i++)
            {
                string[] values = result[i].Split(',');
                if (values[0] == selectedName)
                {
                    exists = true;
                }
            }
            string tempFile = Path.GetTempFileName();

            if (exists)
            {
                Console.WriteLine("\nWhich detail for {0} would you like to edit?", selectedName);
                Console.WriteLine("1. {0}'s name", selectedName);
                Console.WriteLine("2. {0}'s address line 1", selectedName);
                Console.WriteLine("3. {0}'s address line 2", selectedName);
                Console.WriteLine("4. {0}'s address line 3", selectedName);
                Console.WriteLine("5. {0}'s number", selectedName);
                string updateChoice = Console.ReadLine().ToUpper();
                if (updateChoice == "1" || updateChoice == "2" || updateChoice == "3" || updateChoice == "4" || updateChoice == "5")
                {
                    Console.WriteLine("\nEnter the new value desired");
                    string updateValue = Console.ReadLine().ToUpper();
                    for (int i = 0; i < result.Length; i++)
                    {
                        string[] values = result[i].Split(',');
                        if (updateChoice == "1")
                        {
                            if (values[0] != selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = values[1],
                                    AddressLine2 = values[2],
                                    AddressLine3 = values[3],
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                // processFile.WriteToFile(result[i], tempFile, Key, IV);
                            }
                            else if (values[0] == selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = updateValue,
                                    AddressLine1 = values[1],
                                    AddressLine2 = values[2],
                                    AddressLine3 = values[3],
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                //   processFile.WriteToFile(String.Format("{0},{1},{2},{3},{4}", updateValue, values[1], values[2], values[3], values[4]), tempFile, Key, IV);
                            }
                        }
                        else if (updateChoice == "2")
                        {
                            if (values[0] != selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = values[1],
                                    AddressLine2 = values[2],
                                    AddressLine3 = values[3],
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                // processFile.WriteToFile(result[i], tempFile, Key, IV);
                            }
                            else if (values[0] == selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = updateValue,
                                    AddressLine2 = values[2],
                                    AddressLine3 = values[3],
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                //  processFile.WriteToFile(String.Format("{0},{1},{2},{3},{4}", values[0], updateValue, values[2], values[3], values[4]), tempFile, Key, IV);
                            }
                        }
                        else if (updateChoice == "3")
                        {
                            if (values[0] != selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = values[1],
                                    AddressLine2 = values[2],
                                    AddressLine3 = values[3],
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                // processFile.WriteToFile(result[i], tempFile, Key, IV);
                            }
                            else if (values[0] == selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = values[1],
                                    AddressLine2 = updateValue,
                                    AddressLine3 = values[3],
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                // processFile.WriteToFile(String.Format("{0},{1},{2},{3},{4}", values[0], values[1], updateValue, values[3], values[4]), tempFile, Key, IV);
                            }
                        }
                        else if (updateChoice == "4")
                        {
                            if (values[0] != selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = values[1],
                                    AddressLine2 = values[2],
                                    AddressLine3 = values[3],
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                //processFile.WriteToFile(result[i], tempFile, Key, IV);
                            }
                            else if (values[0] == selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = values[1],
                                    AddressLine2 = values[2],
                                    AddressLine3 = updateValue,
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                //processFile.WriteToFile(String.Format("{0},{1},{2},{3},{4}", values[0], values[1], values[2], updateValue, values[4]), tempFile, Key, IV);
                            }
                        }
                        else if (updateChoice == "5")
                        {
                            if (values[0] != selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = values[1],
                                    AddressLine2 = values[2],
                                    AddressLine3 = values[3],
                                    PhoneNumber  = values[4]
                                };
                                ContactList.Add(contact);
                                // processFile.WriteToFile(result[i], tempFile, Key, IV);
                            }
                            else if (values[0] == selectedName)
                            {
                                Contact contact = new Contact
                                {
                                    Name         = values[0],
                                    AddressLine1 = values[1],
                                    AddressLine2 = values[2],
                                    AddressLine3 = values[3],
                                    PhoneNumber  = updateValue
                                };
                                ContactList.Add(contact);
                                //  processFile.WriteToFile(String.Format("{0},{1},{2},{3},{4}", values[0], values[1], values[2], values[3], updateValue), tempFile, Key, IV);
                            }
                        }


                        else
                        {
                            Console.WriteLine("Invalid Selection");
                            break;
                        }
                    }


                    //            //inputStream.Close();
                    processFile.WriteToFile(ContactList, tempFile, Key, IV);
                    File.Delete("friends.txt");
                    File.Move(tempFile, "friends.txt");

                    //            break;
                    //        }
                    //        else
                    //        {
                    //            Console.WriteLine("Invalid selection");
                    //            break;
                    //        }

                    //    }
                }

                else
                {
                    Console.WriteLine("Invalid selection");
                }
            }
            else
            {
                Console.WriteLine("{0} could not be found", selectedName);
            }
        }