超市会员管理系统C++课程设计源代码
时间: 2023-07-11 08:46:45 浏览: 120
很抱歉,我无法提供完整的超市会员管理系统C++课程设计源代码,因为这是一个需要根据具体需求和功能实现的项目。但是,以下是一个简单的参考代码,供您参考。
```cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
class Member {
private:
std::string m_number; //会员编号
std::string m_name; //会员姓名
std::string m_phone; //会员手机号
int m_points; //会员积分
public:
Member(std::string number = "", std::string name = "", std::string phone = "", int points = 0)
: m_number(number), m_name(name), m_phone(phone), m_points(points) {}
void setNumber(std::string number) { m_number = number; }
std::string getNumber() const { return m_number; }
void setName(std::string name) { m_name = name; }
std::string getName() const { return m_name; }
void setPhone(std::string phone) { m_phone = phone; }
std::string getPhone() const { return m_phone; }
void setPoints(int points) { m_points = points; }
int getPoints() const { return m_points; }
std::string toString() const {
std::stringstream ss;
ss << m_number << "," << m_name << "," << m_phone << "," << m_points;
return ss.str();
}
};
class MemberManager {
private:
std::vector<Member> m_members; //会员列表
public:
void addMember(Member member) { m_members.push_back(member); }
Member* findMember(std::string number) {
for (int i = 0; i < m_members.size(); i++) {
if (m_members[i].getNumber() == number) {
return &m_members[i];
}
}
return nullptr;
}
void updateMember(Member member) {
Member* pMember = findMember(member.getNumber());
if (pMember != nullptr) {
*pMember = member;
}
}
void deleteMember(std::string number) {
for (auto iter = m_members.begin(); iter != m_members.end(); iter++) {
if (iter->getNumber() == number) {
m_members.erase(iter);
return;
}
}
}
int getMemberCount() const { return m_members.size(); }
Member* getMember(int index) { return &m_members[index]; }
};
void saveMembers(MemberManager& manager, std::string filename) {
std::ofstream outFile(filename);
for (int i = 0; i < manager.getMemberCount(); i++) {
Member* pMember = manager.getMember(i);
outFile << pMember->toString() << std::endl;
}
outFile.close();
}
void loadMembers(MemberManager& manager, std::string filename) {
std::ifstream inFile(filename);
std::string line;
while (std::getline(inFile, line)) {
std::istringstream iss(line);
std::string number, name, phone;
int points;
std::getline(std::getline(std::getline(iss, number, ','), name, ','), phone, ',') >> points;
manager.addMember(Member(number, name, phone, points));
}
inFile.close();
}
void displayMenu() {
std::cout << "======================" << std::endl;
std::cout << "超市会员管理系统" << std::endl;
std::cout << "1. 添加会员" << std::endl;
std::cout << "2. 查询会员" << std::endl;
std::cout << "3. 修改会员" << std::endl;
std::cout << "4. 删除会员" << std::endl;
std::cout << "5. 显示所有会员" << std::endl;
std::cout << "6. 保存会员信息" << std::endl;
std::cout << "7. 加载会员信息" << std::endl;
std::cout << "0. 退出系统" << std::endl;
std::cout << "======================" << std::endl;
std::cout << "请选择操作:";
}
void addMember(MemberManager& manager) {
std::string number, name, phone;
int points;
std::cout << "请输入会员编号:";
std::getline(std::cin, number);
std::cout << "请输入会员姓名:";
std::getline(std::cin, name);
std::cout << "请输入会员手机号:";
std::getline(std::cin, phone);
std::cout << "请输入会员积分:";
std::cin >> points;
std::cin.ignore();
manager.addMember(Member(number, name, phone, points));
std::cout << "会员添加成功!" << std::endl;
}
void findMember(MemberManager& manager) {
std::string number;
std::cout << "请输入会员编号:";
std::getline(std::cin, number);
Member* pMember = manager.findMember(number);
if (pMember != nullptr) {
std::cout << "会员信息如下:" << std::endl;
std::cout << "编号:" << pMember->getNumber() << std::endl;
std::cout << "姓名:" << pMember->getName() << std::endl;
std::cout << "手机号:" << pMember->getPhone() << std::endl;
std::cout << "积分:" << pMember->getPoints() << std::endl;
}
else {
std::cout << "没有找到该会员!" << std::endl;
}
}
void updateMember(MemberManager& manager) {
std::string number, name, phone;
int points;
std::cout << "请输入会员编号:";
std::getline(std::cin, number);
Member* pMember = manager.findMember(number);
if (pMember != nullptr) {
std::cout << "请输入新的会员姓名(留空表示不修改):";
std::getline(std::cin, name);
if (!name.empty()) {
pMember->setName(name);
}
std::cout << "请输入新的会员手机号(留空表示不修改):";
std::getline(std::cin, phone);
if (!phone.empty()) {
pMember->setPhone(phone);
}
std::cout << "请输入新的会员积分(留空表示不修改):";
std::cin >> points;
std::cin.ignore();
if (points != 0) {
pMember->setPoints(points);
}
std::cout << "会员信息修改成功!" << std::endl;
}
else {
std::cout << "没有找到该会员!" << std::endl;
}
}
void deleteMember(MemberManager& manager) {
std::string number;
std::cout << "请输入会员编号:";
std::getline(std::cin, number);
manager.deleteMember(number);
std::cout << "会员删除成功!" << std::endl;
}
void displayMembers(MemberManager& manager) {
std::cout << "会员列表如下:" << std::endl;
for (int i = 0; i < manager.getMemberCount(); i++) {
Member* pMember = manager.getMember(i);
std::cout << "编号:" << pMember->getNumber() << ",";
std::cout << "姓名:" << pMember->getName() << ",";
std::cout << "手机号:" << pMember->getPhone() << ",";
std::cout << "积分:" << pMember->getPoints() << std::endl;
}
}
void saveMembersToFile(MemberManager& manager) {
std::string filename;
std::cout << "请输入文件名:";
std::getline(std::cin, filename);
saveMembers(manager, filename);
std::cout << "会员信息保存成功!" << std::endl;
}
void loadMembersFromFile(MemberManager& manager) {
std::string filename;
std::cout << "请输入文件名:";
std::getline(std::cin, filename);
loadMembers(manager, filename);
std::cout << "会员信息加载成功!" << std::endl;
}
int main() {
MemberManager manager;
int choice = -1;
while (choice != 0) {
displayMenu();
std::cin >> choice;
std::cin.ignore();
switch (choice) {
case 1:
addMember(manager);
break;
case 2:
findMember(manager);
break;
case 3:
updateMember(manager);
break;
case 4:
deleteMember(manager);
break;
case 5:
displayMembers(manager);
break;
case 6:
saveMembersToFile(manager);
break;
case 7:
loadMembersFromFile(manager);
break;
case 0:
std::cout << "感谢使用超市会员管理系统!" << std::endl;
break;
default:
std::cout << "无效的选择,请重新选择!" << std::endl;
break;
}
}
return 0;
}
```
这个程序实现了一个简单的超市会员管理系统,包括添加、查询、修改、删除、显示所有会员、保存会员信息和加载会员信息等功能。您可以根据具体需求和功能进行修改和完善。
阅读全文