用C++开发一个简单的宠物信息管理系统.要求实现以下基本功能:有简单的用户管理功能,不同用户在登录系统后会有不同的菜单可进行不同的操作。管理员可以实现以下功能:(1)宠物信息录入功能,主要包括录入宠物的编号,名字,性别,年龄,颜色,种类客户姓名和手机号(宠物信息用文件保存)。(2)宠物信息修改功能(修改后的信息也要用文件保存)。(3)宠物信息浏览功能。(4)宠物信息删除功能用户:客户可以输入自己的姓名,或者手机号查询自己的宠物信息。整个设计要求采用面向对象程序设计方法进行系统设计,充分应用封装、抽象、继承、多态、重载、模板等概念完成。写出源代码

时间: 2024-03-06 19:52:06 浏览: 26
好的,下面是一个简单的基于面向对象程序设计的宠物信息管理系统的 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` 的文本文件才能正确读写宠物信息。

最新推荐

recommend-type

C++学校人员信息管理系统课程设计.doc

一个用C++写的学校人员管理系统,适合大一大二的课程设计,里面运用到了链表、文件读写等一些C++基本语法操作
recommend-type

汽车租赁信息管理系统源代码 c++.docx

一、为了方便公司管理各种型号的车辆,并实现以下功能: (1)对车辆进行租赁:先输入车牌号,然后输入车辆类别、品牌型号,并在库存中查找该车辆的相关信息,并进行租车。 (2)添加新的车辆信息:主要完成车辆信息...
recommend-type

C++ 学生成绩管理系统设计报告书

基本实现了: (1)学生基本信息、考试科目及成绩的信息录入。 (2)已有学生信息的显示。 (3)学生基本信息的读取和保存输入数据等功能 (4)学生基本信息的查询与修改。可以对已有的学生信息进行修改。 (5)学生基本信息的...
recommend-type

C++实现病人就医管理系统

主要为大家详细介绍了C++语言实现病人就医管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++实验报告——个人银行账户管理系统.docx

北科C++课程实现个人银行账户管理系统的实验报告。北科C++课程实现个人银行账户管理系统的实验报告。北科C++课程实现个人银行账户管理系统的实验报告。北科C++课程实现个人银行账户管理系统的实验报告
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。