#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<vector>
#include<map>
#include<sstream>
string dou_str(double num) //num也可以是int类型,转换为string类型
{
stringstream ss; //stringstream需要sstream头文件
string str;
ss << num;
ss >> str;
return str;
}
//----------------------------------------雇佣类----------------------------------------------
class Employee {
protected:
string name; //名字
string post; //职务
double salary=0; //工资
int no = 0; //工号
static int totalno; //静态数据成员,目前编号最大值
map<string, string>data; //用map存储,方便查找、更改
public:
friend ostream& operator<<(ostream& out, Employee* ob); //重载左移运算符
Employee(); //构造函数
string Getname(); //获取姓名
string Getpost(); //获取职位
int Getno(); //获取工号
double Getsalary(); //获取工资
virtual void pay()=0; //计算月薪,纯虚函数
void Mapdata(); //将数据储存到map里
string Usedata(string key); //通过该函数使用map内的数据
void Changemap(string key,string target);//更改信息的接口
void Changedata(); //用map里的值更新数据
};
int Employee::totalno = 10000;
static double allvalue = 0; //所属部门销售额
Employee::Employee()
{
no = totalno++; //编号自动获得
salary = 0; //工资初为零
name = "";
post = "";
}
string Employee::Getname() //获取姓名
{
return name;
}
string Employee::Getpost() //获取职位
{
return post;
}
int Employee::Getno() //获取工资
{
return no;
}
double Employee::Getsalary()//获取工资
{
return salary;
}
void Employee::Mapdata()
{
data["姓名"] = name;
data["职位"] = post;
data["工号"] = to_string(no); //存到map时,将工号从 int 转换为 string
data["工资"] = dou_str(salary); //存到map时,将工资从 double 转换为 string
}
string Employee::Usedata(string key)
{
return data[key];
}
void Employee::Changemap(string key,string target)
{
data[key] = target;
}
void Employee::Changedata()
{
name = data["姓名"];
post = data["职位"];
no = atoi(data["工号"].c_str());
salary = stod(data["工资"]);
}
//------------------------------------重载左移运算符--------------------------------------------
ostream& operator<<(ostream& out, Employee* ob)
{
cout << "______________________________________________________________" << endl;
cout << "|" << " 姓名:" << ob->name << " " << "职位:" << ob->post << " " << "工号:" << ob->no << " " << "工资:" << " " << ob->salary << " |";
return out;
}
//----------------------------------------------------------------------------------------------
//---------------------------------------经理---------------------------------------------------
class Manager :virtual public Employee {
protected:
double monthlypay;//月薪
public:
Manager();
Manager(string name);
void pay();
};
Manager::Manager()
{
this->name = "";
monthlypay = 8000;
post = "经理";
}
Manager::Manager(string name)
{
this->name = name;
monthlypay = 8000;
post = "经理";
}
void Manager::pay()
{
salary = monthlypay;
}
//------------------------------------兼职技术人员-----------------------------------------------
class Technician :public Employee {
protected:
double workhours; //工作时长
double hourlyrate; //每小时酬金
public:
Technician();
Technician(string name, double hour);
void pay();
};
Technician::Technician()
{
post = "兼职技术人员";
hourlyrate = 100;
workhours = 0;
name = "";
totalno--; //默认构造时,编号不能递增
}
Technician::Technician(string name, double hour)
{
if (hour < 0) {
throw hour;
}
this->name = name;
this->workhours = hour;
post = "兼职技术人员";
hourlyrate = 100;
}
void Technician::pay()
{
salary = workhours * hourlyrate;
}
//----------------------------------------销售员-------------------------------------------------
class Saleman : virtual public Employee {
protected:
double commrate; //按销售额提取酬金的百分比
double sales; //当月销售额
public:
Saleman();
Saleman(string name, double sales);
void pay();
};
Saleman::Saleman()
{
name = "";
post = "销售员";
sales = 0;
commrate = 0.4;
totalno--; //默认构造时,编号不递增
}
Saleman::Saleman(string name, double sales)
{
if (sales < 0)
{
throw sales;
}
this->name = name;
this->sales = sales;
post = "销售员";
commrate = 0.4;
}
void Saleman::pay()
{
allvalue += sales;
salary = sales * commrate;
}
//---------------------------------------销售经理------------------------------------------------
class Salemanager :public Manager, public Saleman{
public:
Salemanager(string name, double sales) ;
void pay();
};
Salemanager::Salemanager(string name,double sales):Manager(name),Saleman(name,sales)
{
commrate = 0.5;
monthlypay = 5000;
post = "销售经理";
}
void Salemanager::pay()
{
salary = monthlypay + allvalue * commrate;
}
//----------------------------公司类,封装增、删、改、查的功能-----------------------------------
class Company {
private:
//用vector储存数据
vector<Employee*>box;
public:
friend ostream& operator<<(ostream& out, Employee* ob); //重载左移运算符
//主菜单
void Mainmenu();
//增加功能
void Add();
//删除功能
void Dedete();
//检查工号是否存在,判断是否存在这个人,删、改都用得到
//工号在map内是字符串形式,用Getno()获取工号
//返回数组下标
int Checkperson(string handle);
//查找功能
void Findmenu(); //查找菜单
void Findperson(string key); //通过map映射来查找
void Find(); //查找
//更改功能
void Changemenu(); //更改菜单
void Changeperson(string key,int person);//person是该员工在数组中的位置
void Change(); //更改,开发中。。。。。。
//更改功能
//显示信息
void Show();
//将数据存入到文档,写入
void Write();
//从文档中读取数据,读取
void Read();
};
void Company::Mainmenu()
{
cout << "____________________________" << endl;
cout << "| |" << endl;
cout << "| 公司工资管理系统 |" << endl;
cout << "|__________________________|" << endl;
cout << "| |" << endl;
cout << "| 1---添加当月工资信息 |" << endl;
cout << "| 2---查找员工信息 |" << endl;
cout << "| 3---更改员工信息 |" << endl;
cout << "| 4---删除员工信息 |" << endl;
cout << "| 5---显示当月工资信息 |" << endl;
cout << "| 6---显示已保存信息 |" << endl;
cout << "| 7---退出并保存 |" << endl;
cout << "|__________________________|" << endl;
cout << "请输入操作选项:";
}
int Company::Checkperson(string handle)
{
cout << "请输入需要" << handle << "的人员工号:" ;
int no;
cin >> no;
for (int i = 0; i < box.size(); i++)
{
if (box[i]->Getno() == no)
{
return i;
}
}
return -1;
}
//-----------------------------------------增加功能----------------------------------------
void Company::Add()
{
Employee *ptr;
int n = 0;
string name;
//经理
cout << "________" << endl;
cout << "| |" << endl;
cout << "| 经理 |" << endl;
cout << "|______|" << endl;
cout << "请输入经理总人数:";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "请输入经理姓名:";
cin >> name;
Manager ob1(name);
ptr = new Manager(ob1);
ptr->pay();
ptr->Mapdata();
box.push_back(ptr);
}
//兼职技术人员
cout << "________________" << endl;
cout << "| |" << endl;
cout << "| 兼职技术人员 |" << endl;
cout << "|______________|" << endl;
cout << "请输入兼职技术人员总人数:";
cin >> n;
double hour = 0;
for (int i = 0; i < n; i++)
{
while (true)
{
cout << "输入兼职技术人员姓名:";
cin >> name;
cout << "输入该人员工作时长:";
cin >> hour;
try {
Technician ob2(name, hour);
ptr = new Technician(ob2);
ptr->pay();
ptr->Mapdata();
box.push_back(ptr);
break;
}
catch (double)
{
cout << "工作时长数据有误,重新输入!" << endl;
}
}
}
//销售员
cout << "__________" << endl;
cout << "| |" << endl;
cout << "| 销售员 |" << endl;
cout << "|________|" << endl;
cout << "请输入销售员总人数:";
cin >> n;
double sales;
for (int i = 0; i < n; i++)
{
while (true)
{
try {
cout << "输入销售员姓名:";
cin >> name;
cout << "输入该销售员该月销售额:";
cin >> sales;
Saleman ob3(name, sales);
ptr = new Saleman(ob3);
ptr->pay();
ptr->Mapdata();
box.push_back(ptr);
break;
}
catch (double)
{
cout << "销售额数据有误,重新输入!" << endl;
}
}
}
//销售经理
cout << "____________" << endl;
cout << "| |" << endl;
cout << "| 销售经理 |" << endl;
cout << "|__________|" << endl;
cout << "输入销售经理名字:";
cin >> name;
Salemanager ob4(name, 0);
ptr = new Salemanager(ob4);
ptr->pay();
ptr->Mapdata();
box.push_back(ptr);
}
//-----------------------------------------删除功能----------------------------------------
void Company::Dedete()
{
int person = Checkperson("删除");//返回的是下标
if (person==-1)
{
cout << "查无此人" << endl;
return;
}
box.erase(box.begin() + person);
cout << "删除成功" << endl;
}
//-----------------------------------------查找功能----------------------------------------
void Company::Findmenu()
{
cout << "________________________" << endl;
cout << "| |" << endl;
cout << "| 查找信息 |" << endl;
cout << "|______________________|" << endl;
cout << "| |" << endl;
cout << "| 1---按照姓名查找 |" << endl;
cout << "| 2---按照职位查找 |" << endl;
cout << "| 3---按照工号查找 |" << endl;
cout << "| 4---按照工资查找 |" << endl;
cout << "| 5---退出查找功能 |" << endl;
cout << "|______________________|" << endl;
cout << "请输入操作选项:";
}
void Company::Findperson(string key)
{
bool check = true;
cout << "输入需要查找的" << key << ":";
string target;
cin >> target;
for (int i = 0; i < box.size(); i++)
{
if (box[i]->Usedata(key) == target)
{
check = false;
cout << box[i] << endl;
}
}
if (check)
{
cout << "未查找到相关信息!" << endl;
}
}
void Company::Find()
{
int chance = 0;
bool check = false;
while (true)
{
Findmenu();
cin >> chance;
switch (chance)
{
case 1:
Findperson("姓名");
break;
case 2:
Findperson("职位");
break;
case 3:
Findperson("工号");
break;
case 4:
Findperson("工资");
break;
case 5:
check = true;
break;
default:
cout << "输入有误!需重新输入!" << endl;
}
if (check)
{
cout << "查找结束!" << endl;
break;
}
}
}
//-----------------------------------------更改功能-----------------------------------------
void Company::Changemenu()
{
cout << "________________________" << endl;
cout << "| |" << endl;
cout << "| 更改信息 |" << endl;
cout << "|______________________|" << endl;
cout << "| |" << endl;
cout << "| 1---更改员工姓名 |" << endl;
cout << "| 2---更改员工工号 |" << endl;
cout << "| 3---更改员工工资 |" << endl;
cout << "| 4---退出更改功能 |" << endl;
cout << "|______________________|" << endl;
cout << "请输入操作选项:";
}
void Company::Changeperson(string key, int person)
{
cout << "输入更改的" << key << ":";
string target;
cin >> target;
box[person]->Changemap(key, target);
box[person]->Changedata();
cout << "更改成功!" << endl;
}
void Company::Change()
{
int person = Checkperson("更改");//接收数组下标
if (person==-1)
{
cout << "未查找到该成员!" << endl;
return;
}
cout << "该员工原信息:";
cout << box[person] << endl;
int chance;
bool check = false;
while (true)
{
Changemenu();
cin >> chance;
switch (chance)
{
case 1:
Changeperson("姓名", person);
break;
case 2:
Changeperson("工号", person);
break;
case 3:
Changeperson("工资", person);
break;
case 4:
check = true;
break;
default:
cout << "输入有误!" << endl;
}
if (check)
{
cout << "更改信息结束!" << endl;
break;
}
}
}
//-----------------------------------------显示所有人的信息-----------------------------------------
void Company::Show()
{
cout << "总人数:" << box.size() << endl;
for (int i = 0; i < box.size(); i++)
{
cout << box[i] << endl;
}
}
//-----------------------------------------写入信息--------------------------------------------------
void Company::Write()
{
fstream out("Companyfile.txt", ios::app);
if (!out)
{
cout << "文件打开失败" << endl;
return ;
}
string s;
for (int i = 0; i < box.size(); i++)
{
out << "----------------------------------------------------------" << endl;
out<< "姓名:" << box[i]->Getname() << " 职位:" << box[i]->Getpost() << " 工号:" << box[i]->Getno() << " 月薪:" << box[i]->Getsalary() << endl;
}
cout << "保存成功!" << endl;
out.close();
}
//----------------------------------------读取信息-----------------------------------------------------
void Company::Read()
{
fstream in("Companyfile.txt", ios::in);
string s;
if (!in)
{
cout << "文件打开失败" << endl;
return;
}
char c;
in >> c;
if (in.eof())
{
cout << "未存储信息!" << endl;
return;
}
in.seekg(0);
while (in>>s)
{
cout << s << endl;
}
in.close();
}
int main()
{
Company com;
int chance;
bool check = false;
while (true)
{
com.Mainmenu();
cin >> chance;
switch (chance)
{
case 1:
com.Add();
break;
case 2:
com.Find();
break;
case 3:
com.Change();
break;
case 4:
com.Dedete();
break;
case 5:
com.Show();
break;
case 6:
com.Read();
break;
case 7:
com.Write();
check = true;
break;
default:
cout << "输入有误!" << endl;
}
if (check)
{
cout << "关闭管理系统!" << endl;
break;
}
}
return 0;
}
C++小型公司工资管理系统大作业
于 2023-12-13 13:45:37 首次发布