题目
a. 构造一个雇员class。每个员工由一个整数ID号、双精度工资率和员工每周应工作的最大小时数。该类提供的服务有,为新雇员输入数据的功能,为新雇员更改数据的功能,以及为新雇员显示现有数据的功能。
b. 要求为三个雇员输入数据,然后显示输入的数据。
c. 修改程序,使其包含一个为用户提供以下选择的菜单:
- 添加员工
- 修改员工数据
- 删除员工
- 退出菜单
程序应该启动一个操作来实现用户的选择。
代码
#include <iostream>
#include <vector>
using namespace std;
class Employee
{
private:
int employeeID;
int maxHours;
double payRate;
public:
Employee(int = 0, int = 0, double = 1); // 默认值设置
void setID(int id);
void setMaxHours(int h);
void setPayRate(double rate);
void showData();
};
Employee::Employee(int id, int h, double rate)
{
employeeID = id;
maxHours = h;
payRate = rate;
}
void Employee::setID(int id)
{
employeeID = id;
}
void Employee::setMaxHours(int h)
{
maxHours = h;
}
void Employee::setPayRate(double rate)
{
payRate = rate;
}
void Employee::showData()
{
cout << "Employee ID: " << employeeID << ", max working hours: "
<< maxHours << ", pay rate: " << payRate << endl;
}
// test function
int main()
{
vector<Employee> emp(0);
int empLength = 0;
while(true)
{
cout << "1. Add an employee\n"
<< "2. Modify employee data\n"
<< "3. Delete an employee\n"
<< "4. Exit this menu \n";
int input;
cin >> input;
if(input == 1)
{
emp.resize(++empLength);
int tmp;
double tmpDouble;
cout << "------------------\n";
cout << "Input employee ID: ";
cin >> tmp;
emp[empLength - 1].setID(tmp);
cout << "Input max working hours: ";
cin >> tmp;
emp[empLength - 1].setMaxHours(tmp);
cout << "Input payRate: ";
cin >> tmpDouble;
emp[empLength - 1].setPayRate(tmpDouble);
}
else if(input == 2)
{
int ind;
int tmp;
double tmpDouble;
cout << "------------------\n";
cout << "Input the storing index of the employee: ";
cin >> ind;
if(ind < 0 || ind >= empLength)
{
cout << "Invalid index input!";
exit(1);
}
cout << "Input new employee ID: ";
cin >> tmp;
emp[ind].setID(tmp);
cout << "Input new max working hours: ";
cin >> tmp;
emp[ind].setMaxHours(tmp);
cout << "Input new payRate: ";
cin >> tmpDouble;
emp[ind].setPayRate(tmpDouble);
}
else if(input == 3)
{
int ind;
cout << "------------------\n";
cout << "Input the storing index of the employee: ";
cin >> ind;
emp.erase(emp.begin() + ind);
emp.resize(--empLength);
}
else break;
system("CLS");
}
return 0;
}