[ { "id": 1, "customer_id": 1, "type": 1, "is_auto_push": 0 }, { "id": 2, "customer_id": 1, "type": 2, "is_auto_push": 1 }, { "id": 3, "customer_id": 1, "type": 3, "is_auto_push": 1 }, { "id": 4, "customer_id": 1, "type": 4, "is_auto_push": 0 }, { "id": 5, "customer_id": 1, "type": 5, "is_auto_push": 0 } ] 遍历出is_auto_push==1的对象
时间: 2023-12-16 22:04:22 浏览: 97
好的,可以使用以下代码来遍历出is_auto_push等于1的对象:
```
data = [
{"id": 1, "customer_id": 1, "type": 1, "is_auto_push": 0},
{"id": 2, "customer_id": 1, "type": 2, "is_auto_push": 1},
{"id": 3, "customer_id": 1, "type": 3, "is_auto_push": 1},
{"id": 4, "customer_id": 1, "type": 4, "is_auto_push": 0},
{"id": 5, "customer_id": 1, "type": 5, "is_auto_push": 0}
]
for item in data:
if item['is_auto_push'] == 1:
print(item)
```
输出结果为:
```
{'id': 2, 'customer_id': 1, 'type': 2, 'is_auto_push': 1}
{'id': 3, 'customer_id': 1, 'type': 3, 'is_auto_push': 1}
```
相关问题
用C++开发一个简单的宠物信息管理系统.要求实现以下基本功能:有简单的用户管理功能,不同用户在登录系统后会有不同的菜单可进行不同的操作。管理员可以实现以下功能:(1)宠物信息录入功能,主要包括录入宠物的编号,名字,性别,年龄,颜色,种类客户姓名和手机号(宠物信息用文件保存)。(2)宠物信息修改功能(修改后的信息也要用文件保存)。(3)宠物信息浏览功能。(4)宠物信息删除功能用户:客户可以输入自己的姓名,或者手机号查询自己的宠物信息。整个设计要求采用面向对象程序设计方法进行系统设计,充分应用封装、抽象、继承、多态、重载、模板等概念完成。写出源代码
好的,下面是一个简单的基于面向对象程序设计的宠物信息管理系统的 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` 的文本文件才能正确读写宠物信息。
用c++开发一个简单的宠物信息管理系统,并给出源代码。要求实现以下基本功能:有简单的用户管理功能,不同用户在登录系统后会有不同的菜单可进行不同的操作。管理员可以实现以下功能:(1)宠物信息录入功能,主要包括录入宠物的编号,名字,性别,年龄,颜色,种类,客户姓名和手机号 宠物信息必须用文件保存起来。(2)宠物信息修改功能(修改后的信息也要用文件保存)。(3)宠物信息浏览功能。(4)宠物信息删除功能 。(5)模糊查询功能。(6)按编号大小排序功能 用户:可以通过姓名,密码登录,进入客户端,客户可以输入自己的姓名,或者手机号查询自己的宠物信息。要求充分应用封装、抽象、继承、多态、重载、模板完成。
好的,以下是一个用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;
}
```
阅读全文