用C++设计一个小型企业工资管理系统。 设计说明:该小型企业的员工主要有 4 类:经理、技术员、销售员和计时工。要求存储员工的工号、姓名、岗位、月工资等基本信息。 工资计算方法: 1经理:领取固定月薪 8800 元 2技术员:分高级、中级、初级三类,其中高级固定每月 7700 元,中级 固定每月 6600 元,初级固定每月 5500 元 3销售员:底薪 2500+销售额*2%提成 4计时工:工作时间*每小时 66 元 该企业员工的类层次结构为:员工包含经理,技术员,销售员,计时工 要有一个菜单,用于选择各项功能: 1数据录入:输入各种数据(其中每类员工不能少于 3 人) 2数据查询:能根据工号或姓名查询工资信息 3职工信息的删除、修改 4工资计算:能对各类员工进行工资计算 5数据输出:能按类别输出员工信息及工资信息(包括计算工资时所用到 的相关数据,例如:销售员的销售额) 6设置登录本系统的用户名和密码,通过用户名和密码登录
时间: 2023-06-27 17:06:42 浏览: 137
这是一个比较复杂的问题,需要设计多个类和函数,我将给出一个基本的框架,你可以在此基础上进行修改和扩展。
首先,我们需要一个基类 Employee,包含员工的基本信息,如工号、姓名、岗位和月工资。其中,工号和姓名是必须的,而岗位和月工资可以在派生类中进行重载。代码如下:
```cpp
class Employee {
public:
Employee(string id, string name) : id_(id), name_(name) {}
virtual ~Employee() {}
string getId() const { return id_; }
string getName() const { return name_; }
virtual string getPost() const { return ""; }
virtual double getSalary() const { return 0; }
protected:
string id_;
string name_;
};
```
接下来,我们可以派生出经理、技术员、销售员和计时工类,分别实现它们的工资计算方法。代码如下:
```cpp
class Manager : public Employee {
public:
Manager(string id, string name) : Employee(id, name) {}
virtual string getPost() const { return "经理"; }
virtual double getSalary() const { return 8800; }
};
class Technician : public Employee {
public:
enum Level { Junior, Intermediate, Senior };
Technician(string id, string name, Level level)
: Employee(id, name), level_(level) {}
virtual string getPost() const { return "技术员"; }
virtual double getSalary() const {
switch (level_) {
case Junior: return 5500;
case Intermediate: return 6600;
case Senior: return 7700;
default: return 0;
}
}
private:
Level level_;
};
class Salesman : public Employee {
public:
Salesman(string id, string name, double sales)
: Employee(id, name), sales_(sales) {}
virtual string getPost() const { return "销售员"; }
virtual double getSalary() const { return 2500 + sales_ * 0.02; }
private:
double sales_;
};
class HourlyWorker : public Employee {
public:
HourlyWorker(string id, string name, int hours)
: Employee(id, name), hours_(hours) {}
virtual string getPost() const { return "计时工"; }
virtual double getSalary() const { return hours_ * 66; }
private:
int hours_;
};
```
接下来,我们需要一个管理员类,用于录入、查询、删除、修改和输出员工信息。代码如下:
```cpp
class Administrator {
public:
Administrator() {}
void addEmployee(Employee* emp) {
employees_.push_back(emp);
}
Employee* findEmployeeById(string id) {
for (Employee* emp : employees_) {
if (emp->getId() == id) {
return emp;
}
}
return nullptr;
}
vector<Employee*> findEmployeesByName(string name) {
vector<Employee*> results;
for (Employee* emp : employees_) {
if (emp->getName() == name) {
results.push_back(emp);
}
}
return results;
}
bool deleteEmployee(string id) {
for (auto it = employees_.begin(); it != employees_.end(); ++it) {
if ((*it)->getId() == id) {
delete *it;
employees_.erase(it);
return true;
}
}
return false;
}
bool modifyEmployee(string id, string name) {
Employee* emp = findEmployeeById(id);
if (emp) {
emp->name_ = name;
return true;
}
return false;
}
void calculateSalaries() {
for (Employee* emp : employees_) {
cout << emp->getName() << " " << emp->getPost() << " 月工资为 "
<< emp->getSalary() << " 元" << endl;
}
}
void outputEmployeesByPost() {
map<string, vector<Employee*>> postToEmployees;
for (Employee* emp : employees_) {
postToEmployees[emp->getPost()].push_back(emp);
}
for (auto& pair : postToEmployees) {
cout << pair.first << ":" << endl;
for (Employee* emp : pair.second) {
cout << " 工号:" << emp->getId()
<< ",姓名:" << emp->getName()
<< ",月工资:" << emp->getSalary() << " 元"
<< endl;
}
}
}
private:
vector<Employee*> employees_;
};
```
最后,我们需要一个用户类,用于设置用户名和密码,以及登录系统。代码如下:
```cpp
class User {
public:
User(string username, string password)
: username_(username), password_(password) {}
bool login(string username, string password) const {
return username == username_ && password == password_;
}
void setUsername(string username) { username_ = username; }
void setPassword(string password) { password_ = password; }
private:
string username_;
string password_;
};
```
在主函数中,我们可以使用以上类来实现菜单功能。代码如下:
```cpp
int main() {
Administrator admin;
User user("admin", "123456");
while (true) {
cout << "请选择功能:" << endl;
cout << "1.数据录入" << endl;
cout << "2.数据查询" << endl;
cout << "3.职工信息的删除、修改" << endl;
cout << "4.工资计算" << endl;
cout << "5.数据输出" << endl;
cout << "6.设置用户名和密码" << endl;
cout << "0.退出系统" << endl;
int choice;
cin >> choice;
if (choice == 0) {
break;
}
if (choice == 1) {
cout << "请输入员工信息:" << endl;
while (true) {
cout << "工号(输入0结束):";
string id;
cin >> id;
if (id == "0") {
break;
}
cout << "姓名:";
string name;
cin >> name;
cout << "岗位(1.经理 2.技术员 3.销售员 4.计时工):";
int post;
cin >> post;
if (post == 1) {
admin.addEmployee(new Manager(id, name));
} else if (post == 2) {
cout << "级别(1.初级 2.中级 3.高级):";
int level;
cin >> level;
admin.addEmployee(new Technician(id, name, static_cast<Technician::Level>(level - 1)));
} else if (post == 3) {
cout << "销售额:";
double sales;
cin >> sales;
admin.addEmployee(new Salesman(id, name, sales));
} else if (post == 4) {
cout << "工作时间(小时):";
int hours;
cin >> hours;
admin.addEmployee(new HourlyWorker(id, name, hours));
}
}
}
if (choice == 2) {
cout << "请输入员工工号或姓名:";
string keyword;
cin >> keyword;
Employee* emp = admin.findEmployeeById(keyword);
if (emp) {
cout << "工号:" << emp->getId()
<< ",姓名:" << emp->getName()
<< ",岗位:" << emp->getPost()
<< ",月工资:" << emp->getSalary() << " 元"
<< endl;
} else {
vector<Employee*> results = admin.findEmployeesByName(keyword);
if (results.empty()) {
cout << "未找到员工信息" << endl;
} else {
cout << "共找到 " << results.size() << " 条记录:" << endl;
for (Employee* emp : results) {
cout << " 工号:" << emp->getId()
<< ",姓名:" << emp->getName()
<< ",岗位:" << emp->getPost()
<< ",月工资:" << emp->getSalary() << " 元"
<< endl;
}
}
}
}
if (choice == 3) {
cout << "请输入员工工号:";
string id;
cin >> id;
cout << "请选择要进行的操作(1.删除 2.修改):";
int operation;
cin >> operation;
if (operation == 1) {
if (admin.deleteEmployee(id)) {
cout << "删除成功" << endl;
} else {
cout << "未找到该员工" << endl;
}
} else if (operation == 2) {
cout << "请输入员工姓名:";
string name;
cin >> name;
if (admin.modifyEmployee(id, name)) {
cout << "修改成功" << endl;
} else {
cout << "未找到该员工" << endl;
}
}
}
if (choice == 4) {
admin.calculateSalaries();
}
if (choice == 5) {
admin.outputEmployeesByPost();
}
if (choice == 6) {
string username, password;
cout << "请输入用户名:";
cin >> username;
cout << "请输入密码:";
cin >> password;
user.setUsername(username);
user.setPassword(password);
}
}
return 0;
}
```
注意:以上代码中并没有对用户名和密码进行验证,你需要自己添加验证功能。此外,还可以添加一些其他的功能,例如修改岗位和月工资等。
阅读全文