c++职工工资管理系统
时间: 2023-12-11 13:03:44 浏览: 42
以下是一个简单的C++职工工资管理系统的实现,包括添加员工、查询员工、显示员工信息、删除员工信息、修改员工信息以及退出系统等功能。具体实现如下:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 员工类
class Employee {
public:
Employee(string id, string name, string gender, int age, double salary, double workHours) {
this->id = id;
this->name = name;
this->gender = gender;
this->age = age;
this->salary = salary;
this->workHours = workHours;
}
string getId() {
return id;
}
string getName() {
return name;
}
string getGender() {
return gender;
}
int getAge() {
return age;
}
double getSalary() {
return salary;
}
double getWorkHours() {
return workHours;
}
void setSalary(double salary) {
this->salary = salary;
}
void setWorkHours(double workHours) {
this->workHours = workHours;
}
private:
string id; // 工号
string name; // 姓名
string gender; // 性别
int age; // 年龄
double salary; // 工资
double workHours; // 工时
};
// 员工管理类
class EmployeeManager {
public:
// 添加员工
void addEmployee(Employee employee) {
employees.push_back(employee);
}
// 查询员工
Employee* findEmployee(string id) {
for (int i = 0; i < employees.size(); i++) {
if (employees[i].getId() == id) {
return &employees[i];
}
}
return NULL;
}
// 显示所有员工信息
void showAllEmployees() {
for (int i = 0; i < employees.size(); i++) {
cout << "工号:" << employees[i].getId() << ",姓名:" << employees[i].getName() << ",性别:" << employees[i].getGender() << ",年龄:" << employees[i].getAge() << ",工资:" << employees[i].getSalary() << ",工时:" << employees[i].getWorkHours() << endl;
}
}
// 删除员工
void deleteEmployee(string id) {
for (vector<Employee>::iterator it = employees.begin(); it != employees.end(); it++) {
if (it->getId() == id) {
employees.erase(it);
break;
}
}
}
// 修改员工信息
void modifyEmployee(string id, double salary, double workHours) {
for (int i = 0; i < employees.size(); i++) {
if (employees[i].getId() == id) {
employees[i].setSalary(salary);
employees[i].setWorkHours(workHours);
break;
}
}
}
private:
vector<Employee> employees; // 员工列表
};
// 主函数
int main() {
EmployeeManager employeeManager;
while (true) {
cout << "请选择操作:1.添加员工 2.查询员工 3.显示所有员工信息 4.删除员工 5.修改员工信息 0.退出系统" << endl;
int choice;
cin >> choice;
if (choice == 1) {
cout << "请输入员工工号、姓名、性别、年龄、工资和工时,用空格隔开:" << endl;
string id, name, gender;
int age;
double salary, workHours;
cin >> id >> name >> gender >> age >> salary >> workHours;
Employee employee(id, name, gender, age, salary, workHours);
employeeManager.addEmployee(employee);
cout << "添加成功!" << endl;
} else if (choice == 2) {
cout << "请输入要查询的员工工号:" << endl;
string id;
cin >> id;
Employee* employee = employeeManager.findEmployee(id);
if (employee == NULL) {
cout << "该员工不存在!" << endl;
} else {
cout << "工号:" << employee->getId() << ",姓名:" << employee->getName() << ",性别:" << employee->getGender() << ",年龄:" << employee->getAge() << ",工资:" << employee->getSalary() << ",工时:" << employee->getWorkHours() << endl;
}
} else if (choice == 3) {
employeeManager.showAllEmployees();
} else if (choice == 4) {
cout << "请输入要删除的员工工号:" << endl;
string id;
cin >> id;
employeeManager.deleteEmployee(id);
cout << "删除成功!" << endl;
} else if (choice == 5) {
cout << "请输入要修改的员工工号、工资和工时,用空格隔开:" << endl;
string id;
double salary, workHours;
cin >> id >> salary >> workHours;
employeeManager.modifyEmployee(id, salary, workHours);
cout << "修改成功!" << endl;
} else if (choice == 0) {
break;
} else {
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
```