C++代码:现在已有水、电、煤气三类,以及教工信息、学生信息两类,请再此基础上写一个管理类,实现人员信息录入、修改、增加、删除、以及查询系统能够找出该用户半年之内的交费记录和本次应交费的数据
时间: 2023-07-10 07:30:49 浏览: 106
好的,我可以为您提供一个简单的示例代码,如下所示:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
// 基类,人员信息类
class PersonInfo {
public:
PersonInfo(string name, int age, string id) : name(name), age(age), id(id) {}
string getName() const { return name; }
int getAge() const { return age; }
string getId() const { return id; }
protected:
string name;
int age;
string id;
};
// 水费类
class WaterFee {
public:
WaterFee(double fee) : fee(fee) {}
double getFee() const { return fee; }
private:
double fee;
};
// 电费类
class ElectricFee {
public:
ElectricFee(double fee) : fee(fee) {}
double getFee() const { return fee; }
private:
double fee;
};
// 煤气费类
class GasFee {
public:
GasFee(double fee) : fee(fee) {}
double getFee() const { return fee; }
private:
double fee;
};
// 缴费记录类
class PaymentRecord {
public:
PaymentRecord(double fee, time_t time) : fee(fee), time(time) {}
double getFee() const { return fee; }
time_t getTime() const { return time; }
private:
double fee;
time_t time;
};
// 教工信息类
class TeacherInfo : public PersonInfo {
public:
TeacherInfo(string name, int age, string id, string department) : PersonInfo(name, age, id), department(department) {}
string getDepartment() const { return department; }
private:
string department;
};
// 学生信息类
class StudentInfo : public PersonInfo {
public:
StudentInfo(string name, int age, string id, string major) : PersonInfo(name, age, id), major(major) {}
string getMajor() const { return major; }
private:
string major;
};
// 管理类
class Management {
public:
// 录入信息
void addPersonInfo(PersonInfo* info) {
personInfos.push_back(info);
}
void addWaterFee(string id, WaterFee* fee) {
waterFees[id].push_back(fee);
}
void addElectricFee(string id, ElectricFee* fee) {
electricFees[id].push_back(fee);
}
void addGasFee(string id, GasFee* fee) {
gasFees[id].push_back(fee);
}
// 修改信息
void modifyPersonInfo(string id, string name, int age) {
for (auto& info : personInfos) {
if (info->getId() == id) {
info->name = name;
info->age = age;
break;
}
}
}
// 增加信息
void addWaterFeeRecord(string id, double fee) {
addWaterFee(id, new WaterFee(fee));
}
void addElectricFeeRecord(string id, double fee) {
addElectricFee(id, new ElectricFee(fee));
}
void addGasFeeRecord(string id, double fee) {
addGasFee(id, new GasFee(fee));
}
// 删除信息
void deletePersonInfo(string id) {
for (auto it = personInfos.begin(); it != personInfos.end(); ++it) {
if ((*it)->getId() == id) {
personInfos.erase(it);
break;
}
}
}
// 查询信息
void printPersonInfo(string id) const {
for (auto& info : personInfos) {
if (info->getId() == id) {
cout << "Name: " << info->getName() << endl;
cout << "Age: " << info->getAge() << endl;
if (auto teacherInfo = dynamic_cast<TeacherInfo*>(info)) {
cout << "Department: " << teacherInfo->getDepartment() << endl;
}
if (auto studentInfo = dynamic_cast<StudentInfo*>(info)) {
cout << "Major: " << studentInfo->getMajor() << endl;
}
printPaymentRecords(id);
break;
}
}
}
void printPaymentRecords(string id) const {
if (waterFees.count(id) > 0) {
cout << "Water Fees:" << endl;
for (auto& record : waterFees.at(id)) {
time_t time = record->getTime();
cout << "Fee: " << record->getFee() << ", Time: " << ctime(&time) << endl;
}
}
if (electricFees.count(id) > 0) {
cout << "Electric Fees:" << endl;
for (auto& record : electricFees.at(id)) {
time_t time = record->getTime();
cout << "Fee: " << record->getFee() << ", Time: " << ctime(&time) << endl;
}
}
if (gasFees.count(id) > 0) {
cout << "Gas Fees:" << endl;
for (auto& record : gasFees.at(id)) {
time_t time = record->getTime();
cout << "Fee: " << record->getFee() << ", Time: " << ctime(&time) << endl;
}
}
}
// 随机生成半年内的交费记录
void generatePaymentRecords(string id) {
time_t now = time(nullptr);
for (int i = 0; i < 10; ++i) {
double fee = (rand() % 100) + 50;
time_t time = now - (rand() % 1000000);
addWaterFee(id, new WaterFee(fee));
addElectricFee(id, new ElectricFee(fee));
addGasFee(id, new GasFee(fee));
}
}
private:
vector<PersonInfo*> personInfos;
map<string, vector<WaterFee*>> waterFees;
map<string, vector<ElectricFee*>> electricFees;
map<string, vector<GasFee*>> gasFees;
};
int main() {
// 测试代码
Management management;
auto teacherInfo = new TeacherInfo("Tom", 30, "001", "Computer Science");
auto studentInfo = new StudentInfo("Jerry", 20, "002", "Mathematics");
management.addPersonInfo(teacherInfo);
management.addPersonInfo(studentInfo);
management.addWaterFeeRecord("001", 200);
management.addElectricFeeRecord("001", 300);
management.addGasFeeRecord("001", 400);
management.addWaterFeeRecord("002", 100);
management.addElectricFeeRecord("002", 200);
management.addGasFeeRecord("002", 300);
management.printPersonInfo("001");
management.printPersonInfo("002");
management.generatePaymentRecords("001");
management.generatePaymentRecords("002");
management.printPersonInfo("001");
management.printPersonInfo("002");
return 0;
}
```
在这个示例代码中,我们定义了几个类来表示人员信息、缴费记录以及管理类。在管理类中,我们定义了一些方法来实现人员信息的录入、修改、增加、删除以及查询,同时也实现了随机生成半年内的交费记录的功能。在查询的时候,我们可以通过调用 `printPersonInfo()` 方法来打印出某个人员的基本信息和缴费记录,也可以通过调用 `printPaymentRecords()` 方法来单独打印出某个人员的缴费记录。
阅读全文