예제 #1
0
        /// <summary>
        /// Checks does we have input book in local collection.
        /// </summary>
        /// <param name="book"></param>
        /// <returns></returns>
        private bool HaveBookInCol(Book book)
        {
            if (book == null)
            {
                logger.Error("User try to check Book which is null.");
                throw new ArgumentNullException("Try to check book, which is null.");
            }

            return(this.isbnBook.ContainsKey(book.ISBNGet()));
        }
예제 #2
0
        public void AddBook(Book newBook)
        {
            logger.Trace($"Start adding Book.");
            if (newBook == null)
            {
                logger.Error("User try to add Book which is null");
                throw new ArgumentNullException("newBook is null!");
            }

            if (this.HaveBookInCol(newBook))
            {
                logger.Error($"User doesnt have a Book {newBook.ISBNGet()} in isbnBook.");
                throw new ArgumentException("Have this book in the collection.");
            }

            logger.Trace($"Add Book {newBook.ISBNGet()} in isbnBook, collection, booksToAdd.");
            this.booksToAdd.Add(newBook);
            this.collection.Add(newBook);
            this.isbnBook[newBook.ISBNGet()] = newBook;
        }
예제 #3
0
        /// <summary>
        /// Used by RemoveBook.
        /// Removes from local collection, from booksToAdd and from isbnBook.
        /// </summary>
        /// <param name="book"></param>
        private void RemoveFromCollections(Book book)
        {
            if (book == null)
            {
                logger.Error("User try to delete Book which is null.");
                throw new ArgumentNullException();
            }

            try
            {
                logger.Trace($"Remove Book {book.ISBNGet()} from collection, booksToAdd, isbnBook.");
                collection.Remove(book);
                booksToAdd.Remove(book);
                isbnBook.Remove(book.ISBNGet());
            }
            catch (Exception e)
            {
                logger.Error($"Error while removing Book {book.ISBNGet()}.\nError message: \n{e.ToString()}.");
            }
        }
예제 #4
0
        /// <summary>
        /// Removes from local and adds in booksToRemove.
        /// </summary>
        /// <param name="removeBook"></param>
        public void RemoveBook(Book removeBook)
        {
            logger.Trace($"Remove Book from collection.");
            if (!this.HaveBookInCol(removeBook))
            {
                logger.Error($"User doesnt have a Book {removeBook.ISBNGet()} in isbnBook.");
                throw new ArgumentException("Dont have this book in the collection.");
            }

            this.RemoveFromCollections(removeBook);
            this.booksToRemove.Add(removeBook);
        }
예제 #5
0
        public static void Main(string[] args)
        {
            Console.WriteLine("*****Test Book's Service*****");
            Console.WriteLine("1 - Create books!");
            Book.Book first = new Book.Book()
            {
                Author        = "Rob Miles",
                Name          = "C# Programming Yellow Book",
                Cost          = int.MaxValue,
                NumberOfPages = 216,
                Publisher     = "Hell University",
                Year          = 2016,
            };
            Console.WriteLine("Create 1!");
            Book.Book second = new Book.Book()
            {
                Author        = "Andrew Stellman, Jennifer Greene",
                Name          = "Head First C#",
                Cost          = 39M,
                NumberOfPages = 960,
                Publisher     = "O'Reilly Media",
                Year          = 2013,
            };
            second.ISBNSet("9780596514822");
            Console.WriteLine("Create 2!");
            Book.Book thirdDel = new Book.Book()
            {
                Author        = "Joe Albahari",
                Name          = "C# 7.0 in a Nutshell",
                Cost          = 66.23M,
                NumberOfPages = 800,
                Publisher     = "Альфа-книга",
                Year          = 2015,
            };
            thirdDel.ISBNSet("9785990944619");

            Console.WriteLine("Create 3!");
            Book.Book fourthAdd = new Book.Book()
            {
                Author        = "Jon Skeet",
                Name          = "C# in Depth",
                Cost          = 28.5M,
                NumberOfPages = 528,
                Publisher     = "Manning Publications",
                Year          = 2019,
            };
            fourthAdd.ISBNSet("978 - 1617294532");

            Console.WriteLine("Create 4!");
            Book.Book fifth = new Book.Book()
            {
                Author        = "Jeffrey Richter",
                Name          = "CLR via C#",
                Cost          = 32.52M,
                NumberOfPages = 896,
                Publisher     = "Microsoft Press",
                Year          = 2012,
            };
            fifth.ISBNSet("978 - 0735667457");

            Console.WriteLine("Create 5!");
            Console.WriteLine("End of creation!\nLets create BookListService!");
            Book.Book[]     massiv  = new Book.Book[] { first, second, thirdDel, fifth };
            BookListService service = new BookListService(massiv, new BinaryStorage());

            Console.WriteLine("Let's throw books in the local storage");
            service.UpdateStorage();
            Console.WriteLine("Let's add book in service!");
            service.AddBook(fourthAdd);
            Console.WriteLine("Let's update the local storage!");
            service.UpdateStorage();
            Console.WriteLine("Find right: " +
                              (service.FindBookByTag("Author", "Joe Albahari")[0].ISBNGet() == thirdDel.ISBNGet()));
            Console.WriteLine("Let's sort our storage!");
            Console.WriteLine("Let's delete book from service!");
            service.RemoveBook(thirdDel);
            Console.WriteLine("Let's sort books by everything!");
            service.SortBooksByTag(new BookYearComparer <Book.Book>());
            Console.WriteLine("By Year:");
            service.ShowLocal();
            service.SortBooksByTag(new BookCostComparer <Book.Book>());
            Console.WriteLine("By cost:");
            service.ShowLocal();
            Console.WriteLine("In storage:");
            service.ShowStorage();
            Console.WriteLine("End of demonstration book service!\n");

            Watches.Watch watch = new Watches.Watch();
            watch.StartRelaxation();
        }