C++ 建立工资计算系统(3)

需求描述

1、企业增加了一种全新的工种,称为销售顾问,其工资按照顾问咨询次数和销售产品的数量共同计算,每咨询一次报酬300元,每销售一件产品提成50元,请在已有类的基础上设计该员工类;
2、由于员工类型多样,系统需要采用多态性对各类员工对象数据进行统一保存和处理,请根据此要求将已有类中涉及到员工工资计算和信息输出的接口设置为虚函数,为多态性的使用奠定基础;
3、设计类型SalaryManager,其成员变量可保存系统中所有员工类型的对象,设计接口getTotalSalary和getAllInfo,分别计算所有员工的工资以及输出所有员工的信息;
4、编写一个测试main方法,为每种员工类型创建1-2个对象,并使用SalaryManager类型对象进行管理,调用方法输出所有员工的工资以及员工信息。

类图设计

总体类图:
C++ 建立工资计算系统(3)_第1张图片

Employee类:
C++ 建立工资计算系统(3)_第2张图片
C++ 建立工资计算系统(3)_第3张图片
ProductLineEmployee类:
C++ 建立工资计算系统(3)_第4张图片

SaleEmployee类:
C++ 建立工资计算系统(3)_第5张图片

AdviserEmployee类:
C++ 建立工资计算系统(3)_第6张图片
SaleAdviserEmployee类:
C++ 建立工资计算系统(3)_第7张图片
SalaryManager类:
C++ 建立工资计算系统(3)_第8张图片

源代码

//class.h
#include 
#include 

using namespace std;

class Date{
    int year;
    int month;
    int day;

public:
    Date();
    Date(int year,int month,int day);
    Date(const Date& x);
    ~Date();
    Date& operator=(const Date& x);
    int getYear();
    void setYear(int year);
    int getMonth();
    void setMonth(int month);
    int getDay();
    void setDay(int day);
    void pritDate();
    float returnSub(Date s);
};

class Salary{
    double money;

public:
    Salary();
    Salary(double money);
    Salary(const Salary& x);
    ~Salary();
    Salary& operator=(const Salary& x);
    Salary& operator+(const Salary& x);
    Salary& operator-(const Salary& x);
    double getMoney();
    void setMoney(double money);
};

class Employee{
    static int counts;
    int no;
    string name;
    Salary sal;
    Date start;
    Date last;

public:
    Employee();
    Employee(string name,Salary sal,Date start,Date last);
    Employee(const Employee& x);
    ~Employee();
    Employee& operator=(const Employee& x);
    int getNo();
    string getName();
    void setName(string name);
    Salary getSalary();
    void setSalary(Salary sal);
    Date getStart();
    void setStart(Date start);
    Date getLast();
    void setLast(Date last);
    virtual void getMessage();
    virtual void printSalary();
};

class ProductLineEmployee: public Employee{
    string type;

public:
    ProductLineEmployee();
    ProductLineEmployee(string name,Salary sal,Date start,Date last);
    ProductLineEmployee(const ProductLineEmployee& x);
    ~ProductLineEmployee();
    ProductLineEmployee& operator=(const ProductLineEmployee& x);
    void getMessage();
    void printSalary();
};

class SaleEmployee: public Employee{
    string type;

public:
    SaleEmployee();
    SaleEmployee(string name,Salary sal,Date start,Date last);
    SaleEmployee(const SaleEmployee& x);
    ~SaleEmployee();
    SaleEmployee& operator=(const SaleEmployee& x);
    void getMessage();
    void printSalary();
};

class AdviserEmployee: public Employee{
    string type;

public:
    AdviserEmployee();
    AdviserEmployee(string name,Salary sal,Date start,Date last);
    AdviserEmployee(const AdviserEmployee& x);
    ~AdviserEmployee();
    AdviserEmployee& operator=(const AdviserEmployee& x);
    void getMessage();
    void printSalary();
};

class SaleAdviserEmployee: public Employee{
    string type;
public:
    SaleAdviserEmployee();
    SaleAdviserEmployee(string name,Salary sal,Date start,Date last);
    SaleAdviserEmployee(const SaleAdviserEmployee& x);
    ~SaleAdviserEmployee();
    SaleAdviserEmployee& operator=(const SaleAdviserEmployee& x);
    void getMessage();
    void printSalary();
};

class SalaryManager{
    vector<Employee*> ob;
public:
    SalaryManager();
    ~SalaryManager();
    void insertEmployee(Employee* x);
    void delEmployee();
    void getTotalSalary();
    void getTotalInfo();
};




//class.cpp
#include "class.h"
#include 

using namespace std;

Date::Date(){
    year = 2020;
    month = 5;
    day = 19;
}

Date::Date(int year,int month,int day){
    this->year = year;
    this->month = month;
    this->day = day;
}

Date::Date(const Date& x){
    year = x.year;
    month = x.month;
    day = x.day;
}

Date::~Date(){};

Date& Date::operator=(const Date& x){
    if(this == &x)
        return *this;
    year = x.year;
    month = x.month;
    day = x.day;
    return *this;
}

int Date:: getYear(){
    return year;
}


void Date::setYear(int year){
    if(year <= 0){
        cout << "data is illegal!" << endl;
        this->year = 2020;
    }
    else
        this->year = year;
}

int Date::getMonth(){
    return month;
}

void Date::setMonth(int month){
    if(month <= 0 || month >12){
        cout << "data is illegal!" << endl;
        this->month = 5;
    }
    else
        this->month = month;
}

int Date::getDay(){
    return day;
}

void Date::setDay(int day){
    if(day <= 0 || day > 31){
        cout << "data is illegal!" << endl;
        this->day = 19;
    }
    else
        this->day = day;
}

void Date::pritDate(){
    cout << year << "-" << month << "-" << day << endl;
}

float Date::returnSub(Date s){
    return ((year-s.year)*12 + ((month+day/30) - (s.month +s.day/30)));
}

Salary::Salary(){
    money = 0.0;
}

Salary::Salary(double money){
    this->money = money;
}

Salary::Salary(const Salary& x){
    money = x.money;
}

Salary::~Salary(){};

Salary& Salary::operator=(const Salary& x){
    if(this == &x)
        return *this;
    money = x.money;
    return *this;
}

Salary& Salary::operator+(const Salary& x){
    money += x.money;
    return *this;
}

Salary& Salary::operator-(const Salary& x){
    money -= x.money;
    return *this;
}

double Salary::getMoney(){
    return money;
}

void Salary::setMoney(double money){
    if(money < 0){
        cout << "data is illegal!" << endl;
        this->money = 0.0;
    }
    else
        this->money = money;
}

int Employee::counts = 0;

Employee::Employee(){
    no = ++counts;
    name = "null";
    sal = 0.0;
}

Employee::Employee(string name,Salary sal,Date start,Date last){
    no = ++counts;
    this->name = name;
    this->sal = sal;
    this->start = start;
    this->last = last;
}

Employee::Employee(const Employee& x){
    no = ++counts;
    name = x.name;
    sal = x.sal;
    start = x.start;
    last = x.last;
}

Employee::~Employee(){
    counts--;
}

Employee& Employee::operator=(const Employee& x){
    if(this == &x)
        return *this;
    no = ++counts;
    name = x.name;
    sal = x.sal;
    start = x.start;
    last = x.last;
    return *this;
}

int Employee::getNo(){
    return no;
}

string Employee::getName(){
    return name;
}

void Employee::setName(string name){
    this->name = name;
}

Salary Employee:: getSalary(){
    return sal;
}

void Employee::setSalary(Salary sal){
    double temp = sal.getMoney();
    if(temp < 0){
        cout << "date is illegal!" << endl;
        this->sal = 0.0;
    }
    else
        this->sal = sal;
}

Date Employee:: getStart(){
    return start;
}

void Employee::setStart(Date start){
    this->start = start;
}

Date Employee::getLast(){
    return last;
}

void Employee::setLast(Date last){
    this->last = last;
}

void Employee::getMessage(){
    cout << "no: " << no << endl;
    cout << "name: " << name << endl;
    double t1 = sal.getMoney() ;
    cout << "salary: " << t1<< endl;
    cout << "startDate: " ;
    start.pritDate();
    cout << "lastDate: ";
    last.pritDate();
    cout << "please input any key to continue_ " << endl;
    char ch = getchar();
}

void Employee::printSalary(){}

ProductLineEmployee::ProductLineEmployee(){
    type = "ProductLineEmployee";
}

ProductLineEmployee::ProductLineEmployee(string name,Salary sal,Date start,Date last):Employee(name,sal,start,last){
    this->type = "ProductLineEmployee";
}

ProductLineEmployee::ProductLineEmployee(const ProductLineEmployee& x):Employee(x){
    type = x.type;
}

ProductLineEmployee::~ProductLineEmployee(){}

ProductLineEmployee& ProductLineEmployee::operator=(const ProductLineEmployee& x){
    if(this == &x)
        return *this;
    type = x.type;
    return *this;
}

void ProductLineEmployee::getMessage(){
    cout << "no: " << this->getNo() << endl;
    cout << "name: " << this->getName() << endl;
    cout << "employeeType: " << type << endl;
    Salary t1 = this->getSalary();
    double t2 = t1.getMoney();
    cout << "salary: " << t2 << endl;
    Date t3 = this->getStart();
    cout << "startDate: ";
    t3.pritDate();
    t3 = this->getLast();
    cout << "lastDate: ";
    t3.pritDate();
    cout << "please input any key to continue_ ";
    char ch = getchar();
}

void ProductLineEmployee::printSalary(){
    Salary t1 = this->getSalary();
    double t2 = t1.getMoney();
    Date t3 = this->getLast();
    Date t4 = this->getStart();
    float f = t3.returnSub(t4);
    cout << "count salary: " << t2*f << endl;
}

SaleEmployee::SaleEmployee(){
    type = "SaleEmployee";
}

SaleEmployee::SaleEmployee(string name,Salary sal,Date start,Date last):Employee(name,sal,start,last){
    this->type = "SaleEmployee";
}

SaleEmployee::SaleEmployee(const SaleEmployee& x):Employee(x){
    type = x.type;
}

SaleEmployee::~SaleEmployee(){}

SaleEmployee& SaleEmployee::operator=(const SaleEmployee& x){
    if(this == &x)
        return *this;
    type = x.type;
    return *this;
}

void SaleEmployee::getMessage(){
    cout << "no: " << this->getNo() << endl;
    cout << "name: " << this->getName() << endl;
    cout << "employeeType: " << type << endl;
    Salary t1 = this->getSalary();
    double t2 = t1.getMoney();
    cout << "basic salary: " <<t2 << endl;
    Date t3 = this->getStart();
    cout << "startDate: ";
    t3.pritDate();
    t3 = this->getLast();
    cout << "lastDate: " ;
    t3.pritDate();
    cout << "please input any key to continue_ ";
    char ch = getchar();
}

void SaleEmployee::printSalary(){
    Salary t1 = this->getSalary();
    double t2 = t1.getMoney();
    Date t3 = this->getLast();
    Date t4 = this->getStart();
    float f = t3.returnSub(t4);
    cout << "count salary: " << t2*f +50*50<< endl;
}

AdviserEmployee::AdviserEmployee(){
    type = "AdviserEmployee";
}

AdviserEmployee::AdviserEmployee(string name,Salary sal,Date start,Date last):Employee(name,sal,start,last){
    this->type = "AdviserEmployee";
}

AdviserEmployee::AdviserEmployee(const AdviserEmployee& x):Employee(x){
    type = x.type;
}

AdviserEmployee::~AdviserEmployee(){}

AdviserEmployee& AdviserEmployee::operator=(const AdviserEmployee& x){
    if(this == &x)
        return *this;
    type = x.type;
    return *this;
}

void AdviserEmployee::getMessage(){
    cout << "no: " << this->getNo() << endl;
    cout << "name: " << this->getName() << endl;
    cout << "employeeType: " << type << endl;
    cout << "salary:  according to the counts of asking" << endl;
    Date t3 = this->getStart();
    cout << "startDate: ";
    t3.pritDate();
    t3 = this->getLast();
    cout << "lastDate: " ;
    t3.pritDate();

    cout << "please input any key to continue_ ";
    char ch = getchar();
}

void AdviserEmployee::printSalary(){
    cout << "count salary: " << 25*50 << endl;
}

SaleAdviserEmployee::SaleAdviserEmployee(){type = "SaleAdviserEmployee";}

SaleAdviserEmployee::SaleAdviserEmployee(string name,Salary sal,Date start,Date last):Employee(name,sal,start,last){
    type = "SaleAdviserEmployee";
}

SaleAdviserEmployee::SaleAdviserEmployee(const SaleAdviserEmployee& x):Employee(x){
    type = x.type;
}

SaleAdviserEmployee::~SaleAdviserEmployee(){}

SaleAdviserEmployee& SaleAdviserEmployee::operator=(const SaleAdviserEmployee& x){
    if(this == &x)
        return *this;
    type = x.type;
    return *this;
}

void SaleAdviserEmployee::getMessage(){
    cout << "no: " << this->getNo() << endl;
    cout << "name: " << this->getName() << endl;
    cout << "employeeType: " << type << endl;
    cout << "salary:  according to the counts of asking and sale" << endl;
    Date t3 = this->getStart();
    cout << "startDate: ";
    t3.pritDate();
    t3 = this->getLast();
    cout << "lastDate: " ;
    t3.pritDate();

    cout << "please input any key to continue_ ";
    char ch = getchar();
}

void SaleAdviserEmployee::printSalary(){
    cout << "count salary: " << 25*300 + 12*50 << endl;
}

SalaryManager::SalaryManager(){}

SalaryManager::~SalaryManager(){}

void SalaryManager::insertEmployee(Employee* x){ob.push_back(x);}

void SalaryManager::delEmployee(){ob.pop_back();}

void SalaryManager::getTotalSalary(){
    cout << "Total Employee's Salary:\n\n";
    vector<Employee*>::iterator it;
    for(it=ob.begin();it!=ob.end();it++){
        cout << "no: "<< (*it)->getNo() << endl;
        cout << "name: " << (*it)->getName() << endl;
        cout << "salary: " ;
        (*it)->printSalary();
        cout << endl << endl;
    }
}

void SalaryManager::getTotalInfo(){
    cout << "Total Employee's Information:\n\n";
    vector<Employee*>::iterator it;
    for(it=ob.begin();it!=ob.end();it++){
        (*it)->getMessage();
        cout<< endl << endl;
    }
}









//main.cpp
#include 
#include "class.h"

using namespace std;

int main()
{
    Date s(2000,2,20);
    Date e(2020,6,1);
    Date e1(2019,6,1);
    Salary m1(500);
    Salary m2(400);
    Salary m3;

    ProductLineEmployee p1("Tom1",m1,s,e);
    ProductLineEmployee p2("Tom2",m1,s,e1);

    SaleEmployee s1("Amy",m2,s,e);

    AdviserEmployee a1("Jack1",m3,s,e);
    AdviserEmployee a2("Jack2",m3,s,e);
    AdviserEmployee a3("Jack3",m3,s,e);

    SaleAdviserEmployee sa1("Lucy1",m3,s,e);
    SaleAdviserEmployee sa2("Lucy2",m3,s,e);

    SalaryManager m;
    m.insertEmployee(&p1);
    m.insertEmployee(&p2);
    m.insertEmployee(&s1);
    m.insertEmployee(&a1);
    m.insertEmployee(&a2);
    m.insertEmployee(&a3);
    m.insertEmployee(&sa1);
    m.insertEmployee(&sa2);

    m.getTotalInfo();
    cout << "" << endl;
    m.getTotalSalary();


    return 0;
}

部分功能测试

C++ 建立工资计算系统(3)_第9张图片
C++ 建立工资计算系统(3)_第10张图片
C++ 建立工资计算系统(3)_第11张图片
C++ 建立工资计算系统(3)_第12张图片
C++ 建立工资计算系统(3)_第13张图片


更多相关内容请参见

我的博客

你可能感兴趣的:(C++,program,c++,类)