private static void UpdateBook(LibraryInformationEntities context) { var bookData = (from e in context.Books select e).ToList(); BookBC book = new BookBC(); book.Display(bookData); Console.Write("\nSelect a book to update (0 to exit): "); //Get user input string userInput = Console.ReadLine(); //verify the user input is an int int.TryParse(userInput, out int selection); //search for the book that the user entered if (selection != 0) { var data = (from e in context.Books where e.BookID == selection select e).ToList(); Book b = data[0]; Console.Write($"ISBN currently set to {b.ISBN} enter a value if you wish to update (leave blank to ignore): "); string tempISBN = Console.ReadLine(); Console.Write($"Title currently set to {b.Title} enter a value if you wish to update (leave blank to ignore): "); string tempTitle = Console.ReadLine(); Console.Write($"Author currently set to {b.Author.Person.FirstName} {b.Author.Person.LastName} a new first name (leave blank to ignore): "); string tempFirstName = Console.ReadLine(); Console.Write("Enter a new last name (leave blank to ignore): "); string tempLastName = Console.ReadLine(); string first; string last; if (tempFirstName != "") { first = tempFirstName; } else { first = b.Author.Person.FirstName; } if (tempLastName != "") { last = tempLastName; } else { last = b.Author.Person.LastName; } Author tempAuthor = CreateAuthor(context, first, last); int tempIntNumPages; do { Console.Write($"NumPages currently set to {b.NumPages} enter a value if you wish to update (leave blank to ignore): "); string tempNumPages = Console.ReadLine(); if (tempNumPages != "") { int.TryParse(tempNumPages, out tempIntNumPages); if (tempIntNumPages == 0) { ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid entry, please only use numbers."); Console.ForegroundColor = foreground; } } else { tempIntNumPages = -1; break; } } while (tempIntNumPages == 0); Console.Write($"Subject currently set to {b.Subject} enter a value if you wish to update (leave blank to ignore): "); string tempSubject = Console.ReadLine(); Console.Write($"Description currently set to {b.Description} enter a value if you wish to update (leave blank to ignore): "); string tempDescription = Console.ReadLine(); Console.Write($"Publisher currently set to {b.Publisher} enter a value if you wish to update (leave blank to ignore): "); string tempPublisher = Console.ReadLine(); string tempYearPublished; do { Console.Write($"YearPublished currently set to {b.YearPublished} enter a value if you wish to update (leave blank to ignore): "); tempYearPublished = Console.ReadLine(); if (tempYearPublished != "") { if (tempYearPublished.Length != 4) { ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Year published must be 4 digits. Please enter a new year."); Console.ForegroundColor = foreground; } } else { tempYearPublished = "-1"; break; } } while (tempYearPublished.Length != 4); Console.Write($"Language currently set to {b.Language} enter a value if you wish to update (leave blank to ignore): "); string tempLanguage = Console.ReadLine(); int intTempNumberOfCopies; do { Console.Write($"NumberOfCopies currently set to {b.NumberOfCopies} enter a value if you wish to update (leave blank to ignore): "); string tempNumberOfCopies = Console.ReadLine(); if (tempNumberOfCopies != "") { int.TryParse(tempNumberOfCopies, out intTempNumberOfCopies); if (intTempNumberOfCopies == 0) { ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid entry, please only use numbers."); Console.ForegroundColor = foreground; } else { BookBC bookBC = new BookBC(); int numberOfBooksInStock = bookBC.CountInStockBooks(b); int numberOfBooksCheckedOut = b.NumberOfCopies - numberOfBooksInStock; if (numberOfBooksCheckedOut > intTempNumberOfCopies) { ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Invalid entry, there are currently {numberOfBooksCheckedOut} of those books checked out. Please enter a greater number."); Console.ForegroundColor = foreground; //set to 0 to stay in the loop intTempNumberOfCopies = 0; } } } else { intTempNumberOfCopies = -1; break; } } while (intTempNumberOfCopies == 0); Book tempBook = new Book(); if (tempISBN != "") { tempBook.ISBN = tempISBN; } else { tempBook.ISBN = b.ISBN; } if (tempTitle != "") { tempBook.Title = tempTitle; } else { tempBook.Title = b.Title; } tempBook.Author = tempAuthor; tempBook.AuthorID = tempAuthor.AuthorID; //tempBook.Author = b.Author; if (tempIntNumPages != -1) { tempBook.NumPages = tempIntNumPages; } else { tempBook.NumPages = b.NumPages; } if (tempSubject != "") { tempBook.Subject = tempSubject; } else { tempBook.Subject = b.Subject; } if (tempDescription != "") { tempBook.Description = tempDescription; } else { tempBook.Description = b.Description; } if (tempPublisher != "") { tempBook.Publisher = tempPublisher; } else { tempBook.Publisher = b.Publisher; } if (tempYearPublished != "-1") { tempBook.YearPublished = tempYearPublished; } else { tempBook.YearPublished = b.YearPublished; } if (tempLanguage != "") { tempBook.Language = tempLanguage; } else { tempBook.Language = b.Language; } if (intTempNumberOfCopies != -1) { tempBook.NumberOfCopies = intTempNumberOfCopies; } else { tempBook.NumberOfCopies = b.NumberOfCopies; } tempBook.BookID = b.BookID; string answer; do { Console.WriteLine("\nYou are going to replace this book:"); book.DetailedDisplay(b); Console.WriteLine("\nwith this book:"); book.DetailedDisplay(tempBook); Console.Write("\nAre you sure you wish to continue? (enter 'y' to confirm): "); answer = Console.ReadLine(); if (answer.ToUpper() == "Y") { //Commit the change to the database //context.Books.Attach(b); //b = tempBook; //context.Entry(b).State = EntityState.Modified; //context.Entry(b).State = b.BookID == 0 ? // EntityState.Added : // EntityState.Modified; //b = tempBook; //context.Books.Attach(context.Books.Single(c => c.BookID == tempBook.BookID)); //context.Books.ApplyCurrentValues(tempBook); var updateBook = context.Books.FirstOrDefault(c => c.BookID == tempBook.BookID); updateBook.ISBN = tempBook.ISBN; updateBook.Title = tempBook.Title; updateBook.AuthorID = tempBook.AuthorID; updateBook.NumPages = tempBook.NumPages; updateBook.Subject = tempBook.Subject; updateBook.Description = tempBook.Description; updateBook.Publisher = tempBook.Publisher; updateBook.YearPublished = tempBook.YearPublished; updateBook.Language = tempBook.Language; updateBook.NumberOfCopies = tempBook.NumberOfCopies; //updateBook = tempBook; //b = tempBook; //data[0] = tempBook; try { context.SaveChanges(); ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Successfully updated book in the system."); Console.ForegroundColor = foreground; } catch (Exception e) { ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Failed to update book in the system. {e.InnerException.InnerException.Message}"); Console.ForegroundColor = foreground; } } else { Console.WriteLine("\nOperation canceled, returning to main menu"); break; } } while (answer.ToUpper() != "Y"); Console.WriteLine("\nPress enter to continue."); Console.ReadLine(); } Console.Clear(); }
private static void RemoveBook(LibraryInformationEntities context) { //get ISBN Console.Write("Enter the book's ISBN: "); string isbn = Console.ReadLine(); var bookData = (from e in context.Books where e.ISBN == isbn select e).ToList(); if (bookData.Count > 0) { Book b = bookData[0]; BookBC tempBook = new BookBC(); int inStockBooks = tempBook.CountInStockBooks(b); int copiesCheckedOut = b.NumberOfCopies - inStockBooks; //how many to remove int intNumberToRemove; do { Console.WriteLine($"There are currently {copiesCheckedOut} checked out and {b.NumberOfCopies} total copies."); Console.Write("Please enter the number of copies that you want to remove from the system: "); string strNumberToRemove = Console.ReadLine(); int.TryParse(strNumberToRemove, out intNumberToRemove); if (b.NumberOfCopies - intNumberToRemove >= 0) { if (b.NumberOfCopies - intNumberToRemove >= copiesCheckedOut) { if (b.NumberOfCopies - intNumberToRemove == 0) { //call delete context.Books.Remove(b); try { context.SaveChanges(); ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Successfully removed the book from the system."); Console.ForegroundColor = foreground; } catch (Exception e) { ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Failed to remove book from the system. {e.InnerException.InnerException.Message}"); Console.ForegroundColor = foreground; } } else { //call update var updateBook = context.Books.FirstOrDefault(c => c.BookID == b.BookID); updateBook.NumberOfCopies = b.NumberOfCopies - intNumberToRemove; try { context.SaveChanges(); ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Successfully removed {intNumberToRemove} books from the system."); Console.ForegroundColor = foreground; } catch (Exception e) { ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Failed to update book in the system. {e.InnerException.InnerException.Message}"); Console.ForegroundColor = foreground; } } } else { //re-enter, result can't be less than number checked out ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("The number you entered is invalid. There are too many books checked out. Please try again."); Console.ForegroundColor = foreground; intNumberToRemove = -1; } } else { //re-enter, can't be less than 0 ConsoleColor foreground = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("The number you entered is greater than the total number of books. Please try again."); Console.ForegroundColor = foreground; intNumberToRemove = -1; } } while (intNumberToRemove == -1); Console.Write("Press enter to continue."); Console.ReadLine(); Console.Clear(); //do math //update or delete } }
private static void CheckOutBook(LibraryInformationEntities context) { string checkOut = "out"; //Creates a helper to get the ISBN and CardID from the librarian CheckInOutHelper helper = CheckInOutHelper(checkOut); //Finds the book of the ISBN entered var bookData = (from e in context.Books where e.ISBN == helper.Isbn select e).ToList(); if (bookData.Count > 0) { //check if copies of the book are in stock BookBC book = new BookBC(); int booksInStock = book.CountInStockBooks(bookData[0]); if (booksInStock > 0) { //Finds the card holder of the CardID entered var cardHolderData = (from e in context.Cardholders where e.LibraryCardID == helper.CardID select e).ToList(); //finds the checkout log entries for the card holder int cardholderID = cardHolderData[0].CardHolderID; var CheckOutData = (from e in context.CheckOutLogs where e.CardholderID == cardholderID select e).ToList(); if (CheckOutData.Count > 0) { foreach (CheckOutLog col in CheckOutData) { DateTime time = DateTime.Now; //check if user has overdue book (30 days) if (col.CheckOutDate > time.AddDays(-30)) { //Allow book to be checked out //Stage the changes to the bookToBeCheckedOut CheckOutLog object CheckOutLog bookToBeCheckedOut = new CheckOutLog { BookID = bookData[0].BookID, CardholderID = cardholderID, CheckOutDate = DateTime.Now }; //Add the record to the database and save context.CheckOutLogs.Add(bookToBeCheckedOut); context.SaveChanges(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Successfully checked out book."); Console.WriteLine("\nPress enter to continue."); Console.ReadLine(); Console.Clear(); break; } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"{cardHolderData[0].Person.FirstName} {cardHolderData[0].Person.LastName} has an overdue book and is not allowed to checkout books at this time.\n"); Console.WriteLine("\nPress enter to continue."); Console.ReadLine(); Console.Clear(); } } } else { //user has no checked out books and is allowed to checkout the book //Stage the changes to the bookToBeCheckedOut CheckOutLog object CheckOutLog bookToBeCheckedOut = new CheckOutLog { BookID = bookData[0].BookID, CardholderID = cardholderID, CheckOutDate = DateTime.Now }; //Add the record to the database and save context.CheckOutLogs.Add(bookToBeCheckedOut); context.SaveChanges(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Successfully checked out book."); Console.WriteLine("\nPress enter to continue."); Console.ReadLine(); Console.Clear(); } } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"{book.Title} is not in stock, please check back later."); Console.WriteLine("\nPress enter to continue."); Console.ReadLine(); Console.Clear(); } } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Book not found in the database, please try again."); Console.WriteLine("\nPress enter to continue."); Console.ReadLine(); Console.Clear(); } }