用c++写一段关于仓库管理系统的代码,要求:仓库中货物信息包括:货物编号、货物名称、货物单价、货物数量等。 功能要求: 1) 增加和删除货物信息 2) 按编号或名称查询货物信息 3) 显示库存量不足一定值(由用户输入)的货物信息 4) 显示所有货物的信息 5) 对所有货物信息读写文
时间: 2024-03-26 14:38:07 浏览: 153
好的,以下是一份 C++ 代码,实现了基本的仓库管理系统功能,可以满足您的需求。注意,该代码只是一个简单的示例,并未进行完整的测试和优化,请谨慎使用。
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 声明货物类
class Goods {
public:
Goods(string id, string name, double price, int quantity) {
m_id = id;
m_name = name;
m_price = price;
m_quantity = quantity;
}
string get_id() {
return m_id;
}
string get_name() {
return m_name;
}
double get_price() {
return m_price;
}
int get_quantity() {
return m_quantity;
}
void set_id(string id) {
m_id = id;
}
void set_name(string name) {
m_name = name;
}
void set_price(double price) {
m_price = price;
}
void set_quantity(int quantity) {
m_quantity = quantity;
}
private:
string m_id;
string m_name;
double m_price;
int m_quantity;
};
// 声明仓库类
class Warehouse {
public:
Warehouse() {
// 从文件中读取货物信息
ifstream infile("goods.txt");
string id, name;
double price;
int quantity;
while (infile >> id >> name >> price >> quantity) {
Goods goods(id, name, price, quantity);
m_goods.push_back(goods);
}
infile.close();
}
// 添加货物
void add_goods(Goods goods) {
m_goods.push_back(goods);
}
// 删除货物
void delete_goods(string id) {
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
if (it->get_id() == id) {
m_goods.erase(it);
break;
}
}
}
// 按编号查询货物
Goods* search_goods_by_id(string id) {
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
if (it->get_id() == id) {
return &(*it);
}
}
return NULL;
}
// 按名称查询货物
vector<Goods*> search_goods_by_name(string name) {
vector<Goods*> result;
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
if (it->get_name() == name) {
result.push_back(&(*it));
}
}
return result;
}
// 显示库存量不足一定值的货物信息
vector<Goods*> show_low_quantity_goods(int quantity) {
vector<Goods*> result;
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
if (it->get_quantity() < quantity) {
result.push_back(&(*it));
}
}
return result;
}
// 显示所有货物信息
void show_all_goods() {
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
cout << it->get_id() << " " << it->get_name() << " " << it->get_price() << " " << it->get_quantity() << endl;
}
}
// 保存货物信息到文件
void save_to_file() {
ofstream outfile("goods.txt");
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
outfile << it->get_id() << " " << it->get_name() << " " << it->get_price() << " " << it->get_quantity() << endl;
}
outfile.close();
}
private:
vector<Goods> m_goods;
};
// 主函数
int main() {
Warehouse warehouse;
int choice;
while (true) {
cout << "1.添加货物 2.删除货物 3.按编号查询货物 4.按名称查询货物 5.显示库存量不足的货物 6.显示所有货物 7.保存货物信息到文件 8.退出" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1: {
string id, name;
double price;
int quantity;
cout << "请输入货物编号:";
cin >> id;
cout << "请输入货物名称:";
cin >> name;
cout << "请输入货物单价:";
cin >> price;
cout << "请输入货物数量:";
cin >> quantity;
Goods goods(id, name, price, quantity);
warehouse.add_goods(goods);
cout << "货物添加成功!" << endl;
break;
}
case 2: {
string id;
cout << "请输入货物编号:";
cin >> id;
warehouse.delete_goods(id);
cout << "货物删除成功!" << endl;
break;
}
case 3: {
string id;
cout << "请输入货物编号:";
cin >> id;
Goods* goods = warehouse.search_goods_by_id(id);
if (goods == NULL) {
cout << "找不到指定编号的货物!" << endl;
}
else {
cout << goods->get_id() << " " << goods->get_name() << " " << goods->get_price() << " " << goods->get_quantity() << endl;
}
break;
}
case 4: {
string name;
cout << "请输入货物名称:";
cin >> name;
vector<Goods*> result = warehouse.search_goods_by_name(name);
if (result.size() == 0) {
cout << "找不到指定名称的货物!" << endl;
}
else {
for (vector<Goods*>::iterator it = result.begin(); it != result.end(); it++) {
cout << (*it)->get_id() << " " << (*it)->get_name() << " " << (*it)->get_price() << " " << (*it)->get_quantity() << endl;
}
}
break;
}
case 5: {
int quantity;
cout << "请输入库存量不足的货物数量:";
cin >> quantity;
vector<Goods*> result = warehouse.show_low_quantity_goods(quantity);
if (result.size() == 0) {
cout << "没有库存量不足的货物!" << endl;
}
else {
for (vector<Goods*>::iterator it = result.begin(); it != result.end(); it++) {
cout << (*it)->get_id() << " " << (*it)->get_name() << " " << (*it)->get_price() << " " << (*it)->get_quantity() << endl;
}
}
break;
}
case 6: {
warehouse.show_all_goods();
break;
}
case 7: {
warehouse.save_to_file();
cout << "货物信息保存成功!" << endl;
break;
}
case 8: {
return 0;
}
default: {
cout << "无效的操作!" << endl;
break;
}
}
}
return 0;
}
```
该代码实现了仓库管理系统的基本功能,包括添加货物、删除货物、按编号或名称查询货物、显示库存量不足一定值的货物信息、显示所有货物信息、对所有货物信息进行读写等操作。
阅读全文