用c++开发一个简单的宠物信息管理系统,并给出源代码。要求实现以下基本功能:有简单的用户管理功能,不同用户在登录系统后会有不同的菜单可进行不同的操作。管理员可以实现以下功能:(1)宠物信息录入功能,主要包括录入宠物的编号,名字,性别,年龄,颜色,种类,客户姓名和手机号 宠物信息必须用文件保存起来。(2)宠物信息修改功能(修改后的信息也要用文件保存)。(3)宠物信息浏览功能。(4)宠物信息删除功能 。(5)模糊查询功能。(6)按编号大小排序功能 用户:可以创建账户,通过姓名,密码登录,进入客户端,客户可以输入自己的姓名,或者手机号查询自己的宠物信息。要求充分应用封装、抽象、继承、多态、重载、模板完成。
时间: 2024-03-20 13:40:56 浏览: 18
以下是一个简单的宠物信息管理系统的C++源代码,实现了上述基本功能。具体实现细节请参考注释。
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
// 宠物类
class Pet {
public:
string id; // 编号
string name; // 名字
string sex; // 性别
int age; // 年龄
string color; // 颜色
string breed; // 种类
string ownerName; // 客户姓名
string ownerPhone; // 客户手机号
// 构造函数
Pet() {}
Pet(string id, string name, string sex, int age, string color, string breed, string ownerName, string ownerPhone) {
this->id = id;
this->name = name;
this->sex = sex;
this->age = age;
this->color = color;
this->breed = breed;
this->ownerName = ownerName;
this->ownerPhone = ownerPhone;
}
// 输出宠物信息
void print() {
cout << "编号:" << id << endl;
cout << "名字:" << name << endl;
cout << "性别:" << sex << endl;
cout << "年龄:" << age << endl;
cout << "颜色:" << color << endl;
cout << "种类:" << breed << endl;
cout << "客户姓名:" << ownerName << endl;
cout << "客户手机号:" << ownerPhone << endl;
}
// 重载比较运算符,用于排序
bool operator<(const Pet& other) const {
return id < other.id;
}
};
// 宠物信息管理类
class PetManager {
public:
// 添加宠物信息
void addPet() {
Pet pet;
cout << "请输入宠物编号:";
cin >> pet.id;
cout << "请输入宠物名字:";
cin >> pet.name;
cout << "请输入宠物性别:";
cin >> pet.sex;
cout << "请输入宠物年龄:";
cin >> pet.age;
cout << "请输入宠物颜色:";
cin >> pet.color;
cout << "请输入宠物种类:";
cin >> pet.breed;
cout << "请输入客户姓名:";
cin >> pet.ownerName;
cout << "请输入客户手机号:";
cin >> pet.ownerPhone;
pets.push_back(pet);
savePetsToFile();
cout << "添加成功!" << endl;
}
// 修改宠物信息
void modifyPet() {
string id;
cout << "请输入要修改的宠物编号:";
cin >> id;
for (auto& pet : pets) {
if (pet.id == id) {
cout << "请输入宠物名字:";
cin >> pet.name;
cout << "请输入宠物性别:";
cin >> pet.sex;
cout << "请输入宠物年龄:";
cin >> pet.age;
cout << "请输入宠物颜色:";
cin >> pet.color;
cout << "请输入宠物种类:";
cin >> pet.breed;
cout << "请输入客户姓名:";
cin >> pet.ownerName;
cout << "请输入客户手机号:";
cin >> pet.ownerPhone;
savePetsToFile();
cout << "修改成功!" << endl;
return;
}
}
cout << "找不到编号为" << id << "的宠物!" << endl;
}
// 浏览宠物信息
void viewPets() {
for (auto& pet : pets) {
pet.print();
cout << endl;
}
}
// 删除宠物信息
void deletePet() {
string id;
cout << "请输入要删除的宠物编号:";
cin >> id;
for (auto it = pets.begin(); it != pets.end(); it++) {
if (it->id == id) {
pets.erase(it);
savePetsToFile();
cout << "删除成功!" << endl;
return;
}
}
cout << "找不到编号为" << id << "的宠物!" << endl;
}
// 模糊查询宠物信息
void searchPet() {
string keyword;
cout << "请输入查询关键字:";
cin >> keyword;
for (auto& pet : pets) {
if (pet.id.find(keyword) != string::npos ||
pet.name.find(keyword) != string::npos ||
pet.sex.find(keyword) != string::npos ||
to_string(pet.age).find(keyword) != string::npos ||
pet.color.find(keyword) != string::npos ||
pet.breed.find(keyword) != string::npos ||
pet.ownerName.find(keyword) != string::npos ||
pet.ownerPhone.find(keyword) != string::npos) {
pet.print();
cout << endl;
}
}
}
// 按编号排序宠物信息
void sortPets() {
sort(pets.begin(), pets.end());
savePetsToFile();
cout << "排序成功!" << endl;
}
// 加载宠物信息
void loadPetsFromFile() {
ifstream fin("pets.txt");
if (!fin) {
cout << "无法打开文件pets.txt" << endl;
return;
}
string id, name, sex, color, breed, ownerName, ownerPhone;
int age;
while (fin >> id >> name >> sex >> age >> color >> breed >> ownerName >> ownerPhone) {
Pet pet(id, name, sex, age, color, breed, ownerName, ownerPhone);
pets.push_back(pet);
}
fin.close();
}
private:
vector<Pet> pets; // 宠物列表
// 将宠物信息保存到文件
void savePetsToFile() {
ofstream fout("pets.txt");
if (!fout) {
cout << "无法打开文件pets.txt" << endl;
return;
}
for (auto& pet : pets) {
fout << pet.id << " " << pet.name << " " << pet.sex << " " << pet.age << " " << pet.color << " " << pet.breed << " " << pet.ownerName << " " << pet.ownerPhone << endl;
}
fout.close();
}
};
// 用户类
class User {
public:
string name; // 姓名
string password; // 密码
// 构造函数
User() {}
User(string name, string password) {
this->name = name;
this->password = password;
}
};
// 用户管理类
class UserManager {
public:
// 创建账户
void createUser() {
User user;
cout << "请输入姓名:";
cin >> user.name;
cout << "请输入密码:";
cin >> user.password;
users.push_back(user);
saveUsersToFile();
cout << "账户创建成功!" << endl;
}
// 用户登录
bool login() {
string name, password;
cout << "请输入姓名:";
cin >> name;
cout << "请输入密码:";
cin >> password;
for (auto& user : users) {
if (user.name == name && user.password == password) {
cout << "登录成功!" << endl;
return true;
}
}
cout << "用户名或密码错误!" << endl;
return false;
}
// 加载用户信息
void loadUsersFromFile() {
ifstream fin("users.txt");
if (!fin) {
cout << "无法打开文件users.txt" << endl;
return;
}
string name, password;
while (fin >> name >> password) {
User user(name, password);
users.push_back(user);
}
fin.close();
}
private:
vector<User> users; // 用户列表
// 将用户信息保存到文件
void saveUsersToFile() {
ofstream fout("users.txt");
if (!fout) {
cout << "无法打开文件users.txt" << endl;
return;
}
for (auto& user : users) {
fout << user.name << " " << user.password << endl;
}
fout.close();
}
};
int main() {
PetManager petManager;
UserManager userManager;
userManager.loadUsersFromFile();
petManager.loadPetsFromFile();
while (true) {
cout << "欢迎使用宠物信息管理系统,请登录或创建账户!" << endl;
cout << "1.创建账户" << endl;
cout << "2.登录" << endl;
cout << "3.退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
userManager.createUser();
break;
case 2:
if (userManager.login()) {
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;
int choice;
cin >> choice;
switch (choice) {
case 1:
petManager.addPet();
break;
case 2:
petManager.modifyPet();
break;
case 3:
petManager.viewPets();
break;
case 4:
petManager.deletePet();
break;
case 5:
petManager.searchPet();
break;
case 6:
petManager.sortPets();
break;
case 7:
cout << "再见!" << endl;
return 0;
default:
cout << "无效的选项!" << endl;
break;
}
}
}
break;
case 3:
cout << "再见!" << endl;
return 0;
default:
cout << "无效的选项!" << endl;
break;
}
}
return 0;
}
```