设计一个书店管理系统,能完成书店的日常管理工作。 要求完成的基本功能: 1、进货入库记录。 2、销售出货记录。 3、图书信息查询:可通过书名、作者等途径查询某本图书的详细信息(含书名、作者、出版社、页数、最新入库时间、库存量、价格等)。 4、自动预警提示(当某图书的库存量为1时自动预警提示) 5、数据存储:以文件方式存储出入库记录 可自行补充其它功能。 说明与要求: 1、对象集合的管理可以使用STL中的相关容器类; 用c++语言给出代码
时间: 2023-07-04 15:05:16 浏览: 215
以下是一个简单的书店管理系统的代码,实现了基本功能:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <ctime>
using namespace std;
// 图书信息
struct Book {
string name; // 书名
string author; // 作者
string publisher; // 出版社
int pages; // 页数
time_t lastInTime; // 最新入库时间
int stock; // 库存量
double price; // 价格
};
// 进货记录
struct InRecord {
string bookName; // 书名
int count; // 数量
time_t time; // 时间
};
// 出货记录
struct OutRecord {
string bookName; // 书名
int count; // 数量
time_t time; // 时间
};
// 书店管理系统
class Bookstore {
public:
// 构造函数
Bookstore();
// 进货入库
void In(string name, int count, double price);
// 销售出货
void Out(string name, int count);
// 查询图书信息
void Query(string name);
// 自动预警
void Warn();
// 显示所有图书信息
void ShowAll();
// 保存数据到文件
void SaveToFile();
// 从文件中读取数据
void LoadFromFile();
private:
vector<Book> books; // 图书集合
vector<InRecord> inRecords; // 进货记录集合
vector<OutRecord> outRecords; // 出货记录集合
};
// 构造函数
Bookstore::Bookstore() {
LoadFromFile();
}
// 进货入库
void Bookstore::In(string name, int count, double price) {
bool found = false;
for (int i = 0; i < books.size(); i++) {
if (books[i].name == name) {
books[i].stock += count;
books[i].price = price;
books[i].lastInTime = time(NULL);
found = true;
break;
}
}
if (!found) {
Book book = {name, "", "", 0, time(NULL), count, price};
books.push_back(book);
}
InRecord inRecord = {name, count, time(NULL)};
inRecords.push_back(inRecord);
SaveToFile();
}
// 销售出货
void Bookstore::Out(string name, int count) {
bool found = false;
for (int i = 0; i < books.size(); i++) {
if (books[i].name == name) {
if (books[i].stock < count) {
cout << "库存不足!" << endl;
return;
}
books[i].stock -= count;
found = true;
break;
}
}
if (!found) {
cout << "图书不存在!" << endl;
return;
}
OutRecord outRecord = {name, count, time(NULL)};
outRecords.push_back(outRecord);
SaveToFile();
}
// 查询图书信息
void Bookstore::Query(string name) {
bool found = false;
for (int i = 0; i < books.size(); i++) {
if (books[i].name == name) {
cout << "书名:" << books[i].name << endl;
cout << "作者:" << books[i].author << endl;
cout << "出版社:" << books[i].publisher << endl;
cout << "页数:" << books[i].pages << endl;
cout << "最新入库时间:" << asctime(localtime(&(books[i].lastInTime)));
cout << "库存量:" << books[i].stock << endl;
cout << "价格:" << books[i].price << endl;
found = true;
break;
}
}
if (!found) {
cout << "图书不存在!" << endl;
}
}
// 自动预警
void Bookstore::Warn() {
for (int i = 0; i < books.size(); i++) {
if (books[i].stock == 1) {
cout << "图书《" << books[i].name << "》库存不足,请及时进货!" << endl;
}
}
}
// 显示所有图书信息
void Bookstore::ShowAll() {
for (int i = 0; i < books.size(); i++) {
Query(books[i].name);
}
}
// 保存数据到文件
void Bookstore::SaveToFile() {
ofstream ofs("bookstore.dat");
ofs << books.size() << endl;
for (int i = 0; i < books.size(); i++) {
ofs << books[i].name << endl;
ofs << books[i].author << endl;
ofs << books[i].publisher << endl;
ofs << books[i].pages << endl;
ofs << books[i].lastInTime << endl;
ofs << books[i].stock << endl;
ofs << books[i].price << endl;
}
ofs << inRecords.size() << endl;
for (int i = 0; i < inRecords.size(); i++) {
ofs << inRecords[i].bookName << endl;
ofs << inRecords[i].count << endl;
ofs << inRecords[i].time << endl;
}
ofs << outRecords.size() << endl;
for (int i = 0; i < outRecords.size(); i++) {
ofs << outRecords[i].bookName << endl;
ofs << outRecords[i].count << endl;
ofs << outRecords[i].time << endl;
}
ofs.close();
}
// 从文件中读取数据
void Bookstore::LoadFromFile() {
ifstream ifs("bookstore.dat");
if (!ifs) {
return;
}
int bookCount;
ifs >> bookCount;
books.resize(bookCount);
for (int i = 0; i < bookCount; i++) {
ifs >> books[i].name;
ifs >> books[i].author;
ifs >> books[i].publisher;
ifs >> books[i].pages;
ifs >> books[i].lastInTime;
ifs >> books[i].stock;
ifs >> books[i].price;
}
int inRecordCount;
ifs >> inRecordCount;
inRecords.resize(inRecordCount);
for (int i = 0; i < inRecordCount; i++) {
ifs >> inRecords[i].bookName;
ifs >> inRecords[i].count;
ifs >> inRecords[i].time;
}
int outRecordCount;
ifs >> outRecordCount;
outRecords.resize(outRecordCount);
for (int i = 0; i < outRecordCount; i++) {
ifs >> outRecords[i].bookName;
ifs >> outRecords[i].count;
ifs >> outRecords[i].time;
}
ifs.close();
}
int main() {
Bookstore bookstore;
while (true) {
cout << "1. 进货入库" << endl;
cout << "2. 销售出货" << endl;
cout << "3. 查询图书信息" << endl;
cout << "4. 自动预警" << endl;
cout << "5. 显示所有图书信息" << endl;
cout << "0. 退出" << endl;
int choice;
cin >> choice;
if (choice == 1) {
string name;
int count;
double price;
cout << "请输入书名:";
cin >> name;
cout << "请输入数量:";
cin >> count;
cout << "请输入价格:";
cin >> price;
bookstore.In(name, count, price);
} else if (choice == 2) {
string name;
int count;
cout << "请输入书名:";
cin >> name;
cout << "请输入数量:";
cin >> count;
bookstore.Out(name, count);
} else if (choice == 3) {
string name;
cout << "请输入书名:";
cin >> name;
bookstore.Query(name);
} else if (choice == 4) {
bookstore.Warn();
} else if (choice == 5) {
bookstore.ShowAll();
} else if (choice == 0) {
break;
}
}
return 0;
}
```
这个系统使用了 STL 中的 vector 容器类来管理图书、进货记录和出货记录。数据存储使用文件方式,每次操作后都会将数据保存到文件中,下次启动时会从文件中读取数据。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)