用C++写一个商品管理系统
时间: 2023-07-28 19:10:38 浏览: 41
好的,下面我会为您提供一个简单的商品管理系统的C++代码实现,供您参考:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Commodity {
public:
string name;
double price;
int stock;
Commodity(string n, double p, int s) {
name = n;
price = p;
stock = s;
}
};
class CommodityManager {
public:
vector<Commodity> commodityList;
void addCommodity(Commodity c) {
commodityList.push_back(c);
}
void removeCommodity(int index) {
commodityList.erase(commodityList.begin() + index);
}
void displayCommodityList() {
cout << "商品列表:" << endl;
cout << "编号\t名称\t价格\t库存" << endl;
for (int i = 0; i < commodityList.size(); i++) {
cout << i << "\t" << commodityList[i].name << "\t" << commodityList[i].price << "\t" << commodityList[i].stock << endl;
}
cout << endl;
}
void updateCommodity(int index, Commodity c) {
commodityList[index] = c;
}
};
int main() {
CommodityManager cm;
int choice;
while (true) {
cout << "请选择要进行的操作:" << endl;
cout << "1. 添加商品" << endl;
cout << "2. 删除商品" << endl;
cout << "3. 显示商品列表" << endl;
cout << "4. 修改商品信息" << endl;
cout << "5. 退出" << endl;
cin >> choice;
switch (choice) {
case 1: {
string name;
double price;
int stock;
cout << "请输入商品名称:";
cin >> name;
cout << "请输入商品价格:";
cin >> price;
cout << "请输入商品库存:";
cin >> stock;
Commodity c(name, price, stock);
cm.addCommodity(c);
cout << "添加成功!" << endl;
break;
}
case 2: {
int index;
cm.displayCommodityList();
cout << "请输入要删除的商品编号:";
cin >> index;
cm.removeCommodity(index);
cout << "删除成功!" << endl;
break;
}
case 3: {
cm.displayCommodityList();
break;
}
case 4: {
int index;
string name;
double price;
int stock;
cm.displayCommodityList();
cout << "请输入要修改的商品编号:";
cin >> index;
cout << "请输入新的商品名称:";
cin >> name;
cout << "请输入新的商品价格:";
cin >> price;
cout << "请输入新的商品库存:";
cin >> stock;
Commodity c(name, price, stock);
cm.updateCommodity(index, c);
cout << "修改成功!" << endl;
break;
}
case 5: {
return 0;
}
default: {
cout << "输入有误,请重新输入!" << endl;
break;
}
}
}
return 0;
}
```
这个商品管理系统包括添加商品、删除商品、显示商品列表、修改商品信息等功能。您可以根据自己的需求进行修改和完善。