This code is a simple library management system. It allows...

December 7, 2023 at 11:03 PM

#include <iostream> #include <string> #include <vector> using namespace std; class Book { public: string title; string author; int year; // Constructor Book(string t, string a, int y) : title(t), author(a), year(y) {} }; class User { public: string username; int userID; // Constructor User(string u, int id) : username(u), userID(id) {} }; class Library { public: vector<Book> books; vector<User> users; // Function to display the book inventory void displayBooks() { cout << "Book Inventory:\n"; for (const auto& book : books) { cout << "Title: " << book.title << "\nAuthor: " << book.author << "\nYear: " << book.year << "\n\n"; } } // Function to display the book inventory for adding new books void displayBooksForAddNewBooks() { cout << "Book Inventory for Adding New Books:\n"; for (const auto& book : books) { cout << "Title: " << book.title << "\nAuthor: " << book.author << "\nYear: " << book.year << "\n\n"; } } // Function to borrow a book void borrowBook(int userID) { displayBooks(); string titleToBorrow; cout << "Enter the title of the book you want to borrow: "; cin.ignore(); // Ignore newline character left in the buffer getline(cin, titleToBorrow); for (auto& book : books) { if (book.title == titleToBorrow) { cout << "Book borrowed successfully by User ID " << userID << "!\n"; // Update book quantity or perform other actions as needed return; } } cout << "Book not available for borrowing or does not exist.\n"; } // Function to add a new book void addNewBook() { displayBooksForAddNewBooks(); string newTitle, newAuthor, newBooks2023; int newYear; cout << "Enter the title of the new book: "; cin.ignore(); // Ignore newline character left in the buffer getline(cin, newTitle); cout << "Enter the author of the new book: "; getline(cin, newAuthor); cout << "Enter the publication year of the new book: "; cin >> newYear; books.emplace_back(newTitle, newAuthor, newYear); cout << "New book added to the inventory!\n"; } // Function to return a borrowed book void returnBook(int userID) { displayBooks(); string titleToReturn; cout << "Enter the title of the book you want to return: "; cin.ignore(); // Ignore newline character left in the buffer getline(cin, titleToReturn); for (auto& book : books) { if (book.title == titleToReturn) { cout << "Book returned successfully by User ID " << userID << "!\n"; // Update book quantity or perform other actions as needed return; } } cout << "Book not found in the inventory.\n"; } }; // Function to clear the console screen void clearScreen() { #ifdef _WIN32 system("cls"); // For Windows #else system("clear"); // For Linux #endif } bool login(const string& correctUsername, const string& correctPassword, int& userIDCounter) { string enteredUsername, enteredPassword; int attempts = 0; do { if (attempts > 0) { cout << "Invalid login credentials. Please try again.\n"; } cout << "Enter username: "; cin >> enteredUsername; cout << "Enter password: "; cin >> enteredPassword; attempts++; } while (enteredUsername != correctUsername || enteredPassword != correctPassword); cout << "Login successful!\n"; cout << "Welcome to the Library System, User ID: " << userIDCounter << "!\n"; return true; } char chooseRole() { char role; cout << "Choose your role:\n"; cout << "U. User\n"; cout << "A. Admin\n"; cout << "Enter your choice: "; cin >> role; return role; } int main() { char role = chooseRole(); string correctUsername, correctPassword; if (role == 'U' || role == 'u') { correctUsername = "user"; correctPassword = "password"; } else if (role == 'A' || role == 'a') { correctUsername = "admin"; correctPassword = "adminpass"; } else { cout << "Invalid role. Exiting the system.\n"; return 0; } int userIDCounter = 1000; // Start user IDs from 1000 if (login(correctUsername, correctPassword, userIDCounter)) { Library library; // Initialize the book inventory library.books.emplace_back("Harrow the Ninth", "Tamsyn Muir", 2022); library.books.emplace_back("Upright Women Wanted", "Sarah Gailey", 2020); library.books.emplace_back("The Mother Code", "Carole Stivers", 2020); library.books.emplace_back("To Sleep in a Sea of Stars", "Christopher Paolini", 2020); library.books.emplace_back("The Hero of Ages: Book Three of Mistborn", "Brandon Sanderson", 2020); library.books.emplace_back("Stranger Things", "Brenna Yovanoff", 2020); library.books.emplace_back("Batman: The Dark Knight", "Frank Miller", 2020); library.books.emplace_back("WolfWalkers", "Samuel Sattin", 2020); vector<Book> newBooks2023; newBooks2023.emplace_back("Harrow the Ninth", "Tamsyn Muir", 2023); newBooks2023.emplace_back("Upright Women Wanted", "Sarah Gailey", 2023); newBooks2023.emplace_back("The Mother Code", "Carole Stivers", 2023); newBooks2023.emplace_back("To Sleep in a Sea of Stars", "Christopher Paolini", 2023); newBooks2023.emplace_back("The Hero of Ages: Book Three of Mistborn", "Brandon Sanderson", 2023); newBooks2023.emplace_back("Stranger Things", "Brenna Yovanoff", 2023); newBooks2023.emplace_back("Batman: The Dark Knight", "Frank Miller", 2023); newBooks2023.emplace_back("WolfWalkers", "Samuel Sattin", 2023); int userChoice; do { cout << "\nMenu:\n"; cout << "1. Borrow a Book\n"; cout << "2. Display Book Inventory\n"; cout << "3. Add New Book\n"; cout << "4. Return a Book\n"; cout << "5. Exit\n"; cout << "Enter your choice: "; cin >> userChoice; switch (userChoice) { case 1: library.borrowBook(userIDCounter); break; case 2: library.displayBooks(); break; case 3: cout << "Select a book from the new inventory (2023) to add:\n"; for (const auto& book : newBooks2023) { cout << "Title: " << book.title << "\nAuthor: " << book.author << "\nYear: " << book.year << "\n\n"; } library.AddNewBook(newBooks2023); break; case 4: library.returnBook(userIDCounter); break; case 5: cout << "Exiting the system.\n"; break; default: cout << "Invalid choice. Please try again.\n"; break; } } while (userChoice != 5); } return 0; }

This code is a simple library management system. It allows users to log in as either a user or an admin, and perform various operations such as borrowing a book, displaying the book inventory, adding a new book, and returning a book.

The code defines three classes: Book, User, and Library. The Book class represents a book with properties such as title, author, and year. The User class represents a user with properties such as username and userID. The Library class represents the library itself, with properties such as a vector of books and a vector of users.

The Library class has several member functions. The displayBooks function displays the current book inventory. The displayBooksForAddNewBooks function displays the book inventory for adding new books. The borrowBook function allows a user to borrow a book by entering the title of the book they want to borrow. The addNewBook function allows an admin to add a new book to the inventory by entering the title, author, and publication year of the book. The returnBook function allows a user to return a borrowed book by entering the title of the book they want to return.

The main function is the entry point of the program. It prompts the user to choose a role (user or admin) and then prompts for a username and password. If the login is successful, it initializes a Library object and populates the book inventory. It then displays a menu of options for the user to choose from, and performs the corresponding action based on the user's choice. The program continues to display the menu until the user chooses to exit.

Overall, this code provides a basic implementation of a library management system with user and admin functionality.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node