写一个C++程序代码,用类和对象的方法,也要用文件流的方式,且代码能够在运行界面实现对数据的增删查改。代码要有相应的中文注释。题目是:超市会员管理系统 系统功能的基本要求:会员的基本信息,包括姓名、性别、年龄、工作单位、联系方式。会员购物信息(这个也要有):购买物品编号、物品名称、所属种类,数量,价格。会员返利信息:包括会员积分的情况,享受优惠的等级
时间: 2023-06-13 19:08:43 浏览: 82
很高兴为您提供一个简单的C++程序代码,实现超市会员管理系统的基本功能。代码中使用了类和对象的方法,以及文件流的方式,可以在运行界面实现对数据的增删查改。代码如下:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// 定义会员类
class Member {
public:
string name; // 姓名
string gender; // 性别
int age; // 年龄
string company; // 工作单位
string contact; // 联系方式
int points; // 积分
int level; // 会员等级
// 默认构造函数
Member() {
name = "";
gender = "";
age = 0;
company = "";
contact = "";
points = 0;
level = 0;
}
// 自定义构造函数
Member(string n, string g, int a, string c, string con, int p, int l) {
name = n;
gender = g;
age = a;
company = c;
contact = con;
points = p;
level = l;
}
// 重载输出运算符
friend ostream& operator<<(ostream& os, const Member& m) {
os << "姓名:" << m.name << endl;
os << "性别:" << m.gender << endl;
os << "年龄:" << m.age << endl;
os << "工作单位:" << m.company << endl;
os << "联系方式:" << m.contact << endl;
os << "积分:" << m.points << endl;
os << "等级:" << m.level << endl;
return os;
}
};
// 定义商品类
class Product {
public:
int id; // 商品编号
string name; // 商品名称
string category; // 商品类别
int quantity; // 商品数量
double price; // 商品价格
// 默认构造函数
Product() {
id = 0;
name = "";
category = "";
quantity = 0;
price = 0.0;
}
// 自定义构造函数
Product(int i, string n, string c, int q, double p) {
id = i;
name = n;
category = c;
quantity = q;
price = p;
}
// 重载输出运算符
friend ostream& operator<<(ostream& os, const Product& p) {
os << "编号:" << p.id << endl;
os << "名称:" << p.name << endl;
os << "类别:" << p.category << endl;
os << "数量:" << p.quantity << endl;
os << "价格:" << p.price << endl;
return os;
}
};
// 定义超市会员管理系统类
class Supermarket {
private:
vector<Member> members; // 会员列表
vector<Product> products; // 商品列表
public:
// 读取会员信息和商品信息
void readData() {
// 读取会员信息
ifstream memberFile("members.txt");
if (memberFile.is_open()) {
string n, g, c, con;
int a, p, l;
while (memberFile >> n >> g >> a >> c >> con >> p >> l) {
Member m(n, g, a, c, con, p, l);
members.push_back(m);
}
memberFile.close();
}
// 读取商品信息
ifstream productFile("products.txt");
if (productFile.is_open()) {
int i, q;
string n, c;
double p;
while (productFile >> i >> n >> c >> q >> p) {
Product pro(i, n, c, q, p);
products.push_back(pro);
}
productFile.close();
}
}
// 保存会员信息和商品信息
void saveData() {
// 保存会员信息
ofstream memberFile("members.txt");
if (memberFile.is_open()) {
for (int i = 0; i < members.size(); i++) {
memberFile << members[i].name << " " << members[i].gender << " " << members[i].age << " "
<< members[i].company << " " << members[i].contact << " " << members[i].points << " "
<< members[i].level << endl;
}
memberFile.close();
}
// 保存商品信息
ofstream productFile("products.txt");
if (productFile.is_open()) {
for (int i = 0; i < products.size(); i++) {
productFile << products[i].id << " " << products[i].name << " " << products[i].category << " "
<< products[i].quantity << " " << products[i].price << endl;
}
productFile.close();
}
}
// 显示会员列表
void showMemberList() {
cout << "会员列表:" << endl;
for (int i = 0; i < members.size(); i++) {
cout << "会员" << i + 1 << ":" << endl;
cout << members[i] << endl;
}
}
// 显示商品列表
void showProductList() {
cout << "商品列表:" << endl;
for (int i = 0; i < products.size(); i++) {
cout << "商品" << i + 1 << ":" << endl;
cout << products[i] << endl;
}
}
// 增加会员信息
void addMember() {
string n, g, c, con;
int a, p, l;
cout << "请输入会员姓名:";
cin >> n;
cout << "请输入会员性别:";
cin >> g;
cout << "请输入会员年龄:";
cin >> a;
cout << "请输入会员工作单位:";
cin >> c;
cout << "请输入会员联系方式:";
cin >> con;
cout << "请输入会员积分:";
cin >> p;
cout << "请输入会员等级:";
cin >> l;
Member m(n, g, a, c, con, p, l);
members.push_back(m);
cout << "会员信息添加成功!" << endl;
}
// 增加商品信息
void addProduct() {
int i, q;
string n, c;
double p;
cout << "请输入商品编号:";
cin >> i;
cout << "请输入商品名称:";
cin >> n;
cout << "请输入商品类别:";
cin >> c;
cout << "请输入商品数量:";
cin >> q;
cout << "请输入商品价格:";
cin >> p;
Product pro(i, n, c, q, p);
products.push_back(pro);
cout << "商品信息添加成功!" << endl;
}
// 删除会员信息
void deleteMember() {
int index;
cout << "请输入要删除的会员编号(从1开始):";
cin >> index;
if (index <= members.size()) {
members.erase(members.begin() + index - 1);
cout << "会员信息删除成功!" << endl;
} else {
cout << "输入的会员编号无效,请重新输入!" << endl;
}
}
// 删除商品信息
void deleteProduct() {
int index;
cout << "请输入要删除的商品编号(从1开始):";
cin >> index;
if (index <= products.size()) {
products.erase(products.begin() + index - 1);
cout << "商品信息删除成功!" << endl;
} else {
cout << "输入的商品编号无效,请重新输入!" << endl;
}
}
// 修改会员信息
void modifyMember() {
int index;
cout << "请输入要修改的会员编号(从1开始):";
cin >> index;
if (index <= members.size()) {
string n, g, c, con;
int a, p, l;
cout << "请输入会员姓名:";
cin >> n;
cout << "请输入会员性别:";
cin >> g;
cout << "请输入会员年龄:";
cin >> a;
cout << "请输入会员工作单位:";
cin >> c;
cout << "请输入会员联系方式:";
cin >> con;
cout << "请输入会员积分:";
cin >> p;
cout << "请输入会员等级:";
cin >> l;
Member m(n, g, a, c, con, p, l);
members[index - 1] = m;
cout << "会员信息修改成功!" << endl;
} else {
cout << "输入的会员编号无效,请重新输入!" << endl;
}
}
// 修改商品信息
void modifyProduct() {
int index;
cout << "请输入要修改的商品编号(从1开始):";
cin >> index;
if (index <= products.size()) {
int i, q;
string n, c;
double p;
cout << "请输入商品编号:";
cin >> i;
cout << "请输入商品名称:";
cin >> n;
cout << "请输入商品类别:";
cin >> c;
cout << "请输入商品数量:";
cin >> q;
cout << "请输入商品价格:";
cin >> p;
Product pro(i, n, c, q, p);
products[index - 1] = pro;
cout << "商品信息修改成功!" << endl;
} else {
cout << "输入的商品编号无效,请重新输入!" << endl;
}
}
// 进行会员购物
void shopping(Member& m) {
int choice;
cout << "请选择要购买的商品编号(输入0结束选择):" << endl;
showProductList();
cin >> choice;
while (choice != 0) {
bool found = false;
for (int i = 0; i < products.size(); i++) {
if (products[i].id == choice) {
found = true;
int quantity;
cout << "请输入购买数量:";
cin >> quantity;
if (quantity <= products[i].quantity) {
products[i].quantity -= quantity;
double cost = quantity * products[i].price;
m.points += int(cost / 10);
if (m.points >= 500) {
m.level = 3;
} else if (m.points >= 200) {
m.level = 2;
} else {
m.level = 1;
}
cout << "已购买:" << products[i].name << ",数量:" << quantity << ",总价:" << cost << endl;
break;
} else {
cout << "商品数量不足,请重新选择!" << endl;
}
}
}
if (!found) {
cout << "输入的商品编号无效,请重新选择!" << endl;
}
cout << "请选择要购买的商品编号(输入0结束选择):" << endl;
showProductList();
cin >> choice;
}
}
};
int main() {
Supermarket s; // 创建超市会员管理系统对象
s.readData(); // 读取会员信息和商品信息
int choice;
while (true) {
cout << "欢迎使用超市会员管理系统!" << endl;
cout << "1. 显示会员列表" << endl;
cout << "2. 显示商品列表" << endl;
cout << "3. 增加会员信息" << endl;
cout << "4. 增加商品信息" << endl;
cout << "5. 删除会员信息" << endl;
cout << "6. 删除商品信息" << endl;
cout << "7. 修改会员信息" << endl;
cout << "8. 修改商品信息" << endl;
cout << "9. 会员购物" << endl;
cout << "0. 退出系统" << endl;
cout << "请输入功能编号:";
cin >> choice;
switch (choice) {
case 1:
s.showMemberList();
break;
case 2:
s.showProductList();
break;
case 3:
s.addMember();
s.saveData();
break;
case 4:
s.addProduct();
s.saveData();
break;
case 5:
s.deleteMember();
s.saveData();
break;
case 6:
s.deleteProduct();
s.saveData();
break;
case 7:
s.modifyMember();
s.saveData();
break;
case 8:
s.modifyProduct();
s.saveData();
break;
case 9:
int index;
cout << "请输入要购物的会员编号(从1开始):";
cin >> index;
if (index <= s.members.size()) {
s.shopping(s.members[index - 1]);
s.saveData();
} else {
cout << "输入的会员编号无效,请重新输入!" << endl;
}
break;
case 0:
s.saveData();
cout << "感谢使用超市会员管理系统!" << endl;
return 0;
default:
cout << "输入的功能编号无效,请重新输入!" << endl;
break;
}
}
return 0;
}
```
此代码实现了超市会员管理系统的基本功能,包括读取会员信息和商品信息、保存会员信息和商品信息、显示会员列表、显示商品列表、增加会员信息、增加商品信息、删除会员信息、删除商品信息、修改会员信息、修改商品信息、会员购物等操作。
阅读全文