C++设计一个图书管理系统
时间: 2023-09-12 07:12:38 浏览: 145
好的,下面是一个简单的图书管理系统的设计思路:
1. 定义图书类Book,包括图书编号、书名、作者、出版社、价格等属性,以及相关的get和set方法。
2. 定义图书管理类BookManager,包括添加图书、删除图书、修改图书信息、查询图书等操作,以及相关的方法。
3. 使用vector容器存储图书,可以方便地进行增删改查操作。
4. 通过文件读写的方式将数据存储在本地,可以实现数据持久化。
5. 设计一个用户界面,可以让用户输入命令,进行相应的操作。
下面是一个简单的示例代码:
```c++
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Book {
private:
string id;
string name;
string author;
string publisher;
double price;
public:
Book(string id, string name, string author, string publisher, double price) {
this->id = id;
this->name = name;
this->author = author;
this->publisher = publisher;
this->price = price;
}
string getId() {
return id;
}
string getName() {
return name;
}
string getAuthor() {
return author;
}
string getPublisher() {
return publisher;
}
double getPrice() {
return price;
}
void setName(string name) {
this->name = name;
}
void setAuthor(string author) {
this->author = author;
}
void setPublisher(string publisher) {
this->publisher = publisher;
}
void setPrice(double price) {
this->price = price;
}
};
class BookManager {
private:
vector<Book> books;
public:
void addBook(Book book) {
books.push_back(book);
}
void removeBook(string id) {
for (vector<Book>::iterator it = books.begin(); it != books.end(); ++it) {
if (it->getId() == id) {
books.erase(it);
break;
}
}
}
void updateBook(string id, string name, string author, string publisher, double price) {
for (vector<Book>::iterator it = books.begin(); it != books.end(); ++it) {
if (it->getId() == id) {
it->setName(name);
it->setAuthor(author);
it->setPublisher(publisher);
it->setPrice(price);
break;
}
}
}
Book* findBook(string id) {
for (vector<Book>::iterator it = books.begin(); it != books.end(); ++it) {
if (it->getId() == id) {
return &(*it);
}
}
return NULL;
}
void loadBooks() {
ifstream fin("books.txt");
string id, name, author, publisher;
double price;
while (fin >> id >> name >> author >> publisher >> price) {
Book book(id, name, author, publisher, price);
addBook(book);
}
fin.close();
}
void saveBooks() {
ofstream fout("books.txt");
for (vector<Book>::iterator it = books.begin(); it != books.end(); ++it) {
fout << it->getId() << " " << it->getName() << " " << it->getAuthor() << " " << it->getPublisher() << " " << it->getPrice() << endl;
}
fout.close();
}
};
int main() {
BookManager manager;
manager.loadBooks();
string cmd;
while (true) {
cout << "请输入命令(add/remove/update/find/save/quit):" << endl;
cin >> cmd;
if (cmd == "add") {
string id, name, author, publisher;
double price;
cout << "请输入图书编号、书名、作者、出版社、价格:" << endl;
cin >> id >> name >> author >> publisher >> price;
Book book(id, name, author, publisher, price);
manager.addBook(book);
} else if (cmd == "remove") {
string id;
cout << "请输入要删除的图书编号:" << endl;
cin >> id;
manager.removeBook(id);
} else if (cmd == "update") {
string id, name, author, publisher;
double price;
cout << "请输入要修改的图书编号、书名、作者、出版社、价格:" << endl;
cin >> id >> name >> author >> publisher >> price;
manager.updateBook(id, name, author, publisher, price);
} else if (cmd == "find") {
string id;
cout << "请输入要查找的图书编号:" << endl;
cin >> id;
Book* book = manager.findBook(id);
if (book != NULL) {
cout << "图书编号:" << book->getId() << endl;
cout << "书名:" << book->getName() << endl;
cout << "作者:" << book->getAuthor() << endl;
cout << "出版社:" << book->getPublisher() << endl;
cout << "价格:" << book->getPrice() << endl;
} else {
cout << "未找到该图书!" << endl;
}
} else if (cmd == "save") {
manager.saveBooks();
} else if (cmd == "quit") {
break;
} else {
cout << "无效命令!" << endl;
}
}
return 0;
}
```
在上述代码中,我们定义了Book类和BookManager类,分别表示图书和图书管理,其中Book类封装了图书的属性和相关的get和set方法,BookManager类封装了添加、删除、修改、查询图书等操作,并使用vector容器存储图书。同时,我们通过文件读写的方式将数据存储在本地,实现了数据持久化。最后,我们设计了一个用户界面,让用户可以输入命令,进行相应的操作。
阅读全文