用C++开发一个简单的宠物信息管理系统.要求实现以下基本功能:有简单的用户管理功能,不同用户在登录系统后会有不同的菜单可进行不同的操作。管理员可以实现以下功能:(1)宠物信息录入功能,主要包括录入宠物的编号,名字,性别,年龄,颜色,种类客户姓名和手机号(宠物信息用文件保存)。(2)宠物信息修改功能(修改后的信息也要用文件保存)。(3)宠物信息浏览功能。(4)宠物信息删除功能用户:客户可以输入自己的姓名,或者手机号查询自己的宠物信息。整个设计要求采用面向对象程序设计方法进行系统设计,充分应用封装、抽象、继承、多态、重载、模板等概念完成。写出源代码
时间: 2024-03-06 20:52:06 浏览: 182
好的,下面是一个简单的基于面向对象程序设计的宠物信息管理系统的 C++ 源代码实现:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Pet {
public:
string id;
string name;
string sex;
int age;
string color;
string type;
string customer_name;
string customer_phone;
};
class User {
public:
string name;
string phone;
vector<Pet> pets;
};
class Menu {
public:
static void show_admin_menu() {
cout << "1. 宠物信息录入" << endl;
cout << "2. 宠物信息修改" << endl;
cout << "3. 宠物信息浏览" << endl;
cout << "4. 宠物信息删除" << endl;
cout << "0. 退出" << endl;
}
static void show_user_menu() {
cout << "1. 查询自己的宠物信息" << endl;
cout << "0. 退出" << endl;
}
};
class PetStore {
public:
static bool login(string username, string password) {
if (username == "admin" && password == "admin") {
return true;
}
return false;
}
static void register_user(string name, string phone) {
User user;
user.name = name;
user.phone = phone;
users.push_back(user);
cout << "注册成功!" << endl;
}
static void add_pet() {
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.type;
cout << "请输入客户姓名:";
cin >> pet.customer_name;
cout << "请输入客户手机号:";
cin >> pet.customer_phone;
pets.push_back(pet);
save_to_file();
cout << "添加成功!" << endl;
}
static void modify_pet() {
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.type;
cout << "请输入客户姓名:";
cin >> pet.customer_name;
cout << "请输入客户手机号:";
cin >> pet.customer_phone;
save_to_file();
cout << "修改成功!" << endl;
return;
}
}
cout << "没有找到该宠物编号对应的宠物!" << endl;
}
static void view_pets() {
for (auto& pet : pets) {
cout << "编号:" << pet.id << endl;
cout << "名字:" << pet.name << endl;
cout << "性别:" << pet.sex << endl;
cout << "年龄:" << pet.age << endl;
cout << "颜色:" << pet.color << endl;
cout << "种类:" << pet.type << endl;
cout << "客户姓名:" << pet.customer_name << endl;
cout << "客户手机号:" << pet.customer_phone << endl;
cout << endl;
}
}
static void delete_pet() {
string id;
cout << "请输入要删除的宠物编号:";
cin >> id;
for (auto it = pets.begin(); it != pets.end(); it++) {
if (it->id == id) {
pets.erase(it);
save_to_file();
cout << "删除成功!" << endl;
return;
}
}
cout << "没有找到该宠物编号对应的宠物!" << endl;
}
static void search_pets(string name, string phone) {
for (auto& user : users) {
if (user.name == name || user.phone == phone) {
cout << "客户姓名:" << user.name << endl;
cout << "客户手机号:" << user.phone << endl;
cout << "宠物信息:" << endl;
for (auto& pet : user.pets) {
cout << "编号:" << pet.id << endl;
cout << "名字:" << pet.name << endl;
cout << "性别:" << pet.sex << endl;
cout << "年龄:" << pet.age << endl;
cout << "颜色:" << pet.color << endl;
cout << "种类:" << pet.type << endl;
cout << endl;
}
return;
}
}
cout << "没有找到该客户对应的宠物信息!" << endl;
}
static void load_from_file() {
ifstream ifs("pets.txt");
if (ifs.is_open()) {
while (!ifs.eof()) {
Pet pet;
ifs >> pet.id >> pet.name >> pet.sex >> pet.age >> pet.color >> pet.type >> pet.customer_name >> pet.customer_phone;
if (pet.id != "") {
pets.push_back(pet);
}
}
ifs.close();
}
}
static void save_to_file() {
ofstream ofs("pets.txt");
if (ofs.is_open()) {
for (auto& pet : pets) {
ofs << pet.id << " " << pet.name << " " << pet.sex << " " << pet.age << " " << pet.color << " " << pet.type << " " << pet.customer_name << " " << pet.customer_phone << endl;
}
ofs.close();
}
}
private:
static vector<User> users;
static vector<Pet> pets;
};
vector<User> PetStore::users;
vector<Pet> PetStore::pets;
int main() {
PetStore::load_from_file();
while (true) {
int choice;
cout << "请选择您的身份(1. 管理员,2. 客户,0. 退出):";
cin >> choice;
if (choice == 1) {
string username, password;
cout << "请输入管理员账号和密码:" << endl;
cout << "账号:";
cin >> username;
cout << "密码:";
cin >> password;
if (PetStore::login(username, password)) {
while (true) {
Menu::show_admin_menu();
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
PetStore::add_pet();
break;
case 2:
PetStore::modify_pet();
break;
case 3:
PetStore::view_pets();
break;
case 4:
PetStore::delete_pet();
break;
case 0:
return 0;
default:
cout << "无效的选择!" << endl;
}
}
}
else {
cout << "登录失败!" << endl;
}
}
else if (choice == 2) {
string name, phone;
cout << "请输入您的姓名和手机号码:" << endl;
cout << "姓名:";
cin >> name;
cout << "手机号码:";
cin >> phone;
PetStore::search_pets(name, phone);
while (true) {
Menu::show_user_menu();
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
PetStore::search_pets(name, phone);
break;
case 0:
return 0;
default:
cout << "无效的选择!" << endl;
}
}
}
else if (choice == 0) {
return 0;
}
else {
cout << "无效的选择!" << endl;
}
}
return 0;
}
```
注:上面的代码实现使用了文件读写操作,需要与源代码文件同级目录下存在名为 `pets.txt` 的文本文件才能正确读写宠物信息。
阅读全文