写一个图书管理系统
时间: 2023-11-30 18:11:25 浏览: 20
好的,我可以为您提供一个简单的图书管理系统的代码:
```
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Book {
private:
string title;
string author;
int yearPublished;
bool isAvailable;
public:
Book(string title, string author, int yearPublished, bool isAvailable) {
this->title = title;
this->author = author;
this->yearPublished = yearPublished;
this->isAvailable = isAvailable;
}
string getTitle() {
return title;
}
string getAuthor() {
return author;
}
int getYearPublished() {
return yearPublished;
}
bool getIsAvailable() {
return isAvailable;
}
void setIsAvailable(bool isAvailable) {
this->isAvailable = isAvailable;
}
};
class Library {
private:
vector<Book> books;
public:
void addBook(Book book) {
books.push_back(book);
}
void removeBook(int index) {
books.erase(books.begin() + index);
}
void displayBooks() {
for (int i = 0; i < books.size(); i++) {
cout << i+1 << ". " << books[i].getTitle() << " by " << books[i].getAuthor() << ", published in " << books[i].getYearPublished() << endl;
}
}
void searchBook(string title) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == title) {
cout << "Book found! " << books[i].getTitle() << " by " << books[i].getAuthor() << ", published in " << books[i].getYearPublished() << endl;
return;
}
}
cout << "Book not found." << endl;
}
void borrowBook(int index) {
if (books[index].getIsAvailable()) {
books[index].setIsAvailable(false);
cout << "Book borrowed successfully." << endl;
} else {
cout << "Book is not available for borrowing." << endl;
}
}
void returnBook(int index) {
if (!books[index].getIsAvailable()) {
books[index].setIsAvailable(true);
cout << "Book returned successfully." << endl;
} else {
cout << "Book was not borrowed." << endl;
}
}
};
int main() {
Library library;
// Add some books to the library
library.addBook(Book("The Lord of the Rings", "J.R.R. Tolkien", 1954, true));
library.addBook(Book("To Kill a Mockingbird", "Harper Lee", 1960, true));
library.addBook(Book("The Great Gatsby", "F. Scott Fitzgerald", 1925, true));
int choice;
bool exit = false;
while (!exit) {
cout << "Welcome to the library. What would you like to do?" << endl;
cout << "1. Display all books" << endl;
cout << "2. Search for a book" << endl;
cout << "3. Borrow a book" << endl;
cout << "4. Return a book" << endl;
cout << "5. Exit" << endl;
cin >> choice;
switch (choice) {
case 1:
library.displayBooks();
break;
case 2:
string title;
cout << "Enter the title of the book you are searching for: ";
cin.ignore();
getline(cin, title);
library.searchBook(title);
break;
case 3:
int index;
cout << "Enter the number of the book you would like to borrow: ";
cin >> index;
index--;
library.borrowBook(index);
break;
case 4:
cout << "Enter the number of the book you would like to return: ";
cin >> index;
index--;
library.returnBook(index);
break;
case 5:
exit = true;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
```
这个图书管理系统包括以下功能:
- 添加图书
- 显示所有图书
- 搜索图书
- 借阅图书
- 归还图书
- 退出系统
您可以根据需要进行修改和扩展。