编写一个售卖系统程序: (1)售卖系统中包括3项数据:商品名称、商品数量和商品价格 (2)对商品的录入、查询、删除、售卖、修改5项功能;(3)设计主控菜单,模块化设计,通过main函数调用;5(4)界面美观;用include代码实现
时间: 2024-03-28 17:38:50 浏览: 12
好的,我可以帮你编写一个售卖系统程序。以下是代码实现:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 商品类
class Product {
private:
string name; // 商品名称
int quantity; // 商品数量
double price; // 商品价格
public:
// 构造函数
Product(string n = "", int q = 0, double p = 0.0) {
name = n;
quantity = q;
price = p;
}
// 获取商品名称
string getName() {
return name;
}
// 获取商品数量
int getQuantity() {
return quantity;
}
// 获取商品价格
double getPrice() {
return price;
}
// 设置商品名称
void setName(string n) {
name = n;
}
// 设置商品数量
void setQuantity(int q) {
quantity = q;
}
// 设置商品价格
void setPrice(double p) {
price = p;
}
// 输出商品信息
void print() {
cout << "商品名称:" << name << endl;
cout << "商品数量:" << quantity << endl;
cout << "商品价格:" << price << endl;
}
};
// 售卖系统类
class SalesSystem {
private:
vector<Product> products; // 商品列表
public:
// 添加商品
void addProduct() {
string name;
int quantity;
double price;
cout << "请输入商品名称:";
cin >> name;
cout << "请输入商品数量:";
cin >> quantity;
cout << "请输入商品价格:";
cin >> price;
Product product(name, quantity, price);
products.push_back(product);
cout << "商品添加成功!" << endl;
}
// 查询商品
void queryProduct() {
string name;
cout << "请输入要查询的商品名称:";
cin >> name;
bool found = false;
for (int i = 0; i < products.size(); i++) {
if (products[i].getName() == name) {
products[i].print();
found = true;
break;
}
}
if (!found) {
cout << "没有找到该商品!" << endl;
}
}
// 删除商品
void deleteProduct() {
string name;
cout << "请输入要删除的商品名称:";
cin >> name;
bool found = false;
for (int i = 0; i < products.size(); i++) {
if (products[i].getName() == name) {
products.erase(products.begin() + i);
found = true;
cout << "商品删除成功!" << endl;
break;
}
}
if (!found) {
cout << "没有找到该商品!" << endl;
}
}
// 售卖商品
void sellProduct() {
string name;
int quantity;
cout << "请输入要售卖的商品名称:";
cin >> name;
cout << "请输入要售卖的商品数量:";
cin >> quantity;
bool found = false;
for (int i = 0; i < products.size(); i++) {
if (products[i].getName() == name) {
if (products[i].getQuantity() >= quantity) {
products[i].setQuantity(products[i].getQuantity() - quantity);
cout << "商品售卖成功!" << endl;
found = true;
break;
} else {
cout << "商品数量不足!" << endl;
found = true;
break;
}
}
}
if (!found) {
cout << "没有找到该商品!" << endl;
}
}
// 修改商品
void modifyProduct() {
string name;
cout << "请输入要修改的商品名称:";
cin >> name;
bool found = false;
for (int i = 0; i < products.size(); i++) {
if (products[i].getName() == name) {
string newName;
int newQuantity;
double newPrice;
cout << "请输入新的商品名称:";
cin >> newName;
cout << "请输入新的商品数量:";
cin >> newQuantity;
cout << "请输入新的商品价格:";
cin >> newPrice;
products[i].setName(newName);
products[i].setQuantity(newQuantity);
products[i].setPrice(newPrice);
cout << "商品修改成功!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "没有找到该商品!" << endl;
}
}
// 显示菜单
void showMenu() {
cout << "欢迎使用售卖系统!" << endl;
cout << "1. 添加商品" << endl;
cout << "2. 查询商品" << endl;
cout << "3. 删除商品" << endl;
cout << "4. 售卖商品" << endl;
cout << "5. 修改商品" << endl;
cout << "0. 退出系统" << endl;
}
// 运行系统
void run() {
int choice;
do {
showMenu();
cout << "请输入您的选择:";
cin >> choice;
switch (choice) {
case 1:
addProduct();
break;
case 2:
queryProduct();
break;
case 3:
deleteProduct();
break;
case 4:
sellProduct();
break;
case 5:
modifyProduct();
break;
case 0:
cout << "谢谢使用,再见!" << endl;
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
} while (choice != 0);
}
};
int main() {
SalesSystem salesSystem;
salesSystem.run();
return 0;
}
```
以上就是一个简单的售卖系统程序,包括商品类和售卖系统类,实现了添加商品、查询商品、删除商品、售卖商品、修改商品等功能,并且通过主控菜单进行模块化设计,界面简洁美观。