#include <iostream> #include <vector> #include <string> #include <ctime> // for handling dates using namespace std; const int BORROW_PERIOD = 7; // Borrow period in days // Define structures for Book and Student struct Book { string name; string author; bool isAvailable; time_t borrowDate; // Date when book was borrowed time_t returnDate; // Date when book was returned }; struct Student { int id; string name; string course; string department; string college; string contact; string email; vector<string> borrowedBooks; vector<time_t> dueDates; // Due dates of borrowed books }; // Function prototypes void addBook(vector<Book> &library); void addStudent(vector<Student> &students); void borrowBooks(vector<Book> &library, vector<Student> &students); void returnBook(vector<Book> &library, vector<Student> &students); void displayAvailableBooks(const vector<Book> &library); void recommendBooks(const Student &student); void generateReports(const vector<Book> &library, const vector<Student> &students); int main() { vector<Book> library; vector<Student> students; while (true) { int choice; cout << "=======***Library Management System***=======" << endl; cout << "\n"; cout << "\t 1. Add New Book" << endl; cout << "\t 2. Add New Student" << endl; cout << "\t 3. Borrow A Book" << endl; cout << "\t 4. Return A Book" << endl; // Added option to return a book cout << "\t 5. Display Available Books" << endl; cout << "\t 6. Get Book Recommendations" << endl; cout << "\t 7. Generate Reports" << endl; cout << "\t 8. Exit" << endl; // Fixed the option number cout << "\n"; cout << "Enter Your Choice: "; cin >> choice; switch (choice) { case 1: addBook(library); break; case 2: addStudent(students); break; case 3: borrowBooks(library, students); break; case 4: returnBook(library, students); break; case 5: displayAvailableBooks(library); break; case 6: // Assuming the student is logged in recommendBooks(students[0]); break; case 7: generateReports(library, students); break; case 8: cout << "Exiting program..." << endl; return 0; default: cout << "Invalid choice. Please try again." << endl; } } return 0; } void addBook(vector<Book> &library) { Book newBook; cout << "Enter Book Name: "; cin.ignore(); getline(cin, newBook.name); cout << "Enter Book Author: "; getline(cin, newBook.author); newBook.isAvailable = true; newBook.borrowDate = 0; // Initializing borrow date newBook.returnDate = 0; // Initializing return date library.push_back(newBook); cout << "Book Added Successfully." << endl; } void addStudent(vector<Student> &students) { Student newStudent; cout << "Enter Student Name: "; cin.ignore(); getline(cin, newStudent.name); cout << "Enter Student ID: "; cin >> newStudent.id; cout << "Enter Student Course: "; cin.ignore(); getline(cin, newStudent.course); cout << "Enter Student College Department: "; getline(cin, newStudent.department); cout << "Enter School Name: "; // Corrected typo getline(cin, newStudent.college); cout << "Enter Student Contact Number: "; getline(cin, newStudent.contact); cout << "Enter Student Email: "; getline(cin, newStudent.email); students.push_back(newStudent); cout << "Student Added Successfully." << endl; } void borrowBooks(vector<Book> &library, vector<Student> &students) { int studentID; cout << "Enter student ID: "; cin >> studentID; // Find student by ID bool studentFound = false; size_t studentIndex; for (size_t i = 0; i < students.size(); ++i) { if (students[i].id == studentID) { studentFound = true; studentIndex = i; break; } } if (!studentFound) { cout << "Student Not Found." << endl; return; } cout << "Enter Book Name To Borrow: "; string bookName; cin.ignore(); getline(cin, bookName); // Find book by name bool bookFound = false; size_t bookIndex; for (size_t i = 0; i < library.size(); ++i) { if (library[i].name == bookName && library[i].isAvailable) { bookFound = true; bookIndex = i; break; } } if (!bookFound) { cout << "Book not found or not available for borrowing." << endl; return; } // Set book as borrowed and update borrow date library[bookIndex].isAvailable = false; library[bookIndex].borrowDate = time(nullptr); // Calculate due date time_t dueDate = time(nullptr) + BORROW_PERIOD * 86400; // 86400 seconds in a day students[studentIndex].dueDates.push_back(dueDate); // Update student's borrowed books students[studentIndex].borrowedBooks.push_back(bookName); cout << "Book '" << bookName << "' borrowed successfully." << endl; cout << "Due date: " << ctime(&dueDate); } void returnBook(vector<Book> &library, vector<Student> &students) { // Implementation for returning a book int studentID; cout << "Enter Student ID: "; cin >> studentID; // Find student by ID bool studentFound = false; size_t studentIndex; for (size_t i = 0; i < students.size(); ++i) { if (students[i].id == studentID) { studentFound = true; studentIndex = i; break; } } if (!studentFound) { cout << "Student Not Found." << endl; return; } cout << "Enter Book Name To Return: "; string bookName; cin.ignore(); getline(cin, bookName); // Find book by name in student's borrowed books bool bookFound = false; for (size_t i = 0; i < students[studentIndex].borrowedBooks.size(); ++i) { if (students[studentIndex].borrowedBooks[i] == bookName) { bookFound = true; // Find book index in library for (size_t j = 0; j < library.size(); ++j) { if (library[j].name == bookName) { // Mark book as available and update return date library[j].isAvailable = true; library[j].returnDate = time(nullptr); cout << "Book '" << bookName << "' returned successfully." << endl; break; } } // Remove book from student's borrowed books students[studentIndex].borrowedBooks.erase(students[studentIndex].borrowedBooks.begin() + i); // Remove due date corresponding to the returned book students[studentIndex].dueDates.erase(students[studentIndex].dueDates.begin() + i); break; } } if (!bookFound) { cout << "Book not found in student's borrowed books." << endl; } } void displayAvailableBooks(const vector<Book> &library) { cout << "Available books in the library:" << endl; for (const auto &book : library) { if (book.isAvailable) { cout << "Book Name: " << book.name << ", Author: " << book.author << endl; } } } void recommendBooks(const Student &student) { // Implementation for recommending books based on borrowing history } void generateReports(const vector<Book> &library, const vector<Student> &students) { // Implementation for generating reports }
function that: add two numbers together ```python def add(a, b): return a + b add(1, 2) ``` function that: #include <iostream> #include <vector> #include <string> #include <ctime> // for handling dates using namespace std; const int BORROW_PERIOD = 7; // Borrow period in days // Define structures for Book and Student struct Book { string name; string author; bool isAvailable; time_t borrowDate; // Date when book was borrowed time_t returnDate; // Date when book was returned }; struct Student { int id; string name; string course; string department; string college; string contact; string email; vector<string> borrowedBooks; vector<time_t> dueDates; // Due dates of borrowed books }; // Function prototypes void addBook(vector<Book> &library); void addStudent(vector<Student> &students); void borrowBooks(vector<Book> &library, vector<Student> &students); void returnBook(vector<Book> &library, vector<Student> &students); void displayAvailableBooks(const vector<Book> &library); void recommendBooks(const Student &student); void generateReports(const vector<Book> &library, const vector<Student> &students); int main() { vector<Book> library; vector<Student> students; while (true) { int choice; cout << "=======***Library Management System***=======" << endl; cout << "\n"; cout << "\t 1. Add New Book" << endl; cout << "\t 2. Add New Student" << endl; cout << "\t 3. Borrow A Book" << endl; cout << "\t 4. Return A Book" << endl; // Added option to return a book cout << "\t 5. Display Available Books" << endl; cout << "\t 6. Get Book Recommendations" << endl; cout << "\t 7. Generate Reports" << endl; cout << "\t 8. Exit" << endl; // Fixed the option number cout << "\n"; cout << "Enter Your Choice: "; cin >> choice; switch (choice) { case 1: addBook(library); break; case 2: addStudent(students); break; case 3: borrowBooks(library, students); break; case 4: returnBook(library, students); break; case 5: displayAvailableBooks(library); break; case 6: // Assuming the student is logged in recommendBooks(students[0]); break; case 7: generateReports(library, students); break; case 8: cout << "Exiting program..." << endl; return 0; default: cout << "Invalid choice. Please try again." << endl; } } return 0; } void addBook(vector<Book> &library) { Book newBook; cout << "Enter Book Name: "; cin.ignore(); getline(cin, newBook.name); cout << "Enter Book Author: "; getline(cin, newBook.author); newBook.isAvailable = true; newBook.borrowDate = 0; // Initializing borrow date newBook.returnDate = 0; // Initializing return date library.push_back(newBook); cout << "Book Added Successfully." << endl; } void addStudent(vector<Student> &students) { Student newStudent; cout << "Enter Student Name: "; cin.ignore(); getline(cin, newStudent.name); cout << "Enter Student ID: "; cin >> newStudent.id; cout << "Enter Student Course: "; cin.ignore(); getline(cin, newStudent.course); cout << "Enter Student College Department: "; getline(cin, newStudent.department); cout << "Enter School Name: "; // Corrected typo getline(cin, newStudent.college); cout << "Enter Student Contact Number: "; getline(cin, newStudent.contact); cout << "Enter Student Email: "; getline(cin, newStudent.email); students.push_back(newStudent); cout << "Student Added Successfully." << endl; } void borrowBooks(vector<Book> &library, vector<Student> &students) { int studentID; cout << "Enter student ID: "; cin >> studentID; // Find student by ID bool studentFound = false; size_t studentIndex; for (size_t i = 0; i < students.size(); ++i) { if (students[i].id == studentID) { studentFound = true; studentIndex = i; break; } } if (!studentFound) { cout << "Student Not Found." << endl; return; } cout << "Enter Book Name To Borrow: "; string bookName; cin.ignore(); getline(cin, bookName); // Find book by name bool bookFound = false; size_t bookIndex; for (size_t i = 0; i < library.size(); ++i) { if (library[i].name == bookName && library[i].isAvailable) { bookFound = true; bookIndex = i; break; } } if (!bookFound) { cout << "Book not found or not available for borrowing." << endl; return; } // Set book as borrowed and update borrow date library[bookIndex].isAvailable = false; library[bookIndex].borrowDate = time(nullptr); // Calculate due date time_t dueDate = time(nullptr) + BORROW_PERIOD * 86400; // 86400 seconds in a day students[studentIndex].dueDates.push_back(dueDate); // Update student's borrowed books students[studentIndex].borrowedBooks.push_back(bookName); cout << "Book '" << bookName << "' borrowed successfully." << endl; cout << "Due date: " << ctime(&dueDate); } void returnBook(vector<Book> &library, vector<Student> &students) { // Implementation for returning a book int studentID; cout << "Enter Student ID: "; cin >> studentID; // Find student by ID bool studentFound = false; size_t studentIndex; for (size_t i = 0; i < students.size(); ++i) { if (students[i].id == studentID) { studentFound = true; studentIndex = i; break; } } if (!studentFound) { cout << "Student Not Found." << endl; return; } cout << "Enter Book Name To Return: "; string bookName; cin.ignore(); getline(cin, bookName); // Find book by name in student's borrowed books bool bookFound = false; for (size_t i = 0; i < students[studentIndex].borrowedBooks.size(); ++i) { if (students[studentIndex].borrowedBooks[i] == bookName) { bookFound = true; // Find book index in library for (size_t j = 0; j < library.size(); ++j) { if (library[j].name == bookName) { // Mark book as available and update return date library[j].isAvailable = true; library[j].returnDate = time(nullptr); cout << "Book '" << bookName << "' returned successfully." << endl; break; } } // Remove book from student's borrowed books students[studentIndex].borrowedBooks.erase(students[studentIndex].borrowedBooks.begin() + i); // Remove due date corresponding to the returned book students[studentIndex].dueDates.erase(students[studentIndex].dueDates.begin() + i); break; } } if (!bookFound) { cout << "Book not found in student's borrowed books." << endl; } } void displayAvailableBooks(const vector<Book> &library) { cout << "Available books in the library:" << endl; for (const auto &book : library) { if (book.isAvailable) { cout << "Book Name: " << book.name << ", Author: " << book.author << endl; } } } void recommendBooks(const Student &student) { // Implementation for recommending books based on borrowing history } void generateReports(const vector<Book> &library, const vector<Student> &students) { // Implementation for generating reports } ```C++