帮我使用C++编写一份完整的图书管理系统,需要具有书籍类、图书类型类、图书类和派生类,系统要能在控制台运行,并且能进行数据的增删改查和读写
时间: 2024-03-04 17:49:37 浏览: 54
一个C++编写的图书管理系统
4星 · 用户满意度95%
好的,我可以给你提供一个简单的图书管理系统的实现,包括书籍类、图书类型类、图书类和派生类,以及控制台界面和数据的增删改查和读写。
首先,我们需要定义一个图书类型类,用于描述一本书的类型,包括类型编号和类型名称。
```cpp
class BookType {
public:
int id;
string name;
BookType(int id, string name) {
this->id = id;
this->name = name;
}
};
```
接下来,我们定义一个书籍类 Book,用于描述一本书的基本信息,包括书籍编号、书名、作者、价格、出版社和类型等。
```cpp
class Book {
public:
int id;
string name;
string author;
double price;
string press;
BookType type;
Book(int id, string name, string author, double price, string press, BookType type) {
this->id = id;
this->name = name;
this->author = author;
this->price = price;
this->press = press;
this->type = type;
}
};
```
然后,我们定义一个图书管理类 BookManager,用于管理所有的书籍和图书类型,包括增加、删除、修改和查询等操作。为了方便实现,我们使用 STL 中的 vector 来存储所有的书籍和图书类型。
```cpp
class BookManager {
private:
vector<BookType> types;
vector<Book> books;
public:
void addType(BookType type);
void removeType(int id);
void updateType(int id, string name);
void listTypes();
void addBook(Book book);
void removeBook(int id);
void updateBook(int id, Book book);
void listBooks();
void saveToFile(string fileName);
void loadFromFile(string fileName);
};
```
在 BookManager 类中,我们定义了一些方法来实现各种图书和图书类型的操作。其中,addType()、removeType()、updateType()、listTypes() 方法用于操作图书类型,addBook()、removeBook()、updateBook()、listBooks() 方法用于操作书籍信息,saveToFile()、loadFromFile() 方法用于数据的读写操作。
接下来,我们来实现这些方法。
```cpp
void BookManager::addType(BookType type) {
types.push_back(type);
}
void BookManager::removeType(int id) {
for (vector<BookType>::iterator it = types.begin(); it != types.end(); it++) {
if (it->id == id) {
types.erase(it);
break;
}
}
}
void BookManager::updateType(int id, string name) {
for (vector<BookType>::iterator it = types.begin(); it != types.end(); it++) {
if (it->id == id) {
it->name = name;
break;
}
}
}
void BookManager::listTypes() {
cout << "所有图书类型:" << endl;
for (vector<BookType>::iterator it = types.begin(); it != types.end(); it++) {
cout << it->id << " " << it->name << endl;
}
}
void BookManager::addBook(Book book) {
books.push_back(book);
}
void BookManager::removeBook(int id) {
for (vector<Book>::iterator it = books.begin(); it != books.end(); it++) {
if (it->id == id) {
books.erase(it);
break;
}
}
}
void BookManager::updateBook(int id, Book book) {
for (vector<Book>::iterator it = books.begin(); it != books.end(); it++) {
if (it->id == id) {
it->name = book.name;
it->author = book.author;
it->price = book.price;
it->press = book.press;
it->type = book.type;
break;
}
}
}
void BookManager::listBooks() {
cout << "所有图书:" << endl;
for (vector<Book>::iterator it = books.begin(); it != books.end(); it++) {
cout << it->id << " " << it->name << " " << it->author << " " << it->price << " " << it->press << " " << it->type.name << endl;
}
}
void BookManager::saveToFile(string fileName) {
ofstream fout(fileName);
for (vector<BookType>::iterator it = types.begin(); it != types.end(); it++) {
fout << it->id << " " << it->name << endl;
}
for (vector<Book>::iterator it = books.begin(); it != books.end(); it++) {
fout << it->id << " " << it->name << " " << it->author << " " << it->price << " " << it->press << " " << it->type.id << endl;
}
fout.close();
}
void BookManager::loadFromFile(string fileName) {
ifstream fin(fileName);
int id, typeId;
string name, author, press;
double price;
while (fin >> id >> name) {
types.push_back(BookType(id, name));
}
while (fin >> id >> name >> author >> price >> press >> typeId) {
for (vector<BookType>::iterator it = types.begin(); it != types.end(); it++) {
if (it->id == typeId) {
books.push_back(Book(id, name, author, price, press, *it));
break;
}
}
}
fin.close();
}
```
最后,我们来实现控制台界面,让用户可以通过控制台来使用这个图书管理系统。
```cpp
int main() {
BookManager bm;
bm.loadFromFile("books.txt");
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加图书类型" << endl;
cout << "2. 删除图书类型" << endl;
cout << "3. 修改图书类型" << endl;
cout << "4. 列出所有图书类型" << endl;
cout << "5. 添加图书" << endl;
cout << "6. 删除图书" << endl;
cout << "7. 修改图书" << endl;
cout << "8. 列出所有图书" << endl;
cout << "9. 保存数据到文件" << endl;
cout << "0. 退出程序" << endl;
int choice;
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
int typeId;
string typeName;
cout << "请输入图书类型编号和名称:" << endl;
cin >> typeId >> typeName;
bm.addType(BookType(typeId, typeName));
cout << "图书类型添加成功!" << endl;
break;
case 2:
int removeTypeId;
cout << "请输入要删除的图书类型编号:" << endl;
cin >> removeTypeId;
bm.removeType(removeTypeId);
cout << "图书类型删除成功!" << endl;
break;
case 3:
int updateTypeId;
string updateTypeName;
cout << "请输入要修改的图书类型编号和名称:" << endl;
cin >> updateTypeId >> updateTypeName;
bm.updateType(updateTypeId, updateTypeName);
cout << "图书类型修改成功!" << endl;
break;
case 4:
bm.listTypes();
break;
case 5:
int bookId, bookTypeId;
string bookName, bookAuthor, bookPress;
double bookPrice;
cout << "请输入图书编号、名称、作者、价格、出版社和类型编号:" << endl;
cin >> bookId >> bookName >> bookAuthor >> bookPrice >> bookPress >> bookTypeId;
for (vector<BookType>::iterator it = bm.types.begin(); it != bm.types.end(); it++) {
if (it->id == bookTypeId) {
bm.addBook(Book(bookId, bookName, bookAuthor, bookPrice, bookPress, *it));
cout << "图书添加成功!" << endl;
break;
}
}
break;
case 6:
int removeBookId;
cout << "请输入要删除的图书编号:" << endl;
cin >> removeBookId;
bm.removeBook(removeBookId);
cout << "图书删除成功!" << endl;
break;
case 7:
int updateBookId, updateBookTypeId;
string updateBookName, updateBookAuthor, updateBookPress;
double updateBookPrice;
cout << "请输入要修改的图书编号、名称、作者、价格、出版社和类型编号:" << endl;
cin >> updateBookId >> updateBookName >> updateBookAuthor >> updateBookPrice >> updateBookPress >> updateBookTypeId;
for (vector<BookType>::iterator it = bm.types.begin(); it != bm.types.end(); it++) {
if (it->id == updateBookTypeId) {
bm.updateBook(updateBookId, Book(updateBookId, updateBookName, updateBookAuthor, updateBookPrice, updateBookPress, *it));
cout << "图书修改成功!" << endl;
break;
}
}
break;
case 8:
bm.listBooks();
break;
case 9:
bm.saveToFile("books.txt");
cout << "数据保存成功!" << endl;
break;
default:
cout << "无效的操作!" << endl;
break;
}
}
return 0;
}
```
这样,一个简单的图书管理系统就实现了,用户可以通过控制台来进行各种数据的增删改查和读写操作。
阅读全文