码迷,mamicode.com
首页 > 编程语言 > 详细

雇员记录系统(C++)

时间:2020-01-15 09:51:11      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:dde   string   nat   hal   ISE   sea   record   orm   str   

包含两个类:Employee及Dataset

//employee.h
#pragma once
#include<string>
namespace Record {
    const int kDefaultstartingSalary = 30000;
    class Employee {
    public:
        Employee();
        void promote(int raiseAmount = 1000);
        void demote(int demoritAmount = 1000);
        void hire();
        void fire();
        void display() const;
        void setFirstName(const std::string& firstName);
        const std::string &getFirstName() const;
        void setLastName(const std::string& lastName);
        const std::string& getLastName()const;
        void setEmployeeNumber(int Employeenumber);
        int getEmployeeNum()const;

        void setSalary(int newSalary);
        int getSalary()const;

        bool getIsHired() const;
    private:
        std::string mFirstName;
        std::string mLastName;
        int mEmployeeNumber;
        int mSalary;
        bool mIsHired;

    };
}
//employee.cpp
#include<iostream>
#include"employee.h"

using namespace std;
namespace Record {
    Employee::Employee()
        :mFirstName(""),
        mLastName(""),
        mEmployeeNumber(-1),
        mSalary(kDefaultstartingSalary),
        mIsHired(false)
    {}
    void Employee::promote(int raiseAmount)
    {
        setSalary(getSalary() + raiseAmount);
    }
    void Employee::demote(int raiseAmount)
    {
        setSalary(getSalary() - raiseAmount);
    }
    void Employee::hire()
    {
        mIsHired = true;
    }
    void Employee::fire()
    {
        mIsHired = false;
    }
    void Employee::display()const
    {
        cout << "Employee:" << getLastName() << "," << getFirstName() << endl;
        cout << "---------------" << endl;
        cout << (mIsHired ? "Current Employee" : "Promote Employee") << endl;
        cout << "Employee number:" << getEmployeeNum() << endl;
        cout << "Salary:$" << getSalary() << endl;
        cout << endl;
    }
    void Employee::setFirstName(const std::string& firstName)
    {
        mFirstName = firstName;
    }
    const std::string& Employee::getFirstName() const
    {
        return mFirstName;
    }
    void Employee::setLastName(const std::string& lastName)
    {
        mLastName = lastName;
    }
    const std::string& Employee::getLastName()const
    {
        return mLastName;
    }
    void Employee::setEmployeeNumber(int Employeenumber)
    {
        mEmployeeNumber = Employeenumber;
    }
    int Employee::getEmployeeNum()const
    {
        return mEmployeeNumber;
    }
    void Employee::setSalary(int newSalary)
    {
        mSalary = newSalary;
    }
    int Employee::getSalary()const
    {
        return mSalary;
    }
    bool Employee::getIsHired() const
    {
        return mIsHired;
    }
}
//Dataset.h
#pragma once
#include<iostream>
#include<vector>
#include"employee.h"
namespace Record
{
    const int kFirstEmployeeNumber = 1000;
    class Dataset{
    public:
        Dataset();
        Employee& addEmployee(const std::string firstName,
                              const std::string lastName);
        Employee& getEmployee(int employeeNum);
        Employee& getEmployee(const std::string firstName,
            const std::string lastName);
        void DisplayAll()const;
        void DisplayCurrent()const;
    private:
        std::vector<Employee>mEmployees;
        int mNextEmployeenumber;
    };
}
//Dataset.cpp
#include<iostream>
#include<stdexcept>
#include"Dataset.h"
using namespace std;
namespace Record {
    Dataset::Dataset() :mNextEmployeenumber(kFirstEmployeeNumber)
    {}
    Employee& Dataset::addEmployee(const string firstName, const string lastName)
    {
        Employee theEmployee;
        theEmployee.setFirstName(firstName);
        theEmployee.setLastName(lastName);
        theEmployee.setEmployeeNumber(mNextEmployeenumber++);
        theEmployee.hire();
        mEmployees.push_back(theEmployee);
        return mEmployees[mEmployees.size() - 1];
    }
    Employee& Dataset::getEmployee(int employeeNum)
    {
        for (auto employee : mEmployees)
        {
            if (employee.getEmployeeNum() == employeeNum)
                return employee;
        }
        throw runtime_error("No Employee found.");
    }
    Employee& Dataset::getEmployee(const std::string firstName,
        const std::string lastName)
    {
        for (auto employee : mEmployees)
        {
            if (employee.getFirstName() == firstName && employee.getLastName() == lastName)
                return employee;
        }
        throw runtime_error("No Employee found.");
    }
    void Dataset::DisplayAll()const
    {
        for (const auto & employee : mEmployees)
        {
            employee.display();
        }
    }
    void Dataset::DisplayCurrent()const
    {
        for (const auto& employee : mEmployees)
        {
            if (employee.getIsHired())
                employee.display();
        }
    }
}
//EmployeeTest.cpp
#include<iostream>
#include"employee.h"
using namespace std;
using namespace Record;

int main(int args, char* argv[])
{
    cout << "Testing the Employee Class" << endl;
    Employee emp;
    emp.setLastName("Doe");
    emp.setFirstName("John");
    emp.setEmployeeNumber(71);
    emp.setSalary(50000);
    emp.promote();
    emp.promote(50);
    emp.hire();
    emp.display();
    return 0;
}
//DatasetTest.cpp
#include<iostream>
#include"Dataset.h"

using namespace std;
using namespace Record;

int main()
{
    Dataset myDat;
    Employee& emp1 = myDat.addEmployee("John", "Doe");
    emp1.fire();
    Employee& emp2 = myDat.addEmployee("Michale", "Jackson");
    emp2.setSalary(100000);
    Employee& emp3 = myDat.addEmployee("Jack", "Ma");
    emp3.setSalary(100000);
    emp3.promote();
    cout << "Display all ." << endl;
    myDat.DisplayAll();
    cout << "+++++++++++++++++" << endl;
    cout << "Display Current Employee." << endl;
    myDat.DisplayCurrent();
    return 0;
}
//UserInterface.cpp
#include<iostream>
#include<stdexcept>
#include<exception>
#include"Dataset.h"
#include"employee.h"

using namespace std;
using namespace Record;

int DisplayMenu();
void doHire(Dataset& db);
void doFire(Dataset& db);
void doPromote(Dataset& db);



int main()
{
    Dataset employeeDB;
    bool done = false;
    while (!done)
    {
        int selection = DisplayMenu();
        switch (selection)
        {
        case 1:
            doHire(employeeDB);
            break;
        case 2:
            doFire(employeeDB);
            break;
        case 3:
            doPromote(employeeDB);
            break;
        case 4:
            employeeDB.DisplayAll();
            break;
        case 5:
            employeeDB.DisplayCurrent();
            break;
        case 0:
            done = true;
            break;
        default:
            cerr << "Unknow command." << endl;
            break;


        }
    }
    return 0;
}
int DisplayMenu()
{
    int selection;
    cout << endl;
    cout << "Employee Dataset" << endl;
    cout << "--------------" << endl;
    cout << "1) Hire a new employee" << endl;
    cout << "2) Fire an employee" << endl;
    cout << "3) Promote an employee" << endl;
    cout << "4) List all employees" << endl;
    cout << "5) List all current employees" << endl;
    cout << "6) List all former employees" << endl;
    cout << "0) Quit" << endl;
    cout << endl;
    cout << "------>";
    cin >> selection;
    return selection;
}

void doHire(Dataset& db)
{
    string firstName;
    string lastName;
    cout << "First name? ";
    cin >> firstName;
    cout << "Last Name? ";
    cin >> lastName;
    try {
        db.addEmployee(firstName, lastName);
    }
    catch (const std::exception& exception)
    {
        cerr << "Unable to add new employee:" << exception.what() << endl;
    }
}

void doFire(Dataset& db)
{
    int employeeNumber;
    cout << "employee num?" << endl;
    cin >> employeeNumber;
    try
    {
        Employee& emp = db.getEmployee(employeeNumber);
        emp.fire();
        cout << "Employee" << employeeNumber << " terminated." << endl;

    }
    catch(const std::exception&exception)
    {
        cerr << "Unable to terminate employee:" << exception.what() << endl;
    }
}

void doPromote(Dataset& db)
{
    int employeeNum;
    int raiseNum;
    cout << "employee num?" << endl;
    cin >> employeeNum;
    cout << "How much of a raise ?" << endl;
    cin >> raiseNum;
    try
    {
        Employee& emp = db.getEmployee(employeeNum);
        emp.promote(raiseNum);


    }
    catch (const std::exception& exception)
    {
        cerr << "Unable to Promote employee:" << exception.what() << endl;
    }
}

雇员记录系统(C++)

标签:dde   string   nat   hal   ISE   sea   record   orm   str   

原文地址:https://www.cnblogs.com/fourmi/p/12194863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!