C++ 进阶 2

具体的功能实现在workermanager.cpp文件中

#include"workermanager.h"   //添加对应的头文件

WorkerManager::WorkerManager()  // WorkerManager的构造函数
{
    // the file doesn't exit
    ifstream ifs;   //  创建数据流
    ifs.open("test1",ios::in); // 打开文件
    if(!ifs.is_open())   //判断是否可以打开成功,如果不能的话
    {
        cout << "the file is not exit" << endl;
        this->m_empnum = 0;   //把
        this->m_EmpArray = NULL;
        this->m_fileisempty = true;
        ifs.close();
        return;
    }
    //2.the file exit ,no data;
    char ch;
    ifs >> ch;
    if(ifs.eof())
    {
        cout << "file doesn't have data" << endl;
        this->m_empnum = 0;
        this->m_EmpArray = NULL;
        this->m_fileisempty = true;
        ifs.close();
        return; 
    }
    //3.file exit and have data
    int num = this->get_empnum();
    // cout << "there is " << " " << num << "people in the file" << endl;
    this->m_empnum = num;
    this->m_EmpArray = new Worker*[this->m_empnum];
    this->init_emp();
    this->m_fileisempty = false;
    // for(int i = 0;i <  this->m_empnum; i++)
    // {
    //     cout << "number " << " " << this->m_EmpArray[i]->m_ID
    //          << "name "   << " " << this->m_EmpArray[i]->m_Name
    //          << "position"<< " " << this->m_EmpArray[i]->m_DepId << endl;
    // }
}
void WorkerManager::showmenu()
{
    cout << "****************************************" << endl;
    cout << "****************************************" << endl;
    cout << "*******WELCOME TO USE THIS SYSTEM*******" << endl;
    cout << "*********** 0.EXIT THE SYSTEM **********" << endl;
    cout << "*********** 1.ADD  THE PEOPLE **********" << endl;
    cout << "*********** 2.SHOW THE PEOPLE **********" << endl;
    cout << "*********** 3.DELETE   PEOPLE **********" << endl;
    cout << "*********** 4.CHANGE   PEOPLE **********" << endl;
    cout << "*********** 5.SELETE   PEOPLE **********" << endl;
    cout << "*********** 6.SORT     PEOPLE **********" << endl;
    cout << "*********** 7.FORMAT   PEOPLE **********" << endl;
    cout << endl;
}
void WorkerManager::exitsystem()
{
    cout << "thanks for using " << endl;
    exit(0);
}
void WorkerManager::ADD_Emp()
{
    cout << "please input the num" << endl;
    int addnum = 0;
    cin >> addnum;
    if( addnum > 0)
    {
        int newsize = this->m_empnum + addnum;
        Worker ** newspace = new Worker*[newsize];
        if (this->m_EmpArray != NULL)
        {
            for (int i = 0; i < this->m_empnum; i++)
            {
                newspace[i] = this->m_EmpArray[i];
            }
        }
        for (int i = 0; i < addnum; i++)
        {
            int id;
            string name;
            int dSelect;
            cout << "please input the id of " << i+1 << endl;
            cin >> id;
            cout << "please input the name of " << i+1 << endl;
            cin >> name;
            cout << "please choose the position" << endl;
            cout << "1.employee" << endl;
            cout << "2.manager"  << endl;
            cout << "3.boss"     << endl;
            cin >> dSelect;
            Worker *worker = NULL;
            switch(dSelect)
            {
            case 1:
                worker = new Employee(id,name,1);
                break;
            case 2:
                worker = new Manager(id,name,2);
                break;
            case 3:
                worker = new Boss(id,name,3);
                break;
            default:
                break;
            }
            newspace[this->m_empnum + i] = worker;
        }
        delete [] this->m_EmpArray;
        this->m_EmpArray = newspace;
        this->m_empnum = newsize;
        this->m_fileisempty = false;
        this->save();
        cout << "success adding" << addnum << endl;
    }
    else
    {
        cout << "input error" << endl;
    }
    cout << "press a to  the main menu" << endl;
    string a;
    cin >> a;
    if (a == "a")
    {
        system("clear");
    }
}
void WorkerManager::save()
{
    ofstream ofs;
    ofs.open("test1",ios::out);
    for ( int i = 0; i < this->m_empnum; i++)
    {
        ofs << this->m_EmpArray[i]->m_ID << " "
            << this->m_EmpArray[i]->m_Name << " "
            << this->m_EmpArray[i]->m_DepId << endl;
    }
    ofs.close();
}
int WorkerManager::get_empnum()
{
    ifstream ifs;
    ifs.open("test1",ios::in);
    int id;
    string name;
    int did;
    int num = 0;
    while(ifs >> id &&  ifs >> name , ifs >> did)
    {
        num++;
    }
    return num;
}
void WorkerManager::init_emp()
{
    ifstream ifs;
    ifs.open("test1",ios::in);

    int id;
    string name;
    int did;
    int index = 0;
    while (ifs >> id && ifs >> name && ifs >> did)
    {
       Worker *worker = NULL;
       if(did == 1)
       {
           worker = new Employee(id,name,did);
       }
       else if (did == 2)
       {
           worker = new Manager(id,name,did);
       }
       else 
       {
           worker = new Boss(id,name,did);
       }
       this->m_EmpArray[index] = worker;
       index++;
    }
    ifs.close();
}
void WorkerManager::show_emp()
{
    if(this->m_fileisempty)
    {
        cout << "file is not exit or no data" << endl;
    }
    else
    {
        for(int i = 0; i < m_empnum ; i++ )
        {
            this->m_EmpArray[i]->showInfo();
        }
    }
    cout << "press a to main system" << endl;
    string a;
    cin >> a;
    if( a == "a")
    {
        system("clear");
    }
}
int WorkerManager::isexit(int findnum)
{
    int num = findnum;
    if(this->m_fileisempty)
    {
        cout << "file is not exit or no data" << endl;
    }
    else 
    {
        cout << " " << endl;
    }
    cout << num << endl;
    int index = -1;
    for ( int i = 0; i < this->m_empnum; i++)
    {
        if (num == (this->m_EmpArray[i]->m_ID))
        {
            index = i;
            break;
        }
    }
    return index;
    
}
void WorkerManager::del_emp()
{
    if(this->m_fileisempty)
    {
        cout << "the file is not exit or dong't have data" << endl;
    }
    else
    {
        cout << "please input the number you want to find ;  " << endl;
        int id = 0;
        cin >> id;
        int index = this->isexit(id);
        cout << index << endl;
        if (index != -1)
        {
            for ( int i = index; i != this->m_empnum-1; i++)
            {
                this->m_EmpArray[i] = this->m_EmpArray[i+1];
            }
            this->m_empnum--;
            this->save();
            cout << "delete ok" << endl;
        }
        else 
        {
            cout << "delete error " << endl;
        }
    }
    cout << "please press a to main system" << endl;
    string a;
    cin >> a;
    if(a == "a" )
    {
        system("clear");
    }
    
}

void WorkerManager::mod_emp()
{
    if(this->m_fileisempty)
    {
        cout << "the file is not exit or don't have data";
    }
    else
    {
        cout << "please input num of the one you want to change " << endl;
        int id;
        cin >> id;
        int ret = this->isexit(id);
        if(ret != -1)
        {
            delete this->m_EmpArray[ret];
            int newId = 0;
            string newname = "";
            int dselect = 0;
            cout << id << ": worker" <<  "input id" << endl;
            cin >> newId;
            cout << "input name" << endl;
            cin >> newname;
            cout << "input the position" << endl; 
            cout << "1.employee" << endl;
            cout << "2.manager"  << endl;
            cout << "3.boss"     << endl;
            cin >> dselect;
            Worker * worker = NULL;
            switch(dselect)
           {
            case 1:
                worker = new Employee(newId,newname,1);
                break;
            case 2:
                worker = new Manager(newId,newname,2);
                break;
            case 3:
                worker = new Boss(newId,newname,3);
                break;
            default:
                break;
            }
            this->m_EmpArray[ret] = worker;
            cout << " change ok" << this->m_EmpArray[ret]->m_DepId << endl;
            this->save();
        }
        else
        {
            cout << "there is no this one" << endl;
        } 
    }
    cout << "press a to main system" <<  endl;
    string a;
    cin >> a;
    if(a == "a")
    {
        system("clear");
    }
}
void WorkerManager::select_emp()
{
    if(this->m_fileisempty)
    {
        cout << "the file is not exit " << endl;
    }
    else
    {
        cout << "please input the number you want to find ;  " << endl;
        int id = 0;
        cin >> id;
        int index = this->isexit(id);
        cout << index << endl;
        if (index != -1)
        {
            // cout << this->m_EmpArray[index]->m_ID 
            //      << this->m_EmpArray[index]->m_Name
            //      << this->m_EmpArray[index]->m_DepId << endl;
           this->m_EmpArray[index]->showInfo();

        }
        else
        {
            cout << "don't find this" << endl;
        }
        
    }
} 
void WorkerManager::sort_emp()
{
    if(this->m_fileisempty)
    {
        cout << "not exit" << endl;
        string a;
        cin >> a;
        if(a == "a")
        {
            system("clear");
        }
    }
    else
    {
        cout << "choose the way to sort" << endl;
        cout << "1.      shengxu" << endl;
        cout << "2.      jiangxu" << endl;
        int select = 0;
        cin >> select;
        for( int i = 0; i < m_empnum; i++ )
        {
            int minormax = i;
            for( int j = i + 1; j < this->m_empnum;j++)
            {
                if(select == 1)
                {
                    if(this->m_EmpArray[minormax]->m_ID > this->m_EmpArray[j]->m_ID)
                    {
                     minormax = j;
                    }
                }
                else
                {
                    if(this->m_EmpArray[minormax]->m_ID < this->m_EmpArray[j]->m_ID)
                    {
                        minormax = j;
                    }
                }
            }
            if(i != minormax)
            {
                Worker * temp = this->m_EmpArray[i];
                this->m_EmpArray[i] = this->m_EmpArray[minormax];
                this->m_EmpArray[minormax] = temp;
                cout << "111";
            }
        }
        cout << "the result is " << endl;
        this->save();
        this->show_emp();
    }
}
void WorkerManager::clean_emp()
{
    cout << "are u sure" << endl;
    cout << "1.sure" << endl;
    cout << "2.reback"<< endl;

    int select = 0;
    cin >> select;
    if( select == 1)
    {
        ofstream ofs("test1",ios::trunc);
        ofs.close();
        if(this->m_EmpArray != NULL)
        {
            for( int i = 0; i < this->m_empnum; i++)
            {
                if(this->m_EmpArray[i] != NULL)
                {
                    delete this->m_EmpArray[i];
                }
            }
            this->m_empnum = 0;
            delete[] this->m_EmpArray;
            this->m_EmpArray = NULL;
            this->m_fileisempty = true;
        }
        cout << "delete ok" << endl;
    }
    string a;
    cin >> a;
    if(a == "a")
    {
        system("clear");
    }
} 
WorkerManager::~WorkerManager()
{
    if(this->m_EmpArray != NULL)
    {
        for( int i = 0; i < this->m_empnum; i++)
        {
            if(this->m_EmpArray[i] != NULL)
            {
                delete this->m_EmpArray[i];
            }
        }
        delete[] this->m_EmpArray;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值