示例#1
0
        public bool WriteInBook(List <Person> dataOfSomePeople, string pathToPhoneBookFile)
        {
            if (dataOfSomePeople == null || dataOfSomePeople.Count < 1)
            {
                throw new ArgumentException("Null or empty list of personal data provided to WriteInBook(List<Person>, string)!");
            }
            if (pathToPhoneBookFile == null || pathToPhoneBookFile.Length < 1)
            {
                throw new ArgumentException($"Null or empty pathToPhoneBookFile parameter provided to WriteInBook(List<Person>, string pathToPhoneBookFile =\"{pathToPhoneBookFile}\")!");
            }

            StringBuilder fileContent = new StringBuilder(dataOfSomePeople.Count);

            foreach (Person someone in dataOfSomePeople)
            {
                fileContent.Append(PhoneBookParser.ParsePersonDataToPhoneBookFileRow(someone) + "\n");
            }
            return(WriteContentInFile(fileContent.ToString(), pathToPhoneBookFile));
        }
示例#2
0
        public List <Person> ReadFromBook(string pathToBookFile)
        {
            if (pathToBookFile == null || pathToBookFile.Length < 1)
            {
                throw new ArgumentException($"Null or empty pathToBookFile parameter provided to ReadFromBook(string pathToBookFile =\"{pathToBookFile}\")!");
            }

            if (pathToBookFile.Equals(this.aPhoneBook.FilePath))
            {
                throw new ArgumentException("Duplicating phonebooks not allowed in ReadFromBook()!");
            }
            string textProcessed = ReadFileContent(pathToBookFile);

            string[]      personDataRead = textProcessed.Split(PhoneBookParser.FileRowsSplitParameter, StringSplitOptions.RemoveEmptyEntries);
            List <Person> result         = new List <Person>(personDataRead.Length);

            foreach (string personData in personDataRead)
            {
                result.Add(PhoneBookParser.ParsePersonData(personData));
            }
            return(result);
        }
示例#3
0
        static void Main(string[] args)
        {
            PhoneBookManipulator MyPhoneBook = new PhoneBookManipulator();
            string userChoise = null;

            while (!(userChoise = RequestConsoleMenuInput().Trim()).Equals(QuitCommand))
            {
                switch (userChoise)
                {
                case EnterPersonDataFromConsoleCommand:
                    string[] PeopleData = EnterPersonDataFromConsole();
                    if (PeopleData.Length > 0)
                    {
                        List <Person> People = new List <Person>(PeopleData.Length);
                        foreach (string aPersonData in PeopleData)
                        {
                            People.Add(PhoneBookParser.ParsePersonData(aPersonData));
                        }
                        if (MyPhoneBook.WriteInBook(People))
                        {
                            Console.WriteLine($"Successfully added data for {People.Count} people in phone book! :)");
                        }
                        else
                        {
                            Console.WriteLine("\n\tSomething went wrong while wrting people data in phone book!\n"
                                              + "\n\tCheck for any messages above to find why.\n");
                        }
                    }
                    else
                    {
                        Console.WriteLine("\n\tNo Person data read from your input.\n");
                    }
                    break;

                case PrintPersonDataOnConsoleCommand:
                    if (MyPhoneBook.ThePhoneBook != null &&
                        MyPhoneBook.ThePhoneBook.people != null &&
                        MyPhoneBook.ThePhoneBook.people.Count > 0)
                    {
                        Console.WriteLine("\n\t===Printing phone book content===\n");
                        foreach (Person someone in MyPhoneBook.ReadFromBook(MyPhoneBook.ThePhoneBook))
                        {
                            Console.WriteLine(someone);
                        }
                        Console.WriteLine("\n\t===End of phone book content===\n");
                    }
                    else
                    {
                        Console.WriteLine("\n\tPhone book not yet created or currenty empty.\n");
                    }
                    break;

                case EnterPersonDataFromFileCommand:
                    Console.WriteLine("Enter path to file with phone book content:");
                    string        pathToFhoneBookFile = Console.ReadLine();
                    List <Person> peopleInFile        = MyPhoneBook.ReadFromBook(pathToFhoneBookFile);
                    if (MyPhoneBook.WriteInBook(peopleInFile))
                    {
                        Console.WriteLine($"\n\tSuccessfully added {peopleInFile.Count} people to phone book.\n");
                    }
                    else
                    {
                        Console.WriteLine("Something went wrong while trying to add people to phone book!\n"
                                          + "\n\tCheck for any messages above to see why.\n");
                    }
                    break;

                case PrintPersonDataIntoFileCommand:
                    Console.WriteLine("Enter path to file in which to write phone book content:");
                    string pathToWriteFhoneBookInFile = Console.ReadLine();

                    if (MyPhoneBook.ThePhoneBook != null &&
                        MyPhoneBook.ThePhoneBook.people != null &&
                        MyPhoneBook.ThePhoneBook.people.Count > 0)
                    {
                        if (MyPhoneBook.WriteInBook(MyPhoneBook.ThePhoneBook.people, pathToWriteFhoneBookInFile))
                        {
                            Console.WriteLine($"\n\tSuccessfully written {MyPhoneBook.ThePhoneBook.people.Count} in file {pathToWriteFhoneBookInFile}.\n");
                        }
                        else
                        {
                            Console.WriteLine("\n\tSomething went wrong while wrting people data from current phone book in file!\n"
                                              + "\n\tCheck for any messages above to find why.\n");
                        }
                    }
                    else
                    {
                        Console.WriteLine("\n\tPhone book not yet created or currenty empty.\n");
                    }
                    break;

                case ExecuteCommandsSetFromFileCommand:
                    break;

                case ExecuteCommandFromConsoleCommand:
                    break;

                default:
                    Console.WriteLine($"Unsupported command enetered {userChoise}");
                    break;
                }//switch
                RequestPressOfAnyKey();//Lets user see result of last command chosen
            }
        }