建立工资计算系统(1)——员工和工资

问题描述

1、创建一个Date类,能够表示一个日期,除了对日期进行输出、设置的一般成员方法以外,还需要一个方法,能够计算对象所保存日期与参数所给日期之间的差距,计算单位为“月”,同时重载Date类的赋值运算符。
2、创建一个Employee类,能够保存一个企业员工的基本信息,除了基本信息属性以外,还需要属性保存该员工的月工资、开始工作的日期和上一次发放工资的日期。
3、创建一个Salary类,可以用来表示一个工资金额,除基本成员变量和访问方法外,请为Salary类设计+、-运算符的重载函数;
4、上面类所有成员变量使用private修饰,为这些属性编写设置和读取方法,同时在设置方法中还需要对每个属性值的设置范围进行检查,如果超出取值范围,请为该值设置初始值并提示用户;请为两个类编写构造方法,初始化所有属性的值。
编写一个测试main方法,实例化两个员工对象,其月工资分别为1500和2500,工作日期都是2010年2月10日,请计算需要支付给这两个员工的工资。


问题分析

类图设计
DateSalaryEmployee


源码


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

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

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

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

using namespace std;

int main()
{
    Date d1;
    Date d2(2010,2,10);
    Employee e1("jack",1500,d1,d2);
    Employee e2("Tom",2500,d1,d2);

    Date t1 = e1.getStart();
    Date t2 = e2.getStart();
    Salary ss1 = e1.getSalary();
    Salary ss2 = e2.getSalary();
    double s1 = ss1.getMoney();
    double s2 = ss2.getMoney();
    float re1 = t1.returnSub(d2) * s1 ;
    float re2 = t2.returnSub(d2) * s2;
    cout << re1 << endl;
    cout << re2 << endl;

    return 0;
}


测试

result


更多相关内容请参见

我的博客

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值