用c++开发一个简单的宠物信息管理系统,并给出源代码。要求实现以下基本功能:有简单的用户管理功能,不同用户在登录系统后会有不同的菜单可进行不同的操作。管理员可以实现以下功能:(1)宠物信息录入功能,主要包括录入宠物的编号,名字,性别,年龄,颜色,种类,客户姓名和手机号 宠物信息必须用文件保存起来。(2)宠物信息修改功能(修改后的信息也要用文件保存)。(3)宠物信息浏览功能。(4)宠物信息删除功能 。(5)模糊查询功能。(6)按编号大小排序功能 用户:可以通过姓名,密码登录,进入客户端,客户可以输入自己的姓名,或者手机号查询自己的宠物信息。要求充分应用封装、抽象、继承、多态、重载、模板完成。
时间: 2024-03-19 21:45:55 浏览: 38
宠物店小型管理系统C++代码.doc
5星 · 资源好评率100%
好的,以下是一个用C++开发的简单宠物信息管理系统的源代码:
```
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 宠物类
class Pet {
public:
int id; // 编号
string name; // 名字
string gender; // 性别
int age; // 年龄
string color; // 颜色
string type; // 种类
string ownerName; // 客户姓名
string ownerPhone; // 客户手机号
// 构造函数
Pet(int id, string name, string gender, int age, string color, string type, string ownerName, string ownerPhone) {
this->id = id;
this->name = name;
this->gender = gender;
this->age = age;
this->color = color;
this->type = type;
this->ownerName = ownerName;
this->ownerPhone = ownerPhone;
}
};
// 用户类
class User {
public:
string name; // 姓名
string password; // 密码
// 构造函数
User(string name, string password) {
this->name = name;
this->password = password;
}
};
// 管理员类,继承自用户类
class Admin : public User {
public:
// 构造函数,调用基类构造函数
Admin(string name, string password) : User(name, password) {}
// 宠物信息录入函数
void addPet(vector<Pet>& pets) {
int id, age;
string name, gender, color, type, ownerName, ownerPhone;
cout << "请输入宠物编号:";
cin >> id;
cout << "请输入宠物名字:";
cin >> name;
cout << "请输入宠物性别:";
cin >> gender;
cout << "请输入宠物年龄:";
cin >> age;
cout << "请输入宠物颜色:";
cin >> color;
cout << "请输入宠物种类:";
cin >> type;
cout << "请输入客户姓名:";
cin >> ownerName;
cout << "请输入客户手机号:";
cin >> ownerPhone;
pets.push_back(Pet(id, name, gender, age, color, type, ownerName, ownerPhone));
savePets(pets); // 保存宠物信息到文件
cout << "宠物信息录入成功!" << endl;
}
// 宠物信息修改函数
void modifyPet(vector<Pet>& pets) {
int id;
cout << "请输入要修改的宠物编号:";
cin >> id;
// 查找宠物
auto it = find_if(pets.begin(), pets.end(), [id](const Pet& pet) {
return pet.id == id;
});
if (it == pets.end()) {
cout << "找不到该宠物!" << endl;
return;
}
// 修改宠物信息
cout << "请输入宠物名字:";
cin >> it->name;
cout << "请输入宠物性别:";
cin >> it->gender;
cout << "请输入宠物年龄:";
cin >> it->age;
cout << "请输入宠物颜色:";
cin >> it->color;
cout << "请输入宠物种类:";
cin >> it->type;
cout << "请输入客户姓名:";
cin >> it->ownerName;
cout << "请输入客户手机号:";
cin >> it->ownerPhone;
savePets(pets); // 保存宠物信息到文件
cout << "宠物信息修改成功!" << endl;
}
// 宠物信息浏览函数
void viewPets(vector<Pet>& pets) {
if (pets.empty()) {
cout << "暂无宠物信息!" << endl;
return;
}
cout << "编号\t名字\t性别\t年龄\t颜色\t种类\t客户姓名\t客户手机号" << endl;
for (const auto& pet : pets) {
cout << pet.id << "\t" << pet.name << "\t" << pet.gender << "\t"
<< pet.age << "\t" << pet.color << "\t" << pet.type << "\t"
<< pet.ownerName << "\t" << pet.ownerPhone << endl;
}
}
// 宠物信息删除函数
void deletePet(vector<Pet>& pets) {
int id;
cout << "请输入要删除的宠物编号:";
cin >> id;
// 查找宠物
auto it = find_if(pets.begin(), pets.end(), [id](const Pet& pet) {
return pet.id == id;
});
if (it == pets.end()) {
cout << "找不到该宠物!" << endl;
return;
}
pets.erase(it); // 删除宠物
savePets(pets); // 保存宠物信息到文件
cout << "宠物信息删除成功!" << endl;
}
// 模糊查询函数
void searchPets(vector<Pet>& pets) {
string keyword;
cout << "请输入关键字:";
cin >> keyword;
auto it = find_if(pets.begin(), pets.end(), [keyword](const Pet& pet) {
return pet.name.find(keyword) != string::npos || pet.ownerName.find(keyword) != string::npos;
});
if (it == pets.end()) {
cout << "找不到符合条件的宠物!" << endl;
return;
}
cout << "编号\t名字\t性别\t年龄\t颜色\t种类\t客户姓名\t客户手机号" << endl;
for (; it != pets.end(); ++it) {
if (it->name.find(keyword) != string::npos || it->ownerName.find(keyword) != string::npos) {
cout << it->id << "\t" << it->name << "\t" << it->gender << "\t"
<< it->age << "\t" << it->color << "\t" << it->type << "\t"
<< it->ownerName << "\t" << it->ownerPhone << endl;
}
}
}
// 按编号大小排序函数
void sortPets(vector<Pet>& pets) {
sort(pets.begin(), pets.end(), [](const Pet& a, const Pet& b) {
return a.id < b.id;
});
cout << "编号\t名字\t性别\t年龄\t颜色\t种类\t客户姓名\t客户手机号" << endl;
for (const auto& pet : pets) {
cout << pet.id << "\t" << pet.name << "\t" << pet.gender << "\t"
<< pet.age << "\t" << pet.color << "\t" << pet.type << "\t"
<< pet.ownerName << "\t" << pet.ownerPhone << endl;
}
}
private:
// 从文件中读取宠物信息
void loadPets(vector<Pet>& pets) {
ifstream infile("pets.txt");
if (!infile.is_open()) {
cout << "打开文件失败!" << endl;
return;
}
int id, age;
string name, gender, color, type, ownerName, ownerPhone;
while (infile >> id >> name >> gender >> age >> color >> type >> ownerName >> ownerPhone) {
pets.push_back(Pet(id, name, gender, age, color, type, ownerName, ownerPhone));
}
infile.close();
}
// 将宠物信息保存到文件
void savePets(const vector<Pet>& pets) {
ofstream outfile("pets.txt");
if (!outfile.is_open()) {
cout << "打开文件失败!" << endl;
return;
}
for (const auto& pet : pets) {
outfile << pet.id << " " << pet.name << " " << pet.gender << " "
<< pet.age << " " << pet.color << " " << pet.type << " "
<< pet.ownerName << " " << pet.ownerPhone << endl;
}
outfile.close();
}
};
// 客户类,继承自用户类
class Customer : public User {
public:
// 构造函数,调用基类构造函数
Customer(string name, string password) : User(name, password) {}
// 查询宠物信息函数
void viewMyPets(const vector<Pet>& pets) {
string keyword;
cout << "请输入您的姓名或手机号:";
cin >> keyword;
auto it = find_if(pets.begin(), pets.end(), [this, keyword](const Pet& pet) {
return pet.ownerName == name && pet.ownerPhone == keyword;
});
if (it == pets.end()) {
cout << "找不到您的宠物信息!" << endl;
return;
}
cout << "编号\t名字\t性别\t年龄\t颜色\t种类" << endl;
for (; it != pets.end(); ++it) {
if (it->ownerName == name && it->ownerPhone == keyword) {
cout << it->id << "\t" << it->name << "\t" << it->gender << "\t"
<< it->age << "\t" << it->color << "\t" << it->type << endl;
}
}
}
};
int main() {
vector<Pet> pets;
Admin admin("admin", "admin"); // 管理员账号
Customer customer("zhangsan", "123456"); // 客户账号
int choice;
// 从文件中加载宠物信息
admin.loadPets(pets);
// 用户登录
string name, password;
cout << "请输入用户名:";
cin >> name;
cout << "请输入密码:";
cin >> password;
if (name == admin.name && password == admin.password) { // 管理员登录
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 << "请选择操作:";
cin >> choice;
// 执行操作
switch (choice) {
case 1:
admin.addPet(pets);
break;
case 2:
admin.modifyPet(pets);
break;
case 3:
admin.viewPets(pets);
break;
case 4:
admin.deletePet(pets);
break;
case 5:
admin.searchPets(pets);
break;
case 6:
admin.sortPets(pets);
break;
case 7:
return 0;
default:
cout << "无效操作!" << endl;
}
}
}
else if (name == customer.name && password == customer.password) { // 客户登录
while (true) {
// 显示菜单
cout << "宠物信息查询菜单:" << endl;
cout << "1. 查看我的宠物信息" << endl;
cout << "2. 退出" << endl;
cout << "请选择操作:";
cin >> choice;
// 执行操作
switch (choice) {
case 1:
customer.viewMyPets(pets);
break;
case 2:
return 0;
default:
cout << "无效操作!" << endl;
}
}
}
else {
cout << "用户名或密码错误!" << endl;
}
return 0;
}
```
阅读全文