return

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

    // Input borrower and book IDs and the return date
    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 is not valid.\n";
        return;
    }

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

    // Check if the book ID is valid in any book category
    bool bookFound = false;
    for (auto& category : {fictionBooks, nonFictionBooks, scienceBooks, mysteryBooks, romanceBooks,
                           biographyBooks, historyBooks, technologyBooks, childrenBooks, artBooks}) {
        for (auto& book : category) {
            if (book.id == bookID) {
                bookFound = true;
                break;
            }
        }
        if (bookFound) {
            break;
        }
    }

    if (!bookFound) {
        cout << "Error: Book ID is not valid or not found in the inventory.\n";
        return;
    }

    cin.ignore();
    cout << "Enter Date of Return (YYYY-MM-DD): ";
    getline(cin, returnDate);

    // Iterate through the list of borrowers
    for (auto& borrower : borrowers) {
        if (borrower.id == borrowerID && !borrower.borrowedBooks.empty()) {  // Check if the borrower exists and has borrowed books
            bool bookFoundInBorrowedBooks = false;

            // Find the book that matches the bookID in the borrower's list of borrowed books
            for (auto& borrowedBook : borrower.borrowedBooks) {
                if (borrowedBook.title == "") {
                    continue;
                }
                if (bookFoundInBorrowedBooks) break;
                if (bookFoundInBorrowedBooks = borrowedBook.title == borrowedBook.title) {

                    int overdueFee = calculateOverdueFee(borrowedBook.dateBorrow, returnDate);  // Calculate the overdue fee
                    borrowedBook.dateReturn = returnDate;  // Set the return date
                    borrower.overdueFee = overdueFee;  // Update the borrower's overdue fee

                    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: " << borrowedBook.dateBorrow << "\n";
                        cout << "Date Returned: " << borrowedBook.dateReturn << "\n";
                        cout << "Overdue Fee: " << overdueFee << " pesos\n";
                        cout << "------------------\n";
                    }

                    // Return the book to inventory
                    for (auto& book : fictionBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : nonFictionBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : scienceBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : mysteryBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : romanceBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : biographyBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : historyBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : technologyBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : childrenBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    for (auto& book : artBooks) {  // Repeat for all categories
                        if (book.id == bookID) {
                            book.copies++;  // Increase the available copies
                            borrowedBook.title = "";  // Clear the borrowed book record
                            return;
                        }
                    }

                    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