欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

建立工资计算系统(2)

程序员文章站 2022-05-22 12:49:25
...

问题描述

某企业为了提升自身管理效率,特别委托你为企业设计一个自动计算和发放员工工资的软件,具体设计内容包括:
1、随着该企业规模的扩展,企业的员工类型逐渐多样化,工资系统的管理方式要随之发生改变。企业目前的员工主要分为三类,第一类为生产线员工,每工作一天企业记工资500元;第二类为销售员工,每工作一天企业记工资400元,但是每销售一件产品提成50元;第三类为顾问类员工,工资按咨询次数记,每次300元。
2、请在 工资系统(1) 的基础上分别为这三类员工设计与之对应的员工类,他们共同继承超类Employee,超类表示一个企业员工的基本信息,并定义所有员工类都必须具有的接口,比如个人信息显示、属性设置和读取、工资计算和发放等。
3、在每个派生员工类中设计与员工类型对应的工资计算方法,同时根据每种员工工资计算需要的属性的不同,对每个子类进行扩展,为每个子类设计个人信息和工资信息的输出方法;
4、上面类所有成员变量使用private修饰,为这些属性编写设置和读取方法,同时在设置方法中还需要对每个属性值的设置范围进行检查,如果超出取值范围,请为该值设置初始值并提示用户。
5、编写一个测试方法,每种员工类型实例化两个对象,并采用适当的属性值进行初始化,对每个员工的工资进行计算并输出该员工相关信息。计算需要支付给这两个员工的工资。


类图设计


总体类图
建立工资计算系统(2)
Employee类
建立工资计算系统(2)
ProductLineEmployee类
建立工资计算系统(2)

SaleEmployee类
建立工资计算系统(2)
AdviserEmployee类
建立工资计算系统(2)


源代码

//class.h
#include <string>

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);
    void getMessage();
};

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(int amount);
};

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(int c);
};


//class.cpp
#include "classDefine.h"
#include <iostream>

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();
}

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(int amount){
    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 +amount*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(int c){
    cout << "count salary: " << c*50 << endl;
}




//main.cpp
#include <iostream>
#include "classDefine.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);
    p1.getMessage();
    p1.printSalary();
    cout << "*********************************" << endl;
    p2.getMessage();
    p2.printSalary();

    cout << endl << endl << "-----------------------------------------------" << endl << endl;

    SaleEmployee s1("Amy1",m2,s,e);
    SaleEmployee s2("Amy2",m2,s,e);

    s1.getMessage();
    s1.printSalary(10);
    cout << "*********************************" << endl;
    s2.getMessage();
    s2.printSalary(15);

    cout << endl << endl << "-----------------------------------------------" << endl << endl;

    AdviserEmployee a1("Jack1",m3,s,e);
    AdviserEmployee a2("Jack2",m3,s,e);
    a1.getMessage();
    a1.printSalary(20);

    cout << "*********************************" << endl;
    a2.getMessage();
    a2.printSalary(15);

    return 0;
}


测试

建立工资计算系统(2)建立工资计算系统(2)建立工资计算系统(2)


更多相关内容请参见

我的博客

相关标签: C++ 程序 c++