Ejemplo n.º 1
0
        /// <summary>
        /// Lets the user updade bookinput
        /// </summary>
        /// <param name="books">list of books</param>
        private static void InPutUpdateBook(List <Book> books)
        {
            Console.Write("Select member to update: ");
            string input          = Console.ReadLine();
            int    selectedNumber = int.Parse(input);
            Book   bookToUpdate   = books[selectedNumber - 1];

            Console.Write("Enter a title: ");
            bookToUpdate.Title = Console.ReadLine();

            Console.Write("Enter a language: ");
            bookToUpdate.Language = Console.ReadLine();

            Console.Write("Enter year of publication: ");
            input = Console.ReadLine();
            int.TryParse(input, out int yearOfPublication);
            bookToUpdate.YearOfPublication = yearOfPublication;

            Console.Write("Enter how many pages: ");
            input = Console.ReadLine();
            int.TryParse(input, out int pages);
            bookToUpdate.Pages = pages;

            Console.Write("Enter author: ");
            bookToUpdate.Author = Console.ReadLine();

            Console.Write("Enter number of copies: ");
            input = Console.ReadLine();
            int.TryParse(input, out int copies);
            bookToUpdate.Copies = copies;


            BookRepository.UpdateBookById(bookToUpdate.Id, bookToUpdate);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Lets the user loan a book
        /// </summary>
        private void LoanBookById()
        {
            Console.Clear();

            Member memberToLoan = SelectMember();
            Book   loanBook     = SelectBook();

            Console.Write("Enter a day you want to loan your book: ");
            string input = Console.ReadLine();

            DateTime.TryParse(input, out DateTime startDate);


            Console.Write("Enter when you want to return your book: ");
            input = Console.ReadLine();
            DateTime.TryParse(input, out DateTime endDate);

            List <Loan> loans = LoanRepository.GetLoans();

            bool loanAccepted       = false;
            int  startDateResultat  = DateTime.Compare(startDate, DateTime.Now.Date);
            int  numberOFBooksLoans = 0;

            try
            {
                for (int i = 0; i < loans.Count; i++)
                {
                    if (loans[i].BookRented != null)
                    {
                        if (loans[i].BookRented.Id == loanBook.Id)
                        {
                            //The following is not met: the new loan start date is later than the current loan end date OR the new loan end date is earlier than the current loan start date.
                            if (!(startDate > loans[i].EndDate || endDate < loans[i].StartDate))
                            {
                                numberOFBooksLoans++;
                            }
                        }
                    }
                }

                if (numberOFBooksLoans < loanBook.Copies)
                {
                    loanAccepted = true;
                }

                else
                {
                    throw new Exception("try another date");
                }

                if (startDateResultat < 0)
                {
                    Console.WriteLine($"You have to book {DateTime.Now.ToShortDateString()} or later ");
                    loanAccepted = false;
                }
            }
            catch (System.Exception)
            {
                Console.WriteLine("try another date");
            }

            if (loanAccepted == true)
            {
                BookRepository.UpdateBookById(loanBook.Id, loanBook);
                LoanRepository.LoanBook(loanBook, memberToLoan, startDate, endDate);
            }

            Console.WriteLine("Press enter");
            Console.ReadLine();
        }