demo 熟悉一下C++代码编写
- main.cpp
//
// main.cpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/11.
//
#include <iostream>
#include <fstream> // 文件操作
using namespace std;
#include "workerManager.hpp"
#include "worker.hpp"
#include "employee.hpp"
#include "manager.hpp"
#include "boss.hpp"
void test()
{
Worker *worker = NULL;
worker = new Employee(001, "张三", 1);
worker->showInfo();
delete worker;
worker = new Manager(002, "李经理", 2);
worker->showInfo();
delete worker;
worker = new Boss(003, "王总", 3);
worker->showInfo();
delete worker;
}
int main(int argc, const char * argv[]) {
// test();
// return 0;
WorkerManager wm;
wm.showMenu();
int choice = 0;
while (true) {
wm.showMenu();
cout << "请输入选择:" << endl;
cin >> choice;
switch (choice) {
case 0:
{
cout << " 选择了0 " << endl;
wm.exitSystem();
break;
}
case 1:
{
wm.addEmployee();
cout << " 选择了1 " << endl;
break;
}
case 2:
{
wm.show_Emp();
cout << " 选择了2 " << endl;
break;
}
case 3:
{
wm.delete_Emp();
cout << " 选择了3 " << endl;
break;
}
case 4:
{
wm.modify_Emp();
cout << " 选择了4 " << endl;
break;
}
case 5:
{
cout << " 选择了5 " << endl;
break;
}
case 6:
{
cout << " 选择了6 " << endl;
break;
}
case 7:
{
cout << " 选择了7 " << endl;
break;
}
default:
break;
}
}
return 0;
}
- worker.hpp
//
// worker.hpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#pragma once
#include <iostream>
using namespace std;
#ifndef worker_hpp
#define worker_hpp
#include <stdio.h>
/**
抽象职工类
*/
class Worker
{
public:
// 显示个人信息
virtual void showInfo() = 0;
// 获取岗位名称
virtual string getDeptName() = 0;
int m_Id; // 职工编号
string m_Name; // 职工姓名
int m_DeptId; // 职工所在部门名称编号
virtual ~Worker() {}
};
#endif /* worker_hpp */
- workerManager.cpp
//
// workerManager.cpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#include "workerManager.hpp"
// 生命周期
WorkerManager::WorkerManager()
{
cout << "WorkerManager()" << endl;
ifstream ifs;
ifs.open(FILEPATH, ios::in);
// 1. 判断文件是否存在
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
this->m_EmpNum = 0;
this->m_EmpArray = NULL;
this->m_isFileEmpty = true;
ifs.close();
return;
}
// 2.文件存在,但是文件为空
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空" << endl;
this->m_EmpNum = 0;
this->m_EmpArray = NULL;
this->m_isFileEmpty = true;
ifs.close();
return;
}
else
{
this->m_isFileEmpty = false;
ifs.close();
}
// 3.文件不为空,统计人数
this->get_EmpNum();
this->m_EmpArray = new Worker *[this->m_EmpNum];
this->int_Emp();
// // 测试输出
// for (int i = 0; i < m_EmpNum; i++)
// {
// cout << "职工号:" << this->m_EmpArray[i]->m_Id
// << "职工姓名:" << this->m_EmpArray[i]->m_Name
// << "职工编号:" << this->m_EmpArray[i]->m_DeptId << endl;
// }
}
WorkerManager::~WorkerManager()
{
if (this->m_EmpArray != NULL)
{
delete [] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}
// 需求方法
void WorkerManager::showMenu()
{
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 欢迎使用职工管理系统 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 0.退出管理程序 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 1.增加职工信息 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 2.显示职工信息 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 3.删除职工信息 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 4.修改职工信息 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 5.查找职工信息 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 6.按照编号排序 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~ 7.清空所有文档 ~~~~~~~~~~~" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
void WorkerManager::exitSystem()
{
cout << "退出职工系统" << endl;
exit(0);
}
void WorkerManager:: addEmployee()
{
cout << "请输入添加职工数量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
// 新数组的元素的个数
int newSize = this->m_EmpNum + addNum;
cout << "newSize: " << this->m_EmpNum << endl;
// 创建新数组,用来存放指针的指针
Worker ** newSpace = new Worker*[newSize];
// 1. 先把原来的数组拷贝过来
if (this->m_EmpArray != NULL)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
newSpace[i] = this->m_EmpArray[i];
}
}
// 2.再批量添加新数据
for (int i = 0; i < addNum; i++)
{
int id,dSelect; // 编号、部门
string name; // 姓名
cout << "请输入第 " << i + 1 << "个新职工编号" << endl;
cin >> id;
cout << "请输入第 " << i + 1 << "个新职工姓名" << endl;
cin >> name;
cout << "请选择该职工岗位:" << endl;
cout << "1. 普通职工" << endl;
cout << "2. 经理" << endl;
cout << "3. 老板" << endl;
cin >> dSelect;
Worker *woker = NULL;
switch (dSelect) {
case 1:
woker = new Employee(id, name, 1);
break;
case 2:
woker = new Manager(id, name, 2);
break;
case 3:
woker = new Boss(id, name, 3);
break;
default:
break;
}
newSpace[this->m_EmpNum + i] = woker;
}
delete [] this->m_EmpArray;
this->m_EmpArray = newSpace;
this->m_EmpNum = newSize;
this->save();
}
else
{
cout << "输入有误" << endl;
}
}
void WorkerManager:: save()
{
ofstream ofs;
ofs.open(FILEPATH, 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_DeptId << " " << endl;
}
ofs.close();
}
int WorkerManager:: get_EmpNum()
{
ifstream ifs;
ifs.open(FILEPATH, ios::in);
int id;
string name;
int dId;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> dId)
{
num++;
}
ifs.close();
cout << "人数:" << num << endl;
this->m_EmpNum = num;
return num;
}
void WorkerManager::int_Emp()
{
ifstream ifs;
ifs.open(FILEPATH,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 if (dId == 3)
{
worker = new Boss(id, name, dId);
}
this->m_EmpArray[index] = worker;
index ++;
}
}
void WorkerManager:: show_Emp()
{
if (this->m_isFileEmpty)
{
cout << "文件不存在或为空" << endl;
}
else
{
for (int i = 0; i < m_EmpNum; i++)
{
// 利用多态调用接口
this->m_EmpArray[i]->showInfo();
}
}
}
void WorkerManager:: delete_Emp()
{
cout << "请输入想要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->isExit(id);
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 << "删除成功" << endl;
}
else
{
cout << "未找到该职工" << endl;
}
}
int WorkerManager:: isExit(int id)
{
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Id == id)
{
index = i;
break;
}
}
return index;
}
void WorkerManager:: modify_Emp()
{
if (this->m_isFileEmpty)
{
cout << "文件不存在或为空" << endl;
}
else
{
cout << "请输入要删除的职工编号" << endl;
int id;
cin >> id;
// 先删除原来的职工
int res = this->isExit(id);
if (res != -1)
{
delete this->m_EmpArray[res];
}
// 再添加新职工
this->addEmployee();
}
}
- workerManager.hpp
//
// workerManager.hpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#pragma once
#include <iostream>
using namespace std;
#ifndef workerManager_hpp
#define workerManager_hpp
#include <stdio.h>
#include "worker.hpp"
#include "employee.hpp"
#include "manager.hpp"
#include "boss.hpp"
#include <fstream>
#define FILEPATH "/Users/runhe_ios_jh/Documents/07_自动化测试/empFile.txt"
class WorkerManager
{
public:
// 生命周期
WorkerManager();
~WorkerManager();
// 方法
void showMenu();
void exitSystem();
void addEmployee();
void show_Emp();
void delete_Emp();
void modify_Emp();
private:
int get_EmpNum();
void int_Emp();
int isExit(int id);
void save();
// 属性
// 要给一个初始值
int m_EmpNum = 0; // 职工人数
Worker ** m_EmpArray = NULL; // 职工数组指针
bool m_isFileEmpty;
};
#endif /* workerManager_hpp */
- employee.cpp
//
// employee.cpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#include "employee.hpp"
Employee:: Employee(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dId;
cout << "员工创建" << endl;
}
void Employee:: showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工姓名:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成经历交给的任务" << endl;
}
string Employee:: getDeptName()
{
return string("员工");
}
Employee::~Employee()
{
cout << "员工释放" << endl;
cout << "---" << endl;
}
- employee.hpp
//
// employee.hpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#pragma once
#include <iostream>
#include "worker.hpp"
using namespace std;
#ifndef employee_hpp
#define employee_hpp
#include <stdio.h>
class Employee: public Worker
{
public:
// 构造函数
Employee(int id, string name, int dId);
// 显示个人信息
virtual void showInfo() override;
// 获取员工部门信息
virtual string getDeptName() override;
~Employee();
};
#endif /* employee_hpp */
- manager.cpp
//
// manager.cpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#include "manager.hpp"
Manager:: Manager(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dId;
cout << "经理创建" << endl;
}
void Manager:: showInfo()
{
cout << "经理编号:" << this->m_Id
<< "\t经理姓名:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:安排任务" << endl;
}
string Manager:: getDeptName()
{
return string("经理");
}
Manager:: ~Manager()
{
cout << "经理释放" << endl;
cout << "---" << endl;
}
- manager.hpp
//
// manager.hpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#pragma once
#include <iostream>
#include "worker.hpp"
using namespace std;
#ifndef manager_hpp
#define manager_hpp
#include <stdio.h>
class Manager: public Worker
{
public:
Manager(int id, string name, int dId);
virtual void showInfo() override;
virtual string getDeptName() override;
~Manager();
};
#endif /* manager_hpp */
- boss.cpp
//
// boss.cpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#include "boss.hpp"
Boss:: Boss(int id, string name, int dId)
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = dId;
cout << "Boss创建" << endl;
}
void Boss:: showInfo()
{
cout << "Boss编号:" << this->m_Id
<< "\tBoss姓名:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:Boss赚钱" << endl;
}
string Boss:: getDeptName()
{
return string("Boss");
}
Boss:: ~Boss()
{
cout << "Boss释放" << endl;
cout << "---" << endl;
}
- boss.hpp
//
// boss.hpp
// Cppbase
//
// Created by Runhe_iOS_JH on 2022/8/26.
//
#pragma once
#include <iostream>
#include "worker.hpp"
using namespace std;
#ifndef boss_hpp
#define boss_hpp
#include <stdio.h>
class Boss: public Worker
{
public:
Boss(int id, string name, int dId);
virtual void showInfo() override;
virtual string getDeptName() override;
~Boss();
};
#endif /* boss_hpp */