/// <summary> /// Sort list of books by tag. /// </summary> /// <param name="tag">Tag.</param> public void SortByTag(string tag) { if (tag == null) { return; } tag = tag.ToLower(CultureInfo.CreateSpecificCulture("en-US")); List <string> tags = new List <string> { "isbn", "author", "name", "publishing", "year", "pages", "price" }; try { if (!tags.Contains(tag)) { throw new BookException("Incorrect tag."); } } catch (BookException ex) { Program.Logger.Error(ex.Message); return; } var a = this.books.ToArray(); switch (tag) { case "isbn": { Array.Sort(a, Book.SortIsbn()); this.books = a.ToList(); } break; case "author": { Array.Sort(a, Book.SortAuthor()); this.books = a.ToList(); } break; case "name": { Array.Sort(a, Book.SortName()); this.books = a.ToList(); } break; case "publishing": { Array.Sort(a, Book.SortPublishing()); this.books = a.ToList(); } break; case "year": { Array.Sort(a, Book.SortYear()); this.books = a.ToList(); } break; case "pages": { Array.Sort(a, Book.SortPages()); this.books = a.ToList(); } break; case "price": { Array.Sort(a, Book.SortPrice()); this.books = a.ToList(); } break; } BookListStorage.SaveSortToFile(this.books); }
/// <summary> /// Remove <see cref="Book"/> from list. /// </summary> /// <param name="b"><see cref="Book"/> instance.</param> public void RemoveBook(Book b) { this.books.Remove(b); BookListStorage.RemoveFromFile(this.books); Program.Logger.Info("The book is successfully remove."); }
public static void AddBook() { Console.Write("ISBN: "); string isbn = Console.ReadLine(); Console.Write("Author: "); string author = Console.ReadLine(); Console.Write("Name: "); string name = Console.ReadLine(); Console.Write("Publishing: "); string publishing = Console.ReadLine(); int yearOfPublishing = 0; while (true) { try { Console.Write("Year of publishing: "); yearOfPublishing = Convert.ToInt32(Console.ReadLine(), CultureInfo.CreateSpecificCulture("en-US")); break; } catch (FormatException) { Console.WriteLine("Incorrect number format."); } } int countOfPages = 0; while (true) { try { Console.Write("Count of pages: "); countOfPages = Convert.ToInt32(Console.ReadLine(), CultureInfo.CreateSpecificCulture("en-US")); break; } catch (FormatException) { Console.WriteLine("Incorrect number format."); } } double price = 0; while (true) { try { Console.Write("Price: "); price = Convert.ToDouble(Console.ReadLine(), CultureInfo.CreateSpecificCulture("en-US")); break; } catch (FormatException) { Console.WriteLine("Incorrect number format."); } } Book b = new Book(isbn, author, name, publishing, yearOfPublishing, countOfPages, price); Program.BookListService.AddBook(b); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }