static void Main(string[] args) { var temp = new Book("one", "One"); var first = new Book("Роулинг Джоанн", "Гарри Поттер и философский камень", "Фантастика", "РОСМЭН-Издат", 300); var seventh = new Book("J.K.Rouling", "Harry Potter and the deathly Hallows"); var bookList = new BookListService(); bookList.AddBook(first); bookList.AddBook(seventh); bookList.AddBook(temp); try { bookList.AddBook(null); } catch (ArgumentNullException) { } try { bookList.AddBook(new Book("one", "One")); } catch (ArgumentException) { } try { bookList.RemoveBook(null); } catch (ArgumentNullException) { } try { bookList.RemoveBook(new Book("two", "Two")); } catch (ArgumentException) { } Console.WriteLine("BookListService"); var brw = new BinaryReaderWriter("books_new.bin"); bookList.SaveBooks(brw); bookList.PrintBooks(); Console.WriteLine(); bookList.SortBooksByAuthor(); bookList.PrintBooks(); Console.WriteLine(); bookList.SortBooksByTitle(); bookList.PrintBooks(); Console.WriteLine(); var newList = new BookListService(); newList.LoadBooks(brw); newList.PrintBooks(); Console.ReadLine(); }
static void Main(string[] args) { var bookService = new BookListService(new FileBookRepository("default")); bookService.AddBook(new Book("J. Richter", "C# via", 500.0, 800)); bookService.AddBook(new Book("D. Samal", "Sifo VMSIS", 350.0, 85)); bookService.AddBook(new Book("A. Pushkin", "E.Onegin", 110.0, 150)); bookService.AddBook(new Book("L. Tolstoi", "Voina i mir", 650.0, 1000)); bookService.AddBook(new Book("S. Perro", "Kot v sapogah", 60.0, 50)); foreach (var book in bookService.BookList) Console.WriteLine($"{book}"); bookService.DeleteBook(new Book("L. Tolstoi", "Voina i mir", 650.0, 1000)); bookService.DeleteBook(new Book("S. Perro", "Kot v sapogah", 60.0, 50)); Console.WriteLine("------------------------------------------"); foreach (var book in bookService.BookList) Console.WriteLine($"{book}"); Console.WriteLine("------------------------------------------"); bookService.Sort(new BookComparer()); foreach (var book in bookService.BookList) Console.WriteLine($"{book}"); Console.WriteLine("------------------------------------------"); var selectedBooksByTag = bookService.FindBooksByTags(b => b.Author.Contains('S'.ToString())); foreach (var book in selectedBooksByTag) Console.WriteLine($"{book}"); bookService.Repository.Save(bookService.BookList); Console.ReadKey(); }
/// <summary> /// Creates new instance of the service /// </summary> /// <param name="temp">Base BookListService instance</param> public BookListService(BookListService temp, ILogger logger) { if (ReferenceEquals(logger, null)) { throw new ArgumentNullException($"{nameof(logger)} is null."); } this.logger = logger; bookList = new List <Book>(temp.bookList); }
public static void Serialize(string name , BookListService bs) { if (name.Equals(string.Empty) || name == null) throw new ArgumentNullException("File name not found!"); string path = String.Format(AppDomain.CurrentDomain.BaseDirectory + name); BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { formatter.Serialize(fs, bs); } }
private static void LoadService(IRepository serializator) { try { Console.WriteLine("---------- New Service ----------"); BookListService service1 = new BookListService(); try { service1.Load("test.bin", serializator); } catch (Exception) { } Console.WriteLine(service1.PrintBooks()); } catch (ArgumentException e) { Console.WriteLine("Wrong argument error with message: {0}", e.Message); } catch (SerializationException e) { Console.WriteLine("Can not deserialize book list. Message {0}", e.Message); } }
public static void Serialize(string name, BookListService bs) { if (name.Equals(string.Empty) || name == null) { throw new ArgumentNullException("File name not found!"); } string path = String.Format(AppDomain.CurrentDomain.BaseDirectory + name); BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { formatter.Serialize(fs, bs); } }
static void Main(string[] args) { //var path = "Books.txt"; var path1 = "Book.XML"; //IRepository<Book> rep = new BookRepository(path); IRepository<Book> rep1 = new WorkWithXml(path1); var bookService = new BookListService(rep1); Book book = new Book("3qw", "23", "fantasy", 1); bookService.Add(book); //bookService.Delete(book); var books = bookService.Sort(x => x.Publication); foreach (var b in books) { Console.Write("{0}\t", b.Author); Console.Write("{0}\t", b.Title); Console.Write("{0}\t", b.Genre); Console.Write("{0}\t", b.Publication); Console.WriteLine(); } /* Console.WriteLine(); var _books = bookService.SearchBooks(x => x.Author == "qqq"); foreach (var b in _books) { Console.Write("{0}\t", b.Author); Console.Write("{0}\t", b.Title); Console.Write("{0}\t", b.Genre); Console.Write("{0}\t", b.Publication); Console.WriteLine(); } Console.WriteLine(); var find = bookService.Search(x => x.Author == "qqq"); Console.Write("{0}\t{1}\t{2}\t{3}", find.Author, find.Title, find.Genre, find.Publication); Console.ReadKey(); foreach (var b in books) { Console.Write("{0}\t", b.Author); Console.Write("{0}\t", b.Title); Console.Write("{0}\t", b.Genre); Console.Write("{0}\t", b.Publication); Console.WriteLine(); }*/ Console.ReadKey(); }
static void Main(string[] args) { Book book1 = new Book("MyTitle1", "MyAuthor1", 1981, 101, "MyPublisher1", 100, "2-266-11156-6"); Book book2 = new Book("MyTitle2", "MyAuthor2", 1982, 102, "MyPublisher2", 100, "2-266-11156-6"); Book book3 = new Book("MyTitle3", "MyAuthor3", 1983, 103, "MyPublisher3", 100, "2-266-11156-6"); BookListService service = BookListService.Instance; service.AddBook(book2); service.AddBook(book1); service.AddBook(book3); Console.WriteLine(service.PrintBooks()); Console.WriteLine("_______________________________________________________"); service.SortBooksByTag(BookListService.Crit.author); Console.WriteLine(service.PrintBooks()); service.SaveBooks("Books"); service.LoadBooks("Books"); Console.ReadLine(); }
/// <summary> /// Finds books according to tag /// </summary> /// <param name="tag">Object that contains a rule of searching</param> /// <returns>New <see cref="BookListService"/> object with specified books</returns> public BookListService FindBookByTag(Predicate <Book> tag) { if (ReferenceEquals(tag, null)) { throw new ArgumentNullException(); } BookListService temp = new BookListService(this.logger); foreach (var t in bookList) { if (tag(t)) { temp.AddBook(t); } } return(temp); }
static void Main(string[] args) { BookListService bl = new BookListService("booklist.txt"); bl.AddBook(new Book("Braun","Inferno",500,"Scince. Adventure")); bl.AddBook(new Book("Zamyatin","We",100,"Dystopia")); bl.AddBook(new Book("Tolkien", "The Lord Of The Rings", 1000, "Fantasy")); System.Console.WriteLine(bl.ToString()); //bl.AddBook(new Book("Zamyatin", "Hronic", 134, "Fantasy")); System.Console.WriteLine("__________________"); bl.SortBooksByTag(); System.Console.WriteLine(bl.ToString()); Book bok = bl.FindBookByTag(x => x.PagesAmount == 100); System.Console.WriteLine(bok.ToString()); System.Console.ReadLine(); }
private static void SaveService(IRepository serializator) { BookListService service = new BookListService(); service.AddBook(new Book("fAuthor1", "Title1", "Text1", 1)); service.AddBook(new Book("eAuthor2", "Title2", "Text2", 2)); service.AddBook(new Book("aAuthor3", "Title3", "Text3", 3)); service.AddBook(new Book("cAuthor4", "Title4", "Text4", 4)); service.AddBook(new Book("bAuthor5", "Title5", "Text5", 5)); service.AddBook(new Book("dAuthor6", "Title6", "Text6", 6)); Console.WriteLine("---------- Old Service ----------"); Console.WriteLine(service.PrintBooks()); service.Sort((b1, b2) => b1.Author.CompareTo(b2.Author)); Console.WriteLine("FindByAuthor: " + service.FindBy(author:"aAuthor3").Print()); Console.WriteLine("FindByTitle: " + service.FindBy(title:"Title1").Print()); Console.WriteLine(service.RemoveBook(service.FindBy(author:"fAuthor1"))); try { service.AddBook(null); } catch (Exception) { } try { service.AddBook(new Book("dAuthor6", "Title6", "Text6", 6)); } catch (Exception) { } try { service.RemoveBook(null); } catch (Exception) { } try { service.RemoveBook(new Book("dAuthor", "Title6", "Text6", 6)); } catch (Exception) { } try { service.FindBy(author: null).Print(); } catch (Exception) { } try { service.FindBy(title: "").Print(); } catch (Exception) { } try { try { service.Save("test.bin", serializator); } catch (Exception) { } } catch (ArgumentException e) { Console.WriteLine("Wrong argument error with message: {0}", e.Message); } }
public static void Serialize(string name, BookListService bookList) { Serializator.Serialize(name, bookList); }