Untitled

 avatar
unknown
c_cpp
5 months ago
5.5 kB
5
Indexable
void returnBook() {
    int borrowerID, bookID;
    string returnDate;

    // Input Borrower ID
    cout << "Enter Borrower ID: ";
    cin >> borrowerID;

    // Check if the Borrower ID is valid
    bool borrowerFound = false;
    for (auto& borrower : borrowers) {
        if (borrower.id == borrowerID) {
            borrowerFound = true;
            break;
        }
    }

    if (!borrowerFound) {
        cout << "Error: Borrower ID not found. Please enter a valid Borrower ID.\n";
        return;  // Exit the function if Borrower ID is invalid
    }

    // Input Book ID
    cout << "Enter Book ID: ";
    cin >> bookID;

    // Check if the Book ID is valid and belongs to the borrower
    bool bookFound = false;
    for (auto& borrower : borrowers) {
        if (borrower.id == borrowerID && !borrower.borrowedBooks.empty()) {
            // Check if the borrower has borrowed the book
            for (auto& bookDetails : borrower.borrowedBooks) {
                if (bookDetails.title == to_string(bookID)) {  // Assuming bookID is stored in the book title
                    bookFound = true;
                    break;
                }
            }
        }

        if (bookFound) break;  // Exit the loop once the book is found
    }

    if (!bookFound) {
        cout << "Error: Book ID not found in the borrowed books or borrower has not borrowed this book.\n";
        return;  // Exit the function if the book is not found in the borrower's list
    }

    // Input Return Date
    cin.ignore();  // To ignore the newline character left by cin
    cout << "Enter Date of Return (YYYY-MM-DD): ";
    getline(cin, returnDate);

    // Check if the date is valid
    if (!isValidDate(returnDate)) {
        cout << "Invalid date format. Please enter a valid date (YYYY-MM-DD).\n";
        return;  // Exit the function if the date format is invalid
    }

    // Handle the return of the book
    for (auto& borrower : borrowers) {
        if (borrower.id == borrowerID) {
            // Find the borrowed book by matching bookID
            BorrowedBookDetails* borrowedBook = nullptr;
            for (auto& bookDetails : borrower.borrowedBooks) {
                if (bookDetails.title == to_string(bookID)) {  // Assuming bookID is stored in the book title
                    borrowedBook = &bookDetails;
                    break;
                }
            }

            if (borrowedBook) {  // If book was found in borrowedBooks
                int overdueFee = calculateOverdueFee(borrower.borrowedBooks[0].dateBorrow, returnDate);  // Calculate the overdue fee
                borrowedBook->dateReturn = returnDate;
                borrower.overdueFee = overdueFee;

                if (overdueFee == 0) {
                    // Case 1: On-time return
                    cout << "Book returned SUCCESSFULLY.\n";

                    displayBorrowerTableHeader();

                    // Display the borrower details using displayBorrowerTable
                    displayBorrowerTable(borrower);
                } else {
                    // Case 2: Late return with fee
                    cout << "Book returned SUCCESSFULLY. But, you need to pay for not following the rules.\n";
                    cout << "\n--- E-Receipt ---\n";
                    cout << "Borrower's Name: " << borrower.firstName + " " + borrower.middleInitial + " " + borrower.lastName << "\n";
                    cout << "Book Title: " << borrowedBook->title << "\n";
                    cout << "Date Borrowed: " << borrower.borrowedBooks[0].dateBorrow << "\n";
                    cout << "Date Returned: " << borrowedBook->dateReturn << "\n";
                    cout << "Overdue Fee: " << overdueFee << " pesos\n";
                    cout << "------------------\n";
                }

                // Return the book to inventory
                bool bookFoundInInventory = false;
                for (auto& category : {fictionBooks, nonFictionBooks, scienceBooks, mysteryBooks, romanceBooks,
                                       biographyBooks, historyBooks, technologyBooks, childrenBooks, artBooks}) {
                    // Search for the book in each category
                    for (auto& book : category) {
                        if (book.id == bookID) {
                            borrower.borrowedBooks.erase(remove_if(borrower.borrowedBooks.begin(), borrower.borrowedBooks.end(),
                                [&bookID](const BorrowedBookDetails& bookDetails) {
                                    return bookDetails.title == to_string(bookID);  // Assuming bookID is stored in the book title
                                }), borrower.borrowedBooks.end());  // Remove the returned book
                            bookFoundInInventory = true;
                            break;
                        }
                    }

                    if (bookFoundInInventory) {
                        break;  // Exit the outer loop once the book is found
                    }
                }

                if (!bookFoundInInventory) {
                    cout << "Book not found in the library inventory.\n";
                }

                return;
            }
        }
    }

    // If borrower or book ID not found
    cout << "Borrower ID or Book ID not found, or the borrower did not borrow any books.\n";
}
Editor is loading...
Leave a Comment