C++图书馆管理系统
作者:野牛程序员:2024-10-30 10:12:31 C++阅读 2641
C++图书馆管理系统
C++图书馆管理系统
以下是一个更完整的 C++ 图书馆管理系统示例,包括用户管理、书籍分类、借阅和归还功能。该系统使用了基本的面向对象编程方法,可以作为进一步开发的基础。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class Book {
public:
std::string title;
std::string author;
std::string category;
bool isBorrowed;
Book(std::string t, std::string a, std::string c)
: title(t), author(a), category(c), isBorrowed(false) {}
};
class User {
public:
std::string name;
std::string userId;
User(std::string n, std::string id) : name(n), userId(id) {}
};
class Library {
private:
std::vector<Book> books;
std::vector<User> users;
public:
void addBook(const std::string& title, const std::string& author, const std::string& category) {
books.emplace_back(title, author, category);
std::cout << "Book added: " << title << " by " << author << " in category " << category << std::endl;
}
void viewBooks() const {
if (books.empty()) {
std::cout << "No books in the library." << std::endl;
return;
}
std::cout << "Books in the library:" << std::endl;
for (const auto& book : books) {
std::cout << "Title: " << book.title
<< ", Author: " << book.author
<< ", Category: " << book.category
<< ", Status: " << (book.isBorrowed ? "Borrowed" : "Available")
<< std::endl;
}
}
void deleteBook(const std::string& title) {
auto it = std::remove_if(books.begin(), books.end(), [&title](const Book& book) {
return book.title == title;
});
if (it != books.end()) {
books.erase(it, books.end());
std::cout << "Book deleted: " << title << std::endl;
} else {
std::cout << "Book not found: " << title << std::endl;
}
}
void borrowBook(const std::string& title, const std::string& userId) {
for (auto& book : books) {
if (book.title == title && !book.isBorrowed) {
book.isBorrowed = true;
std::cout << userId << " borrowed: " << title << std::endl;
return;
}
}
std::cout << "Book not available or does not exist." << std::endl;
}
void returnBook(const std::string& title) {
for (auto& book : books) {
if (book.title == title && book.isBorrowed) {
book.isBorrowed = false;
std::cout << title << " has been returned." << std::endl;
return;
}
}
std::cout << "Book not found or was not borrowed." << std::endl;
}
void addUser(const std::string& name, const std::string& userId) {
users.emplace_back(name, userId);
std::cout << "User added: " << name << " with ID " << userId << std::endl;
}
};
int main() {
Library library;
int choice;
std::string title, author, category, userId;
while (true) {
std::cout << "\nLibrary Management System" << std::endl;
std::cout << "1. Add Book" << std::endl;
std::cout << "2. View Books" << std::endl;
std::cout << "3. Delete Book" << std::endl;
std::cout << "4. Borrow Book" << std::endl;
std::cout << "5. Return Book" << std::endl;
std::cout << "6. Add User" << std::endl;
std::cout << "7. Exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Enter book title: ";
std::cin.ignore();
std::getline(std::cin, title);
std::cout << "Enter author name: ";
std::getline(std::cin, author);
std::cout << "Enter category: ";
std::getline(std::cin, category);
library.addBook(title, author, category);
break;
case 2:
library.viewBooks();
break;
case 3:
std::cout << "Enter book title to delete: ";
std::cin.ignore();
std::getline(std::cin, title);
library.deleteBook(title);
break;
case 4:
std::cout << "Enter book title to borrow: ";
std::cin.ignore();
std::getline(std::cin, title);
std::cout << "Enter user ID: ";
std::getline(std::cin, userId);
library.borrowBook(title, userId);
break;
case 5:
std::cout << "Enter book title to return: ";
std::cin.ignore();
std::getline(std::cin, title);
library.returnBook(title);
break;
case 6:
std::cout << "Enter user name: ";
std::cin.ignore();
std::getline(std::cin, title);
std::cout << "Enter user ID: ";
std::getline(std::cin, userId);
library.addUser(title, userId);
break;
case 7:
std::cout << "Exiting..." << std::endl;
return 0;
default:
std::cout << "Invalid choice, please try again." << std::endl;
}
}
return 0;
}功能说明:
添加书籍:输入书名、作者和分类。
查看书籍:显示当前所有书籍及其状态。
删除书籍:输入书名进行删除。
借阅书籍:输入书名和用户 ID。
归还书籍:输入书名进行归还。
添加用户:输入用户姓名和 ID。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:c++输入3个数将其中的最小数输出。
- 下一篇:什么是路径压缩?
