使用c++,运用class和vector的知识,写出一个书店管理系统的代码,要有菜单,数据用Excel文件保存,要求完成的基本功能: 1、进货入库记录。 2、销售出货记录。 3、图书信息查询:可通过书名、作者等途径查询某本图书的详细信息(含书名、作者、出版社、页数、最新入库时间、库存量、价格等)。 4、自动预警提示(当某图书的库存量为1时自动预警提示) 5、数据存储:以文件方式存储出入库记录说明与要求: 1、对象集合的管理可以使用STL中的相关容器类;
时间: 2023-07-04 16:12:57 浏览: 168
好的,以下是一个简单的书店管理系统的代码示例。其中,我们使用了class和vector来管理书店的图书信息和库存记录,使用Excel文件来保存数据。
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// 定义一个Book类,用于存储书籍信息
class Book {
public:
string name; // 书名
string author; // 作者
string publisher; // 出版社
int pages; // 页数
int stock; // 库存量
float price; // 价格
string lastUpdate; // 最新入库时间
// 构造函数,用于初始化Book对象
Book(string name, string author, string publisher, int pages, int stock, float price, string lastUpdate) {
this->name = name;
this->author = author;
this->publisher = publisher;
this->pages = pages;
this->stock = stock;
this->price = price;
this->lastUpdate = lastUpdate;
}
};
// 定义一个Bookstore类,用于管理书店的图书信息和库存记录
class Bookstore {
private:
vector<Book> books; // 存储图书信息的向量
public:
// 从Excel文件中读取图书信息,并存储到books向量中
void loadBooks(string filename) {
ifstream file(filename);
string line;
while (getline(file, line)) {
string name, author, publisher, lastUpdate;
int pages, stock;
float price;
stringstream ss(line);
getline(ss, name, ',');
getline(ss, author, ',');
getline(ss, publisher, ',');
ss >> pages;
ss.ignore();
ss >> stock;
ss.ignore();
ss >> price;
ss.ignore();
getline(ss, lastUpdate, ',');
books.push_back(Book(name, author, publisher, pages, stock, price, lastUpdate));
}
file.close();
}
// 将books向量中的图书信息保存到Excel文件中
void saveBooks(string filename) {
ofstream file(filename);
for (auto book : books) {
file << book.name << "," << book.author << "," << book.publisher << "," << book.pages << ","
<< book.stock << "," << book.price << "," << book.lastUpdate << endl;
}
file.close();
}
// 添加一本新书
void addBook(Book book) {
books.push_back(book);
}
// 删除一本图书
void removeBook(string name) {
books.erase(remove_if(books.begin(), books.end(), [name](Book book) { return book.name == name; }), books.end());
}
// 更新一本图书的信息
void updateBook(string name, Book book) {
removeBook(name);
addBook(book);
}
// 查询一本图书的详细信息
void searchBook(string name) {
auto it = find_if(books.begin(), books.end(), [name](Book book) { return book.name == name; });
if (it != books.end()) {
cout << "书名:" << it->name << endl;
cout << "作者:" << it->author << endl;
cout << "出版社:" << it->publisher << endl;
cout << "页数:" << it->pages << endl;
cout << "库存量:" << it->stock << endl;
cout << "价格:" << it->price << endl;
cout << "最新入库时间:" << it->lastUpdate << endl;
} else {
cout << "未找到该书!" << endl;
}
}
// 进货入库
void stockIn(string name, int count) {
auto it = find_if(books.begin(), books.end(), [name](Book book) { return book.name == name; });
if (it != books.end()) {
it->stock += count;
cout << "成功入库 " << count << " 本《" << name << "》!" << endl;
// 更新最新入库时间
time_t t = time(nullptr);
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&t));
it->lastUpdate = buffer;
// 检查库存量是否低于预警值
if (it->stock <= 1) {
cout << "警告:《" << name << "》的库存量已低于预警值!" << endl;
}
} else {
cout << "未找到该书!" << endl;
}
}
// 销售出货
void sellOut(string name, int count) {
auto it = find_if(books.begin(), books.end(), [name](Book book) { return book.name == name; });
if (it != books.end()) {
if (it->stock < count) {
cout << "库存不足,无法出货!" << endl;
} else {
it->stock -= count;
cout << "成功出货 " << count << " 本《" << name << "》!" << endl;
// 更新最新出货时间
time_t t = time(nullptr);
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&t));
it->lastUpdate = buffer;
// 检查库存量是否低于预警值
if (it->stock <= 1) {
cout << "警告:《" << name << "》的库存量已低于预警值!" << endl;
}
}
} else {
cout << "未找到该书!" << endl;
}
}
// 显示菜单
void showMenu() {
cout << "欢迎使用书店管理系统!" << endl;
cout << "1. 进货入库" << endl;
cout << "2. 销售出货" << endl;
cout << "3. 图书信息查询" << endl;
cout << "4. 显示所有图书" << endl;
cout << "5. 退出系统" << endl;
}
// 运行管理系统
void run() {
int choice;
do {
showMenu();
cout << "请输入您的选择:";
cin >> choice;
switch (choice) {
case 1: {
string name;
int count;
cout << "请输入书名:";
cin >> name;
cout << "请输入入库数量:";
cin >> count;
stockIn(name, count);
break;
}
case 2: {
string name;
int count;
cout << "请输入书名:";
cin >> name;
cout << "请输入出货数量:";
cin >> count;
sellOut(name, count);
break;
}
case 3: {
string name;
cout << "请输入书名:";
cin >> name;
searchBook(name);
break;
}
case 4: {
for (auto book : books) {
cout << "书名:" << book.name << endl;
cout << "作者:" << book.author << endl;
cout << "出版社:" << book.publisher << endl;
cout << "页数:" << book.pages << endl;
cout << "库存量:" << book.stock << endl;
cout << "价格:" << book.price << endl;
cout << "最新入/出库时间:" << book.lastUpdate << endl;
cout << endl;
}
break;
}
case 5: {
cout << "感谢使用书店管理系统!" << endl;
break;
}
default: {
cout << "无效的选择,请重新输入!" << endl;
break;
}
}
cout << endl;
} while (choice != 5);
}
};
int main() {
Bookstore store;
store.loadBooks("books.csv"); // 从Excel文件中读取图书信息
store.run(); // 运行管理系统
store.saveBooks("books.csv"); // 将图书信息保存到Excel文件中
return 0;
}
```
以上代码仅供参考,实际实现中可能需要根据具体需求进行修改和完善。
阅读全文