请设计一个书店管理系统 能完成书店的日程管理工作,用c++并生成代码 包括:书店,图书,时间三个类。 叔带你 要求完成的基本功能: 1.进货入库功能,先判断书店里有无该图书,没有则输入该图书的相信信息进行入库,有则只需要在原有的基础上增添数量即可。 2.销售出货记录。 3、图书信息查询:可通过书名、作者等途径查询某本图书的详细信息(含书名、作者、出版社、页数、最新入库时间、库存量、价格等)。 4、自动预警提示(当某图书的库存量为1时自动预警提示) 5、数据存储:以文件方式存储出入库记录 对象集合的管理必须使用STL的相关容器类。
时间: 2024-02-13 09:59:59 浏览: 20
以下是C++书店管理系统的代码实现:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
class Book {
public:
string name; // 书名
string author; // 作者
string publisher; // 出版社
int pages; // 页数
int stock; // 库存量
double price; // 价格
time_t last_in; // 最新入库时间
Book(string name, string author, string publisher, int pages, int stock, double price) {
this->name = name;
this->author = author;
this->publisher = publisher;
this->pages = pages;
this->stock = stock;
this->price = price;
time(&this->last_in); // 记录最新入库时间
}
void print_info() {
cout << "书名:" << name << endl;
cout << "作者:" << author << endl;
cout << "出版社:" << publisher << endl;
cout << "页数:" << pages << endl;
cout << "库存量:" << stock << endl;
cout << "价格:" << price << endl;
cout << "最新入库时间:" << ctime(&last_in) << endl;
}
};
class BookStore {
private:
vector<Book> books; // 图书集合
public:
void import_book() {
string name, author, publisher;
int pages, stock;
double price;
cout << "请输入书名:";
cin >> name;
// 判断是否已存在该图书,如果存在则只增加库存量
bool found = false;
for (auto& book : books) {
if (book.name == name) {
cout << "请输入增加的库存量:";
cin >> stock;
book.stock += stock;
found = true;
break;
}
}
if (!found) {
cout << "请输入作者:";
cin >> author;
cout << "请输入出版社:";
cin >> publisher;
cout << "请输入页数:";
cin >> pages;
cout << "请输入库存量:";
cin >> stock;
cout << "请输入价格:";
cin >> price;
books.emplace_back(name, author, publisher, pages, stock, price);
}
}
void sell_book() {
string name;
int amount;
cout << "请输入书名:";
cin >> name;
// 判断是否有足够的库存
bool found = false;
for (auto& book : books) {
if (book.name == name) {
cout << "请输入售出数量:";
cin >> amount;
if (amount <= book.stock) {
book.stock -= amount;
found = true;
break;
} else {
cout << "库存不足!" << endl;
}
}
}
if (!found) {
cout << "未找到该图书!" << endl;
}
}
void query_book() {
string name, author;
bool found = false;
cout << "请输入书名或作者:";
cin >> name;
for (auto& book : books) {
if (book.name == name || book.author == name) {
book.print_info();
found = true;
}
}
if (!found) {
cout << "未找到该图书!" << endl;
}
}
void warn_book() {
for (auto& book : books) {
if (book.stock == 1) {
cout << "图书《" << book.name << "》库存量为1,请及时补货!" << endl;
}
}
}
void save_data() {
ofstream ofs("bookstore.dat");
for (auto& book : books) {
ofs << book.name << " " << book.author << " " << book.publisher << " "
<< book.pages << " " << book.stock << " " << book.price << " " << book.last_in << endl;
}
ofs.close();
}
void load_data() {
ifstream ifs("bookstore.dat");
string name, author, publisher;
int pages, stock;
double price;
time_t last_in;
while (ifs >> name >> author >> publisher >> pages >> stock >> price >> last_in) {
books.emplace_back(name, author, publisher, pages, stock, price);
books.back().last_in = last_in;
}
ifs.close();
}
};
class Time {
public:
int year, month, day;
Time() {
time_t now = time(nullptr);
struct tm* t = localtime(&now);
year = t->tm_year + 1900;
month = t->tm_mon + 1;
day = t->tm_mday;
}
void print_date() {
cout << year << "-" << month << "-" << day << endl;
}
};
int main() {
BookStore store;
Time t;
int choice;
store.load_data();
do {
cout << "1.进货入库\n2.销售出货\n3.图书查询\n4.自动预警\n5.退出" << endl;
cout << "请输入操作编号:";
cin >> choice;
switch (choice) {
case 1:
store.import_book();
break;
case 2:
store.sell_book();
break;
case 3:
store.query_book();
break;
case 4:
cout << "今天是";
t.print_date();
store.warn_book();
break;
case 5:
store.save_data();
cout << "谢谢使用!" << endl;
break;
default:
cout << "无效的操作!" << endl;
break;
}
} while (choice != 5);
return 0;
}
```
在该实现中,Book 表示图书类,包括书名、作者、出版社、页数、库存量、价格和最新入库时间等属性,以及打印图书信息的方法。BookStore 表示书店类,包括图书集合,以及进货入库、销售出货、图书查询和自动预警等方法。Time 表示时间类,包括年、月、日等属性,以及打印日期的方法。在 main 函数中,使用一个循环读取用户的操作选择,并调用相应的方法。同时,读写数据采用文件方式存储,便于数据的持久化。