Esempio n. 1
0
        public static bool WriteBookContentIntoFile(List <Person> people, string pathToPhoneBookFile, string fileType)
        {
            if (people == null || people.Count < 1)
            {
                throw new ArgumentException("A null or empty list of people provided- Check again how you call me!");
            }
            if (pathToPhoneBookFile == null || pathToPhoneBookFile.Trim().Length == 0)
            {
                throw new ArgumentException($"Invalid pathToPhoneBookFile argument provided: \"{pathToPhoneBookFile}\"");
            }
            if (fileType == null || fileType.Trim().Length == 0)
            {
                throw new ArgumentException($"Invalid fileType argument provided: \"{fileType}\"");
            }

            StringBuilder contentToWrite = new StringBuilder(people.Count * 100);

            switch (fileType)
            {
            case JSONFileType:
                foreach (Person someone in people)
                {
                    contentToWrite.Append(XMLAndJSONSerializer.JSONSerialize(someone) + "\n");
                }
                break;

            case XMLFileType:
                foreach (Person someone in people)
                {
                    contentToWrite.Append(XMLAndJSONSerializer.XMLSerialize(someone) + "\n");
                }
                break;

            default: throw new ArgumentOutOfRangeException($"Unsupported file type provided: {fileType}");
            }
            return(PhoneBookManipulator.WriteContentInFile(contentToWrite.ToString(), pathToPhoneBookFile));
        }
Esempio n. 2
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
            }
        }