static void Main(string[] args)
        {
            BookListService service = new BookListService(new BookFileRepository("TestBooksFile"), logger);

            if (!File.Exists("TestBooksFile"))
            {
                service.AddBooks(new List <Book> {
                    new Book("J. K. Rowling", "Harry Potter and the Prisoner of Azkaban", 528, 18.4),
                    new Book("Stephen King", "The Green Mile", 384, 10.6),
                    new Book("Борис Васильев", "А зори здесь тихие...", 432, 10.2),
                    new Book("Антуан де Сент-Экзюпери", "Маленький принц", 104, 16.7),
                });
            }

            IEnumerable <Book> sort = service.SortBooksByTag(book => book.Price);

            Console.WriteLine("Sort by price: ");
            foreach (var book in sort)
            {
                Console.WriteLine($"{book}");
            }

            sort = service.SortBooksByTag(book => book.NumberPages);
            Console.WriteLine("Sort by number of pages: ");
            foreach (var book in sort)
            {
                Console.WriteLine($"{book}");
            }

            IEnumerable <Book> find = service.FindBookByTag(book => book.Author == "Антуан де Сент-Экзюпери");

            Console.WriteLine("Find by author Антуан де Сент-Экзюпери");
            foreach (var book in find)
            {
                Console.WriteLine($"{book} ");
            }

            Console.WriteLine("Test remove (Harry Potter) and add (Godfather):");
            service.AddBook(new Book("Mario Puzo", "Godfather", 288, 25.6));
            service.RemoveBook(new Book("J. K. Rowling", "Harry Potter and the Prisoner of Azkaban", 528, 18.4));

            foreach (var book in service.Books)
            {
                Console.WriteLine($"{book}");
            }

            Console.WriteLine("Test exceptions:");
            try
            {
                service.AddBook(null);
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
            }
            try
            {
                service.RemoveBook(new Book("Margaret Mitchell", "Gone with the Wind", 1280, 17));
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            BookListService service = new BookListService(new BookRepository("books"));

            if (!File.Exists("books"))
            {
                service.AddBooks(new List <Book> {
                    new Book("J. K. Rowling", "Harry Potter and the Prisoner of Azkaban", "Махаон", 528, 184100),
                    new Book("J. K. Rowling", "Harry Potter and the Philosopher's Stone", "МАХАОН", 432, 172100),
                    new Book("Stephen King", "The Green Mile", "ACT", 384, 106100),
                    new Book("Margaret Mitchell", "Gone with the Wind", "Эксмо", 1280, 173100),
                    new Book("Arthur Conan Doyle", "A Study in Scarlet", "ACT", 304, 123700),
                    new Book("George Martin", "A Storm of Swords", "ACT", 960, 127300),
                    new Book("Борис Васильев", "А зори здесь тихие...", "Мартин", 432, 102200),
                    new Book("Alexandre Dumas", "Le comte de Monte Cristo", "ACT", 1216, 166700),
                    new Book("Harper Lee", "To Kill a Mockingbird", "ACT", 416, 90100)
                });
            }

            IEnumerable <Book> find = service.Find(book => book.PublishOrganization.ToLowerInvariant() == "махаон");

            WriteLine("\tFind by publish organisation Махаон\n");

            foreach (var book in find)
            {
                WriteLine($"{book}\n");
            }

            IEnumerable <Book> sort = service.Sort(book => book.Price);

            WriteLine("\n\n\tSort by price\n");
            foreach (var book in sort)
            {
                WriteLine($"{book}\n");
            }

            sort = service.Sort(book => book.PagesNumber);
            WriteLine("\n\n\tSort by number of pages\n");
            foreach (var book in sort)
            {
                WriteLine($"{book}\n");
            }

            WriteLine("\n\n\ttest exceptions\n");
            try {
                service.AddBook(new Book("Margaret Mitchell", "Gone with the Wind", "Эксмо", 1280, 173100));
            } catch (BookListServiceException ex) {
                WriteLine(ex.Message);
            }
            try {
                service.RemoveBook(new Book("Margaret Mitchell", "Gone with the Wind", "ACT", 1280, 173100));
            } catch (BookListServiceException ex) {
                WriteLine(ex.Message);
            }

            WriteLine("\n\n\tTest remove: 'Gone with the Wind' and '...the Prisoner of Azkaban'");
            service.RemoveBook(new Book("Margaret Mitchell", "Gone with the Wind", "Эксмо", 1280, 173100));
            service.RemoveBook(new Book("J. K. Rowling", "Harry Potter and the Prisoner of Azkaban", "Махаон", 528, 184100));

            foreach (var book in service.Books)
            {
                WriteLine($"{book}\n");
            }

            WriteLine("\n\n\tTest add book: 'Gone with the Wind' and '...the Prisoner of Azkaban' and 'Godfather'");
            service.AddBooks(new List <Book> {
                new Book("Margaret Mitchell", "Gone with the Wind", "Эксмо", 1280, 173100),
                new Book("J. K. Rowling", "Harry Potter and the Prisoner of Azkaban", "Махаон", 528, 184100),
                new Book("Mario Puzo", "Godfather", "Айрис-пресс", 288, 256300)
            });

            foreach (var book in service.Books)
            {
                WriteLine($"{book}\n");
            }

            service.RemoveBook(new Book("Mario Puzo", "Godfather", "Айрис-пресс", 288, 256300));

            ReadLine();
        }